Browse Source

:gear: Speedup first connections by using expbackoff timers

Ettore Di Giacinto 3 years ago
parent
commit
a99fb07b14
5 changed files with 66 additions and 4 deletions
  1. 1 0
      go.mod
  2. 1 0
      go.sum
  3. 5 3
      pkg/blockchain/ledger.go
  4. 2 1
      pkg/services/dht_relay_discovery.go
  5. 57 0
      pkg/utils/ticker.go

+ 1 - 0
go.mod

@@ -6,6 +6,7 @@ require (
 	github.com/Masterminds/sprig/v3 v3.2.2
 	github.com/Masterminds/sprig/v3 v3.2.2
 	github.com/benbjohnson/clock v1.3.0
 	github.com/benbjohnson/clock v1.3.0
 	github.com/c-robinson/iplib v1.0.3
 	github.com/c-robinson/iplib v1.0.3
+	github.com/cenkalti/backoff v2.2.1+incompatible // indirect
 	github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
 	github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
 	github.com/google/btree v1.0.1 // indirect
 	github.com/google/btree v1.0.1 // indirect
 	github.com/gookit/color v1.5.0 // indirect
 	github.com/gookit/color v1.5.0 // indirect

+ 1 - 0
go.sum

@@ -116,6 +116,7 @@ github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7
 github.com/c-robinson/iplib v1.0.3 h1:NG0UF0GoEsrC1/vyfX1Lx2Ss7CySWl3KqqXh3q4DdPU=
 github.com/c-robinson/iplib v1.0.3 h1:NG0UF0GoEsrC1/vyfX1Lx2Ss7CySWl3KqqXh3q4DdPU=
 github.com/c-robinson/iplib v1.0.3/go.mod h1:i3LuuFL1hRT5gFpBRnEydzw8R6yhGkF4szNDIbF8pgo=
 github.com/c-robinson/iplib v1.0.3/go.mod h1:i3LuuFL1hRT5gFpBRnEydzw8R6yhGkF4szNDIbF8pgo=
 github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
 github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
+github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
 github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
 github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
 github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
 github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=

+ 5 - 3
pkg/blockchain/ledger.go

@@ -27,6 +27,7 @@ import (
 	"time"
 	"time"
 
 
 	"github.com/mudler/edgevpn/pkg/hub"
 	"github.com/mudler/edgevpn/pkg/hub"
+	"github.com/mudler/edgevpn/pkg/utils"
 
 
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 )
 )
@@ -66,7 +67,7 @@ func (l *Ledger) newGenesis() {
 // writes the blockchain to the  periodically
 // writes the blockchain to the  periodically
 func (l *Ledger) Syncronizer(ctx context.Context, t time.Duration) {
 func (l *Ledger) Syncronizer(ctx context.Context, t time.Duration) {
 	go func() {
 	go func() {
-		t := time.NewTicker(t)
+		t := utils.NewBackoffTicker(utils.BackoffMaxInterval(t))
 		defer t.Stop()
 		defer t.Stop()
 		for {
 		for {
 			select {
 			select {
@@ -139,9 +140,10 @@ func (l *Ledger) Update(f *Ledger, h *hub.Message, c chan *hub.Message) (err err
 // Sends a broadcast at the specified interval
 // Sends a broadcast at the specified interval
 // by making sure the async retrieved value is written to the
 // by making sure the async retrieved value is written to the
 // blockchain
 // blockchain
-func (l *Ledger) Announce(ctx context.Context, t time.Duration, async func()) {
+func (l *Ledger) Announce(ctx context.Context, d time.Duration, async func()) {
 	go func() {
 	go func() {
-		t := time.NewTicker(t)
+		//t := time.NewTicker(t)
+		t := utils.NewBackoffTicker(utils.BackoffMaxInterval(d))
 		defer t.Stop()
 		defer t.Stop()
 		for {
 		for {
 			select {
 			select {

+ 2 - 1
pkg/services/dht_relay_discovery.go

@@ -24,6 +24,7 @@ import (
 	"github.com/mudler/edgevpn/pkg/blockchain"
 	"github.com/mudler/edgevpn/pkg/blockchain"
 	"github.com/mudler/edgevpn/pkg/discovery"
 	"github.com/mudler/edgevpn/pkg/discovery"
 	"github.com/mudler/edgevpn/pkg/node"
 	"github.com/mudler/edgevpn/pkg/node"
+	"github.com/mudler/edgevpn/pkg/utils"
 )
 )
 
 
 // AutoRelayFeederService is a service responsible to returning periodically peers to
 // AutoRelayFeederService is a service responsible to returning periodically peers to
@@ -33,7 +34,7 @@ func AutoRelayFeederService(ll log.StandardLogger, peerChan chan peer.AddrInfo,
 		ll.Debug("[relay discovery] Service starts")
 		ll.Debug("[relay discovery] Service starts")
 		ctx, cancel := context.WithCancel(ctx)
 		ctx, cancel := context.WithCancel(ctx)
 		go func() {
 		go func() {
-			t := time.NewTicker(duration)
+			t := utils.NewBackoffTicker(utils.BackoffMaxInterval(duration))
 			defer t.Stop()
 			defer t.Stop()
 			defer cancel()
 			defer cancel()
 			for {
 			for {

+ 57 - 0
pkg/utils/ticker.go

@@ -0,0 +1,57 @@
+package utils
+
+import (
+	"time"
+
+	"github.com/cenkalti/backoff"
+)
+
+type expBackoffOpt func(e *backoff.ExponentialBackOff)
+
+func BackoffInitialInterval(i time.Duration) expBackoffOpt {
+	return func(e *backoff.ExponentialBackOff) {
+		e.InitialInterval = i
+	}
+}
+func BackoffRandomizationFactor(i float64) expBackoffOpt {
+	return func(e *backoff.ExponentialBackOff) {
+		e.RandomizationFactor = i
+	}
+}
+func BackoffMultiplier(i float64) expBackoffOpt {
+	return func(e *backoff.ExponentialBackOff) {
+		e.Multiplier = i
+	}
+}
+
+func BackoffMaxInterval(i time.Duration) expBackoffOpt {
+	return func(e *backoff.ExponentialBackOff) {
+		e.MaxInterval = i
+	}
+}
+
+func BackoffMaxElapsedTime(i time.Duration) expBackoffOpt {
+	return func(e *backoff.ExponentialBackOff) {
+		e.MaxElapsedTime = i
+	}
+}
+
+func newExpBackoff(o ...expBackoffOpt) backoff.BackOff {
+	b := &backoff.ExponentialBackOff{
+		InitialInterval:     5 * time.Second,
+		RandomizationFactor: 0.5,
+		Multiplier:          2,
+		MaxInterval:         2 * time.Minute,
+		MaxElapsedTime:      0,
+		Clock:               backoff.SystemClock,
+	}
+	for _, opt := range o {
+		opt(b)
+	}
+	b.Reset()
+	return b
+}
+
+func NewBackoffTicker(o ...expBackoffOpt) *backoff.Ticker {
+	return backoff.NewTicker(newExpBackoff(o...))
+}