mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-28 10:31:29 +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.
50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
|
|
"github.com/netbirdio/netbird/client/netstate"
|
|
)
|
|
|
|
// Retry mirrors backoff.Retry, but the sleep between attempts also wakes on
|
|
// OS network availability transitions: an operation cut down by a network
|
|
// change retries the moment the network settles instead of sleeping through
|
|
// the recovery. A nil netState never fires, leaving plain backoff.Retry
|
|
// behavior.
|
|
func Retry(ctx context.Context, operation backoff.Operation, bo backoff.BackOff, netState *netstate.State) error {
|
|
bo.Reset()
|
|
for {
|
|
err := operation()
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
var permanent *backoff.PermanentError
|
|
if errors.As(err, &permanent) {
|
|
return permanent.Err
|
|
}
|
|
|
|
next := bo.NextBackOff()
|
|
if next == backoff.Stop {
|
|
if cerr := ctx.Err(); cerr != nil {
|
|
return cerr
|
|
}
|
|
return err
|
|
}
|
|
|
|
timer := time.NewTimer(next)
|
|
select {
|
|
case <-timer.C:
|
|
case <-netState.Changed():
|
|
timer.Stop()
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
}
|