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

+26
View File
@@ -0,0 +1,26 @@
package common
import (
"math/rand/v2"
"time"
)
const backoffJitterFloorDivisor = 4
func BackoffWithJitter(attempt int, initialDelay, maxDelay time.Duration) time.Duration {
if initialDelay <= 0 {
return 0
}
if maxDelay < initialDelay {
maxDelay = initialDelay
}
if attempt < 0 {
attempt = 0
}
ceiling := maxDelay
if shifted := initialDelay << uint(attempt); shifted > 0 && shifted < maxDelay {
ceiling = shifted
}
floor := ceiling / backoffJitterFloorDivisor
return floor + time.Duration(rand.Int64N(int64(ceiling-floor)+1))
}
@@ -16,11 +16,6 @@ import (
"whitelist-bypass/relay/tunnel"
)
const (
dionReconnectInitialDelay = time.Second
dionReconnectMaxDelay = 16 * time.Second
)
type DionHeadlessJoiner struct {
logFn func(string, ...any)
OnConnected func(tunnel.DataTunnel)
@@ -165,19 +160,7 @@ func (j *DionHeadlessJoiner) runOnce(httpClient *http.Client, slug, displayName
}
func (j *DionHeadlessJoiner) waitBeforeRetry(attempt int) bool {
delay := dionReconnectInitialDelay << attempt
if delay > dionReconnectMaxDelay || delay <= 0 {
delay = dionReconnectMaxDelay
}
j.logFn("dion-joiner: waiting %s before reconnect", delay)
timer := time.NewTimer(delay)
defer timer.Stop()
select {
case <-timer.C:
return !j.isClosed()
case <-j.stopCh:
return false
}
return waitReconnectBackoff(attempt, j.logFn, "dion-joiner", j.stopCh, j.isClosed)
}
func (j *DionHeadlessJoiner) isClosed() bool {
@@ -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
}
}
@@ -22,11 +22,9 @@ import (
)
const (
TmAPIBase = tmapi.APIBase
TmOrigin = tmapi.Origin
TmPingPeriod = 5 * time.Second
telemostReconnectInitialDelay = time.Second
telemostReconnectMaxDelay = 16 * time.Second
TmAPIBase = tmapi.APIBase
TmOrigin = tmapi.Origin
TmPingPeriod = 5 * time.Second
)
type TelemostHeadlessJoiner struct {
@@ -169,19 +167,7 @@ func (j *TelemostHeadlessJoiner) runOnce() error {
}
func (j *TelemostHeadlessJoiner) waitBeforeRetry(attempt int) bool {
delay := telemostReconnectInitialDelay << attempt
if delay > telemostReconnectMaxDelay || delay <= 0 {
delay = telemostReconnectMaxDelay
}
j.logFn("telemost-joiner: waiting %s before reconnect", delay)
timer := time.NewTimer(delay)
defer timer.Stop()
select {
case <-timer.C:
return !j.isClosed()
case <-j.stopCh:
return false
}
return waitReconnectBackoff(attempt, j.logFn, "telemost-joiner", j.stopCh, j.isClosed)
}
func (j *TelemostHeadlessJoiner) resetSessionState() {
+2 -18
View File
@@ -21,11 +21,7 @@ import (
"whitelist-bypass/relay/wtsignal"
)
const (
vkReconnectInitialDelay = time.Second
vkReconnectMaxDelay = 16 * time.Second
vkMaxReconnectAttempts = 10
)
const vkMaxReconnectAttempts = 10
const vkTopologyDirect = "DIRECT"
@@ -192,19 +188,7 @@ func (h *VKHeadlessJoiner) runOnce() error {
}
func (h *VKHeadlessJoiner) waitBeforeRetry(attempt int) bool {
delay := vkReconnectInitialDelay << attempt
if delay > vkReconnectMaxDelay || delay <= 0 {
delay = vkReconnectMaxDelay
}
h.logFn("headless: waiting %s before reconnect", delay)
timer := time.NewTimer(delay)
defer timer.Stop()
select {
case <-timer.C:
return !h.isClosed()
case <-h.stopCh:
return false
}
return waitReconnectBackoff(attempt, h.logFn, "headless", h.stopCh, h.isClosed)
}
func (h *VKHeadlessJoiner) isClosed() bool {
@@ -16,11 +16,6 @@ import (
"whitelist-bypass/relay/wbstream"
)
const (
reconnectInitialDelay = time.Second
reconnectMaxDelay = 16 * time.Second
)
type WBStreamHeadlessJoiner struct {
logFn func(string, ...any)
OnConnected func(tunnel.DataTunnel)
@@ -179,19 +174,7 @@ func (j *WBStreamHeadlessJoiner) MarkConfigAcked() {
}
func (j *WBStreamHeadlessJoiner) waitBeforeRetry(attempt int) bool {
delay := reconnectInitialDelay << attempt
if delay > reconnectMaxDelay || delay <= 0 {
delay = reconnectMaxDelay
}
j.logFn("wbstream-joiner: waiting %s before reconnect", delay)
timer := time.NewTimer(delay)
defer timer.Stop()
select {
case <-timer.C:
return !j.isClosed()
case <-j.stopCh:
return false
}
return waitReconnectBackoff(attempt, j.logFn, "wbstream-joiner", j.stopCh, j.isClosed)
}
func (j *WBStreamHeadlessJoiner) clearSession(sess *wbstream.Session) {