From d14bfc342d16d57e79a5ca8383067095b95631e9 Mon Sep 17 00:00:00 2001
From: David Stainton <dstainton@elixxir.io>
Date: Fri, 26 Aug 2022 18:07:44 -0400
Subject: [PATCH] ud: exponential backoff for channel id registration

---
 ud/channelIDTracking.go | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/ud/channelIDTracking.go b/ud/channelIDTracking.go
index e4b8b024f..79702703f 100644
--- a/ud/channelIDTracking.go
+++ b/ud/channelIDTracking.go
@@ -4,6 +4,7 @@ import (
 	"crypto/ed25519"
 	"encoding/json"
 	"errors"
+	"math"
 	"sync"
 	"time"
 
@@ -234,11 +235,20 @@ func (c *clientIDTracker) Start() (stoppable.Stoppable, error) {
 // registrationWorker is meant to run in it's own goroutine
 // periodically registering, getting a new lease.
 func (c *clientIDTracker) registrationWorker(stopper *stoppable.Single) {
+	// start backoff at 32 seconds
+	base := 2
+	exponent := 5
+	waitTime := time.Second
+	maxBackoff := 300
 	for {
 		if time.Now().After(c.registrationDisk.GetLease().Add(-graceDuration)) {
 			err := c.register()
 			if err != nil {
-				jww.FATAL.Panic(err)
+				backoffSeconds := int(math.Pow(float64(base), float64(exponent)))
+				if backoffSeconds > maxBackoff {
+					backoffSeconds = maxBackoff
+				}
+				waitTime = time.Second * time.Duration(backoffSeconds)
 			}
 		}
 
@@ -252,7 +262,7 @@ func (c *clientIDTracker) registrationWorker(stopper *stoppable.Single) {
 		select {
 		case <-stopper.Quit():
 			return
-		case <-time.After(time.Second):
+		case <-time.After(waitTime):
 		}
 	}
 }
-- 
GitLab