Browse Source

:gear: Use backoff ticker for dht discovery

Ettore Di Giacinto 3 years ago
parent
commit
7abadf677a
3 changed files with 7 additions and 10 deletions
  1. 1 1
      cmd/util.go
  2. 5 8
      pkg/discovery/dht.go
  3. 1 1
      pkg/node/node.go

+ 1 - 1
cmd/util.go

@@ -70,7 +70,7 @@ var CommonFlags []cli.Flag = []cli.Flag{
 		Name:   "discovery-interval",
 		Usage:  "DHT discovery interval time",
 		EnvVar: "EDGEVPNDHTINTERVAL",
-		Value:  120,
+		Value:  720,
 	},
 	&cli.IntFlag{
 		Name:   "ledger-announce-interval",

+ 5 - 8
pkg/discovery/dht.go

@@ -21,6 +21,7 @@ import (
 	"time"
 
 	internalCrypto "github.com/mudler/edgevpn/pkg/crypto"
+	"github.com/mudler/edgevpn/pkg/utils"
 
 	"github.com/ipfs/go-log"
 	"github.com/libp2p/go-libp2p"
@@ -119,18 +120,14 @@ func (d *DHT) Run(c log.StandardLogger, ctx context.Context, host host.Host) err
 
 	go func() {
 		connect()
+		t := utils.NewBackoffTicker(utils.BackoffMaxInterval(d.RefreshDiscoveryTime))
+		defer t.Stop()
 		for {
-			// We don't want a ticker here but a timer
-			// this is less "talkative" as a DHT connect() can take up
-			// long time and can exceed d.RefreshdiscoveryTime.
-			// In this way we ensure we wait at least timeout to fire a connect()
-			timer := time.NewTimer(d.RefreshDiscoveryTime)
 			select {
+			case <-t.C:
+				connect()
 			case <-ctx.Done():
-				timer.Stop()
 				return
-			case <-timer.C:
-				connect()
 			}
 		}
 	}()

+ 1 - 1
pkg/node/node.go

@@ -60,7 +60,7 @@ var defaultLibp2pOptions = []libp2p.Option{
 
 func New(p ...Option) (*Node, error) {
 	c := &Config{
-		DiscoveryInterval:        120 * time.Second,
+		DiscoveryInterval:        5 * time.Minute,
 		StreamHandlers:           make(map[protocol.Protocol]StreamHandler),
 		LedgerAnnounceTime:       5 * time.Second,
 		LedgerSyncronizationTime: 5 * time.Second,