add randomness to reconnections

This commit is contained in:
kulikov0 committed 2026-07-26 12:06:11 +02:00
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
}
}