浏览代码

:wrench: Expose interval from CLI

Ettore Di Giacinto 3 年之前
父节点
当前提交
6e14df6c2e
共有 6 个文件被更改,包括 112 次插入78 次删除
  1. 1 41
      cmd/main.go
  2. 58 0
      cmd/util.go
  3. 3 4
      pkg/discovery/dht.go
  4. 1 1
      pkg/edgevpn/config.go
  5. 3 1
      pkg/edgevpn/edgevpn.go
  6. 46 31
      pkg/edgevpn/options.go

+ 1 - 41
cmd/main.go

@@ -16,41 +16,6 @@ This program comes with ABSOLUTELY NO WARRANTY.
 This is free software, and you are welcome to redistribute it
 under certain conditions.`
 
-var CommonFlags []cli.Flag = []cli.Flag{
-	&cli.StringFlag{
-		Name:   "config",
-		Usage:  "Specify a path to a edgevpn config file",
-		EnvVar: "EDGEVPNCONFIG",
-	},
-	&cli.IntFlag{
-		Name:   "mtu",
-		Usage:  "Specify a mtu",
-		EnvVar: "EDGEVPNMTU",
-		Value:  1200,
-	},
-	&cli.StringFlag{
-		Name:   "ledger-state",
-		Usage:  "Specify a ledger state directory",
-		EnvVar: "EDGEVPNLEDGERSTATE",
-	},
-	&cli.StringFlag{
-		Name:   "log-level",
-		Usage:  "Specify loglevel",
-		EnvVar: "EDGEVPNLOGLEVEL",
-		Value:  "info",
-	},
-	&cli.StringFlag{
-		Name:   "libp2p-log-level",
-		Usage:  "Specify libp2p loglevel",
-		EnvVar: "EDGEVPNLIBP2PLOGLEVEL",
-		Value:  "fatal",
-	},
-	&cli.StringFlag{
-		Name:   "token",
-		Usage:  "Specify an edgevpn token in place of a config file",
-		EnvVar: "EDGEVPNTOKEN",
-	}}
-
 func MainFlags() []cli.Flag {
 	return append([]cli.Flag{
 		&cli.BoolFlag{
@@ -88,12 +53,7 @@ func Main() func(c *cli.Context) error {
 	return func(c *cli.Context) error {
 		if c.Bool("g") {
 			// Generates a new config and exit
-			newData, err := edgevpn.GenerateNewConnectionData()
-			if err != nil {
-				fmt.Println(err)
-				os.Exit(1)
-			}
-
+			newData := edgevpn.GenerateNewConnectionData()
 			bytesData, err := yaml.Marshal(newData)
 			if err != nil {
 				fmt.Println(err)

+ 58 - 0
cmd/util.go

@@ -1,6 +1,8 @@
 package cmd
 
 import (
+	"time"
+
 	"github.com/ipfs/go-log"
 	"github.com/mudler/edgevpn/internal"
 	"github.com/mudler/edgevpn/pkg/blockchain"
@@ -11,6 +13,59 @@ import (
 	"github.com/urfave/cli"
 )
 
+var CommonFlags []cli.Flag = []cli.Flag{
+	&cli.StringFlag{
+		Name:   "config",
+		Usage:  "Specify a path to a edgevpn config file",
+		EnvVar: "EDGEVPNCONFIG",
+	},
+	&cli.IntFlag{
+		Name:   "mtu",
+		Usage:  "Specify a mtu",
+		EnvVar: "EDGEVPNMTU",
+		Value:  1200,
+	},
+	&cli.IntFlag{
+		Name:   "discovery-interval",
+		Usage:  "DHT discovery interval time",
+		EnvVar: "EDGEVPNDHTINTERVAL",
+		Value:  120,
+	},
+	&cli.IntFlag{
+		Name:   "ledger-announce-interval",
+		Usage:  "Ledger announce interval time",
+		EnvVar: "EDGEVPNLEDGERINTERVAL",
+		Value:  10,
+	},
+	&cli.IntFlag{
+		Name:   "ledger-syncronization-interval",
+		Usage:  "Ledger syncronization interval time",
+		EnvVar: "EDGEVPNLEDGERSYNCINTERVAL",
+		Value:  10,
+	},
+	&cli.StringFlag{
+		Name:   "ledger-state",
+		Usage:  "Specify a ledger state directory",
+		EnvVar: "EDGEVPNLEDGERSTATE",
+	},
+	&cli.StringFlag{
+		Name:   "log-level",
+		Usage:  "Specify loglevel",
+		EnvVar: "EDGEVPNLOGLEVEL",
+		Value:  "info",
+	},
+	&cli.StringFlag{
+		Name:   "libp2p-log-level",
+		Usage:  "Specify libp2p loglevel",
+		EnvVar: "EDGEVPNLIBP2PLOGLEVEL",
+		Value:  "fatal",
+	},
+	&cli.StringFlag{
+		Name:   "token",
+		Usage:  "Specify an edgevpn token in place of a config file",
+		EnvVar: "EDGEVPNTOKEN",
+	}}
+
 func displayStart(e *edgevpn.EdgeVPN) {
 	e.Logger().Info(Copyright)
 
@@ -45,6 +100,9 @@ func cliToOpts(c *cli.Context) []edgevpn.Option {
 	}
 
 	opts := []edgevpn.Option{
+		edgevpn.WithDiscoveryInterval(time.Duration(c.Int("discovery-interval")) * time.Second),
+		edgevpn.WithLedgerAnnounceTime(time.Duration(c.Int("ledger-announce-interval")) * time.Second),
+		edgevpn.WithLedgerInterval(time.Duration(c.Int("ledger-syncronization-interval")) * time.Second),
 		edgevpn.Logger(llger),
 		edgevpn.LibP2PLogLevel(libp2plvl),
 		edgevpn.WithInterfaceMTU(c.Int("mtu")),

+ 3 - 4
pkg/discovery/dht.go

@@ -25,7 +25,7 @@ type DHT struct {
 	BootstrapPeers       AddrList
 	latestRendezvous     string
 	console              log.StandardLogger
-	RefreshDiscoveryTime int64
+	RefreshDiscoveryTime time.Duration
 	dht                  *dht.IpfsDHT
 }
 
@@ -83,7 +83,7 @@ func (d *DHT) Run(c log.StandardLogger, ctx context.Context, host host.Host) err
 
 	// Bootstrap the DHT. In the default configuration, this spawns a Background
 	// thread that will refresh the peer table every five minutes.
-	c.Info("Bootstrapping the DHT")
+	c.Info("Bootstrapping DHT")
 	if err = kademliaDHT.Bootstrap(ctx); err != nil {
 		return err
 	}
@@ -100,9 +100,8 @@ func (d *DHT) Run(c log.StandardLogger, ctx context.Context, host host.Host) err
 
 	go func() {
 		connect()
-
 		t := jitterbug.New(
-			time.Second*time.Duration(d.RefreshDiscoveryTime),
+			d.RefreshDiscoveryTime,
 			&jitterbug.Norm{Stdev: time.Second * 10},
 		)
 		defer t.Stop()

+ 1 - 1
pkg/edgevpn/config.go

@@ -53,7 +53,7 @@ type Config struct {
 	StreamHandlers             map[protocol.ID]StreamHandler
 	AdditionalOptions, Options []libp2p.Option
 
-	LedgerSyncronizationTime, LedgerAnnounceTime time.Duration
+	DiscoveryInterval, LedgerSyncronizationTime, LedgerAnnounceTime time.Duration
 }
 
 type StreamHandler func(stream network.Stream)

+ 3 - 1
pkg/edgevpn/edgevpn.go

@@ -38,6 +38,7 @@ type EdgeVPN struct {
 
 func New(p ...Option) *EdgeVPN {
 	c := Config{
+		DiscoveryInterval:        120 * time.Second,
 		StreamHandlers:           make(map[protocol.ID]StreamHandler),
 		LedgerAnnounceTime:       5 * time.Second,
 		LedgerSyncronizationTime: 5 * time.Second,
@@ -135,6 +136,7 @@ func (e *EdgeVPN) Start() error {
 	if err != nil {
 		return err
 	}
+
 	ledger.Announce(
 		context.Background(),
 		e.config.LedgerAnnounceTime,
@@ -165,7 +167,7 @@ func (e *EdgeVPN) Start() error {
 
 // end signals the event loop to exit gracefully
 func (e *EdgeVPN) Stop() {
-	e.doneCh <- struct{}{}
+	close(e.doneCh)
 }
 
 // MessageWriter returns a new MessageWriter bound to the edgevpn instance

+ 46 - 31
pkg/edgevpn/options.go

@@ -3,6 +3,7 @@ package edgevpn
 import (
 	"encoding/base64"
 	"io/ioutil"
+	"time"
 
 	"github.com/ipfs/go-log"
 	"github.com/libp2p/go-libp2p"
@@ -170,15 +171,6 @@ func SealKeyLength(i int) func(cfg *Config) error {
 func LibP2PLogLevel(l log.LogLevel) func(cfg *Config) error {
 	return func(cfg *Config) error {
 		log.SetAllLoggers(l)
-		//log.SetLogLevel("edgevpn", zapcore.Level(l).String())
-		// log.SetLogLevel("pubsub", "fatal")
-		// log.SetLogLevel("dht/RtRefreshManager", "fatal")
-		// log.SetLogLevel("swarm2", "fatal")
-		// log.SetLogLevel("basichost", "fatal")
-		// log.SetLogLevel("relay", "fatal")
-		// log.SetLogLevel("dht", "fatal")
-		// log.SetLogLevel("mdns", "fatal")
-		// log.SetLogLevel("net/identify", "fatal")
 		return nil
 	}
 }
@@ -190,6 +182,27 @@ func MaxMessageSize(i int) func(cfg *Config) error {
 	}
 }
 
+func WithLedgerAnnounceTime(t time.Duration) func(cfg *Config) error {
+	return func(cfg *Config) error {
+		cfg.LedgerAnnounceTime = t
+		return nil
+	}
+}
+
+func WithLedgerInterval(t time.Duration) func(cfg *Config) error {
+	return func(cfg *Config) error {
+		cfg.LedgerSyncronizationTime = t
+		return nil
+	}
+}
+
+func WithDiscoveryInterval(t time.Duration) func(cfg *Config) error {
+	return func(cfg *Config) error {
+		cfg.DiscoveryInterval = t
+		return nil
+	}
+}
+
 type OTPConfig struct {
 	Interval int    `yaml:"interval"`
 	Key      string `yaml:"key"`
@@ -204,16 +217,15 @@ type OTP struct {
 type YAMLConnectionConfig struct {
 	OTP OTP `yaml:"otp"`
 
-	RoomName            string `yaml:"room"`
-	Rendezvous          string `yaml:"rendezvous"`
-	MDNS                string `yaml:"mdns"`
-	MaxBlockChainLength int    `yaml:"max_blockchain_length"`
-	MaxMessageSize      int    `yaml:"max_message_size"`
+	RoomName       string `yaml:"room"`
+	Rendezvous     string `yaml:"rendezvous"`
+	MDNS           string `yaml:"mdns"`
+	MaxMessageSize int    `yaml:"max_message_size"`
 }
 
 func (y YAMLConnectionConfig) copy(cfg *Config) {
 	d := &discovery.DHT{
-		RefreshDiscoveryTime: 60,
+		RefreshDiscoveryTime: cfg.DiscoveryInterval,
 		OTPInterval:          y.OTP.DHT.Interval,
 		OTPKey:               y.OTP.DHT.Key,
 		KeyLength:            y.OTP.DHT.Length,
@@ -228,22 +240,25 @@ func (y YAMLConnectionConfig) copy(cfg *Config) {
 	cfg.MaxMessageSize = y.MaxMessageSize
 }
 
-func GenerateNewConnectionData() (*YAMLConnectionConfig, error) {
-	config := YAMLConnectionConfig{}
-
-	config.RoomName = utils.RandStringRunes(23)
-	config.Rendezvous = utils.RandStringRunes(23)
-	config.MDNS = utils.RandStringRunes(23)
-
-	config.OTP.DHT.Key = gotp.RandomSecret(16)
-	config.OTP.Crypto.Key = gotp.RandomSecret(16)
-	config.OTP.DHT.Interval = 9000
-	config.OTP.Crypto.Interval = 9000
-	config.OTP.Crypto.Length = 12
-	config.OTP.DHT.Length = 12
-	config.MaxMessageSize = 20 << 20 // 20MB
-
-	return &config, nil
+func GenerateNewConnectionData() *YAMLConnectionConfig {
+	return &YAMLConnectionConfig{
+		MaxMessageSize: 20 << 20, // 20MB
+		RoomName:       utils.RandStringRunes(23),
+		Rendezvous:     utils.RandStringRunes(23),
+		MDNS:           utils.RandStringRunes(23),
+		OTP: OTP{
+			DHT: OTPConfig{
+				Key:      gotp.RandomSecret(16),
+				Interval: 9000,
+				Length:   12,
+			},
+			Crypto: OTPConfig{
+				Key:      gotp.RandomSecret(16),
+				Interval: 9000,
+				Length:   12,
+			},
+		},
+	}
 }
 
 func FromYaml(path string) func(cfg *Config) error {