Browse Source

:gear: Always set a connmanager

The connmanager is also responsible for tracking max connections, and we
want to do this globally always. Expose a max-connections option
Ettore Di Giacinto 3 years ago
parent
commit
09d65fb100
5 changed files with 36 additions and 5 deletions
  1. 26 0
      cmd/util.go
  2. 0 2
      pkg/services/files.go
  3. 2 3
      pkg/services/services.go
  4. 6 0
      pkg/vpn/config.go
  5. 2 0
      pkg/vpn/vpn.go

+ 26 - 0
cmd/util.go

@@ -106,6 +106,12 @@ var CommonFlags []cli.Flag = []cli.Flag{
 		Usage:  "Changes the default rate limiting configured in helping other peers determine their reachability status",
 		EnvVar: "EDGEVPNNATRATELIMIT",
 	},
+	&cli.IntFlag{
+		Name:   "max-connections",
+		Usage:  "Max connections",
+		EnvVar: "EDGEVPNMAXCONNS",
+		Value:  100,
+	},
 	&cli.StringFlag{
 		Name:   "ledger-state",
 		Usage:  "Specify a ledger state directory",
@@ -151,6 +157,11 @@ var CommonFlags []cli.Flag = []cli.Flag{
 		Usage:  "Enable low profile. Lowers connections usage",
 		EnvVar: "EDGEVPNLOWPROFILE",
 	},
+	&cli.BoolFlag{
+		Name:   "low-profile-vpn",
+		Usage:  "Enable low profile on vpn. Doesn't keep open connections",
+		EnvVar: "EDGEVPNVPNLOWPROFILE",
+	},
 	&cli.StringFlag{
 		Name:   "log-level",
 		Usage:  "Specify loglevel",
@@ -260,6 +271,10 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 
 	libp2pOpts := []libp2p.Option{libp2p.UserAgent("edgevpn")}
 
+	if c.Bool("low-profile-vpn") {
+		vpnOpts = append(vpnOpts, vpn.LowProfile)
+	}
+
 	if c.Bool("autorelay") {
 		libp2pOpts = append(libp2pOpts, libp2p.EnableAutoRelay())
 	}
@@ -272,6 +287,17 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 		))
 	}
 
+	cm, err := connmanager.NewConnManager(
+		20,
+		c.Int("max-connections"),
+		connmanager.WithGracePeriod(80*time.Second),
+	)
+	if err != nil {
+		llger.Fatal("could not create connection manager")
+	}
+
+	libp2pOpts = append(libp2pOpts, libp2p.ConnectionManager(cm))
+
 	if c.Bool("low-profile") {
 		cm := connmanager.NewConnManager(20, 100, 80*time.Second)
 		libp2pOpts = append(libp2pOpts, libp2p.ConnectionManager(cm))

+ 0 - 2
pkg/services/files.go

@@ -88,7 +88,6 @@ func ShareFile(ll log.StandardLogger, announcetime time.Duration, fileID, filepa
 						}
 						io.Copy(stream, f)
 						f.Close()
-
 						stream.Close()
 
 						ll.Infof("(file %s) Done handling %s", fileID, stream.Conn().RemotePeer().String())
@@ -171,7 +170,6 @@ func ReceiveFile(ctx context.Context, ledger *blockchain.Ledger, n *node.Node, l
 				}
 
 				io.Copy(f, stream)
-
 				f.Close()
 
 				l.Infof("Received file %s to %s", fileID, path)

+ 2 - 3
pkg/services/services.go

@@ -87,7 +87,6 @@ func RegisterService(ll log.StandardLogger, announcetime time.Duration, serviceI
 
 					stream.Close()
 					c.Close()
-
 					ll.Infof("(service %s) Handled correctly '%s'", serviceID, stream.Conn().RemotePeer().String())
 				}()
 			}
@@ -158,7 +157,7 @@ func ConnectToService(ctx context.Context, ledger *blockchain.Ledger, node *node
 				}
 
 				// Open a stream
-				stream, err := node.Host().NewStream(context.Background(), d, protocol.ServiceProtocol.ID())
+				stream, err := node.Host().NewStream(ctx, d, protocol.ServiceProtocol.ID())
 				if err != nil {
 					conn.Close()
 					ll.Debugf("could not open stream '%s'", err.Error())
@@ -171,8 +170,8 @@ func ConnectToService(ctx context.Context, ledger *blockchain.Ledger, node *node
 				go copyStream(closer, conn, stream)
 				<-closer
 
-				stream.Close()
 				conn.Close()
+				stream.Close()
 				ll.Infof("(service %s) Done handling %s", serviceID, l.Addr().String())
 			}()
 		}

+ 6 - 0
pkg/vpn/config.go

@@ -39,10 +39,16 @@ type Config struct {
 	Timeout           time.Duration
 	Concurrency       int
 	ChannelBufferSize int
+	lowProfile        bool
 }
 
 type Option func(cfg *Config) error
 
+var LowProfile Option = func(cfg *Config) error {
+	cfg.lowProfile = true
+	return nil
+}
+
 // Apply applies the given options to the config, returning the first error
 // encountered (if any).
 func (cfg *Config) Apply(opts ...Option) error {

+ 2 - 0
pkg/vpn/vpn.go

@@ -178,7 +178,9 @@ func handleFrame(frame ethernet.Frame, c *Config, n *node.Node, ip net.IP, ledge
 	}
 
 	stream.Write(frame)
+	//if c.lowProfile {
 	stream.Close()
+	//}
 	return nil
 }