Browse Source

:gear: Enable low-profile mode

Ettore Di Giacinto 3 years ago
parent
commit
00429289ab
4 changed files with 43 additions and 18 deletions
  1. 21 3
      cmd/util.go
  2. 2 1
      go.mod
  3. 6 1
      pkg/discovery/dht.go
  4. 14 13
      pkg/node/options.go

+ 21 - 3
cmd/util.go

@@ -21,6 +21,8 @@ import (
 
 	"github.com/ipfs/go-log"
 	"github.com/libp2p/go-libp2p"
+	connmanager "github.com/libp2p/go-libp2p-connmgr"
+	dht "github.com/libp2p/go-libp2p-kad-dht"
 	"github.com/mudler/edgevpn/internal"
 	"github.com/mudler/edgevpn/pkg/blockchain"
 	"github.com/mudler/edgevpn/pkg/discovery"
@@ -143,6 +145,11 @@ var CommonFlags []cli.Flag = []cli.Flag{
 		Usage:  "Enable DHT for peer discovery",
 		EnvVar: "EDGEVPNDHT",
 	},
+	&cli.BoolTFlag{
+		Name:   "low-profile",
+		Usage:  "Enable low profile. Lowers connections usage",
+		EnvVar: "EDGEVPNLOWPROFILE",
+	},
 	&cli.StringFlag{
 		Name:   "log-level",
 		Usage:  "Specify loglevel",
@@ -184,7 +191,7 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 	iface := c.String("interface")
 	logLevel := c.String("log-level")
 	libp2plogLevel := c.String("libp2p-log-level")
-	dht, mDNS := c.Bool("dht"), c.Bool("mdns")
+	dhtE, mDNS := c.Bool("dht"), c.Bool("mdns")
 
 	ledgerState := c.String("ledger-state")
 
@@ -215,6 +222,12 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 		}
 	}
 
+	dhtOpts := []dht.Option{}
+
+	if c.Bool("low-profile") {
+		dhtOpts = append(dhtOpts, dht.BucketSize(20))
+	}
+
 	opts := []node.Option{
 		node.WithDiscoveryInterval(time.Duration(c.Int("discovery-interval")) * time.Second),
 		node.WithLedgerAnnounceTime(time.Duration(c.Int("ledger-announce-interval")) * time.Second),
@@ -224,8 +237,8 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 		node.WithBlacklist(c.StringSlice("blacklist")...),
 		node.LibP2PLogLevel(libp2plvl),
 		node.WithInterfaceAddress(address),
-		node.FromBase64(mDNS, dht, token),
-		node.FromYaml(mDNS, dht, config),
+		node.FromBase64(mDNS, dhtE, token, dhtOpts...),
+		node.FromYaml(mDNS, dhtE, config, dhtOpts...),
 	}
 
 	vpnOpts := []vpn.Option{
@@ -257,6 +270,11 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 		))
 	}
 
+	if c.Bool("low-profile") {
+		cm := connmanager.NewConnManager(20, 100, 80*time.Second)
+		libp2pOpts = append(libp2pOpts, libp2p.ConnectionManager(cm))
+	}
+
 	if c.Bool("holepunch") {
 		libp2pOpts = append(libp2pOpts, libp2p.EnableHolePunching())
 	}

+ 2 - 1
go.mod

