mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-24 16:41:30 +02:00
On network changes the client restarted the whole engine. That is heavy-handed and slow: it tears down working state to recover from a transition the engine could handle itself. This replaces the restart with proper network event handling. Suspend the retry loops while no network is available. Instead of burning through backoff intervals against an unreachable network, the reconnection loops park until the OS reports a usable network again. Reconnect immediately on a network switch. When the OS hands us a new network, connections bound to the old one are swept and re-dialed right away, rather than waiting for a timeout to notice they are dead.
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()
|
|
}
|
|
}
|
|
}
|