mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-28 02:21:30 +02:00
Three pieces, each verified on Android across WiFi/cellular switches: - A sweep-aware backoff wrapper: the first retry after a disconnect that follows a recent network-change mark comes after 200ms instead of the randomized [0..1.6s] interval. Any other failure keeps the unchanged spread, so the clients of a restarted server still scatter their reconnects. - The retry sleep wakes on OS network availability transitions (nbgrpc.Retry): a disconnect that precedes the offline flag by a few milliseconds no longer sleeps blindly through the recovery - the loop parks on the netstate gate and resumes the moment the network returns. - The connection state is re-checked after WaitForStateChange: a dial settling in Ready proceeds immediately instead of burning another backoff round on an already-usable channel. Measured after a network switch: management and signal recover in 270-470ms deterministically, down from a 312-1593ms lottery.
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package netsweep
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestQuickRetryAfterRecentMark(t *testing.T) {
|
|
sweeper := New()
|
|
sweeper.MarkNetworkChange()
|
|
|
|
slow := backoff.NewConstantBackOff(5 * time.Second)
|
|
bo := sweeper.QuickRetryBackoff(context.Background(), slow, nil)
|
|
|
|
assert.Equal(t, quickRetryDelay, bo.NextBackOff(), "first retry after a mark must be quick")
|
|
assert.Equal(t, 5*time.Second, bo.NextBackOff(), "second retry must fall back to the wrapped backoff")
|
|
|
|
bo.Reset()
|
|
assert.Equal(t, quickRetryDelay, bo.NextBackOff(), "reset must re-arm the quick retry")
|
|
}
|
|
|
|
func TestQuickRetryWithoutMarkKeepsSpread(t *testing.T) {
|
|
sweeper := New()
|
|
|
|
slow := backoff.NewConstantBackOff(5 * time.Second)
|
|
bo := sweeper.QuickRetryBackoff(context.Background(), slow, nil)
|
|
|
|
assert.Equal(t, 5*time.Second, bo.NextBackOff(), "without a mark the wrapped backoff decides")
|
|
|
|
sweeper.mu.Lock()
|
|
sweeper.lastMark = time.Now().Add(-recentMarkWindow)
|
|
sweeper.mu.Unlock()
|
|
assert.Equal(t, 5*time.Second, bo.NextBackOff(), "a stale mark must not trigger the quick retry")
|
|
}
|
|
|
|
func TestQuickRetryNilSweeperPassthrough(t *testing.T) {
|
|
var sweeper *Sweeper
|
|
|
|
slow := backoff.NewConstantBackOff(5 * time.Second)
|
|
bo := sweeper.QuickRetryBackoff(context.Background(), slow, nil)
|
|
|
|
assert.Equal(t, backoff.BackOff(slow), bo, "nil sweeper must return the backoff unchanged")
|
|
}
|
|
|
|
func TestQuickRetryHonorsContext(t *testing.T) {
|
|
sweeper := New()
|
|
sweeper.MarkNetworkChange()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
bo := sweeper.QuickRetryBackoff(ctx, backoff.NewConstantBackOff(time.Millisecond), nil)
|
|
|
|
assert.Equal(t, backoff.Stop, bo.NextBackOff(), "cancelled context must stop the retry loop")
|
|
}
|