@@ -10,7 +10,7 @@ require (
 	github.com/google/btree v1.0.1 // indirect
 	github.com/gookit/color v1.5.0 // indirect
 	github.com/hashicorp/errwrap v1.1.0 // indirect
-	github.com/hashicorp/golang-lru v0.5.4 // indirect
+	github.com/hashicorp/golang-lru v0.5.4
 	github.com/ipfs/go-cid v0.1.0 // indirect
 	github.com/ipfs/go-datastore v0.5.1 // indirect
 	github.com/ipfs/go-log v1.0.5
@@ -20,6 +20,7 @@ require (
 	github.com/labstack/echo/v4 v4.6.1
 	github.com/labstack/gommon v0.3.1 // indirect
 	github.com/libp2p/go-libp2p v0.17.0
+	github.com/libp2p/go-libp2p-connmgr v0.2.4
 	github.com/libp2p/go-libp2p-core v0.13.0
 	github.com/libp2p/go-libp2p-discovery v0.6.0
 	github.com/libp2p/go-libp2p-kad-dht v0.15.0

+ 6 - 1
pkg/discovery/dht.go

@@ -41,6 +41,11 @@ type DHT struct {
 	console              log.StandardLogger
 	RefreshDiscoveryTime time.Duration
 	dht                  *dht.IpfsDHT
+	dhtOptions           []dht.Option
+}
+
+func NewDHT(d ...dht.Option) *DHT {
+	return &DHT{dhtOptions: d}
 }
 
 func (d *DHT) Option(ctx context.Context) func(c *libp2p.Config) error {
@@ -67,7 +72,7 @@ func (d *DHT) startDHT(ctx context.Context, h host.Host) (*dht.IpfsDHT, error) {
 		// client because we want each peer to maintain its own local copy of the
 		// DHT, so that the bootstrapping node of the DHT can go down without
 		// inhibiting future peer discovery.
-		kad, err := dht.New(ctx, h)
+		kad, err := dht.New(ctx, h, d.dhtOptions...)
 		if err != nil {
 			return d.dht, err
 		}

+ 14 - 13
pkg/node/options.go

@@ -22,6 +22,7 @@ import (
 
 	"github.com/ipfs/go-log"
 	"github.com/libp2p/go-libp2p"
+	dht "github.com/libp2p/go-libp2p-kad-dht"
 	"github.com/mudler/edgevpn/pkg/blockchain"
 	discovery "github.com/mudler/edgevpn/pkg/discovery"
 	"github.com/mudler/edgevpn/pkg/protocol"
@@ -227,15 +228,15 @@ func (y YAMLConnectionConfig) YAML() string {
 	return string(bytesData)
 }
 
-func (y YAMLConnectionConfig) copy(mdns, dht bool, cfg *Config) {
-	d := &discovery.DHT{
-		RefreshDiscoveryTime: cfg.DiscoveryInterval,
-		OTPInterval:          y.OTP.DHT.Interval,
-		OTPKey:               y.OTP.DHT.Key,
-		KeyLength:            y.OTP.DHT.Length,
-		RendezvousString:     y.Rendezvous,
-		BootstrapPeers:       cfg.DiscoveryBootstrapPeers,
-	}
+func (y YAMLConnectionConfig) copy(mdns, dht bool, cfg *Config, opts ...dht.Option) {
+	d := discovery.NewDHT(opts...)
+	d.RefreshDiscoveryTime = cfg.DiscoveryInterval
+	d.OTPInterval = y.OTP.DHT.Interval
+	d.OTPKey = y.OTP.DHT.Key
+	d.KeyLength = y.OTP.DHT.Length
+	d.RendezvousString = y.Rendezvous
+	d.BootstrapPeers = cfg.DiscoveryBootstrapPeers
+
 	m := &discovery.MDNS{DiscoveryServiceTag: y.MDNS}
 	cfg.ExchangeKey = y.OTP.Crypto.Key
 	cfg.RoomName = y.RoomName
@@ -283,7 +284,7 @@ func GenerateNewConnectionData(i ...int) *YAMLConnectionConfig {
 	}
 }
 
-func FromYaml(enablemDNS, enableDHT bool, path string) func(cfg *Config) error {
+func FromYaml(enablemDNS, enableDHT bool, path string, d ...dht.Option) func(cfg *Config) error {
 	return func(cfg *Config) error {
 		if len(path) == 0 {
 			return nil
@@ -299,12 +300,12 @@ func FromYaml(enablemDNS, enableDHT bool, path string) func(cfg *Config) error {
 			return errors.Wrap(err, "parsing yaml")
 		}
 
-		t.copy(enablemDNS, enableDHT, cfg)
+		t.copy(enablemDNS, enableDHT, cfg, d...)
 		return nil
 	}
 }
 
-func FromBase64(enablemDNS, enableDHT bool, bb string) func(cfg *Config) error {
+func FromBase64(enablemDNS, enableDHT bool, bb string, d ...dht.Option) func(cfg *Config) error {
 	return func(cfg *Config) error {
 		if len(bb) == 0 {
 			return nil
@@ -318,7 +319,7 @@ func FromBase64(enablemDNS, enableDHT bool, bb string) func(cfg *Config) error {
 		if err := yaml.Unmarshal(configDec, &t); err != nil {
 			return errors.Wrap(err, "parsing yaml")
 		}
-		t.copy(enablemDNS, enableDHT, cfg)
+		t.copy(enablemDNS, enableDHT, cfg, d...)
 		return nil
 	}
 }