add randomness to reconnections
This commit is contained in:
1 parent
ff21aad59e
commit
c36c561e35
6 files changed
+59
-72
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package joiner
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"whitelist-bypass/relay/common"
|
||||
)
|
||||
|
||||
const (
|
||||
reconnectInitialDelay = time.Second
|
||||
reconnectMaxDelay = 16 * time.Second
|
||||
)
|
||||
|
||||
func waitReconnectBackoff(attempt int, logFn func(string, ...any), logPrefix string, stopCh <-chan struct{}, isClosed func() bool) bool {
|
||||
delay := common.BackoffWithJitter(attempt, reconnectInitialDelay, reconnectMaxDelay)
|
||||
logFn("%s: waiting %s before reconnect", logPrefix, delay)
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-timer.C:
|
||||
return !isClosed()
|
||||
case <-stopCh:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user