[client] Name the netevents fields after their types

Review feedback on #7254: the fields kept their netstate-era names after the
type changed to *netevents.Manager or a NetworkWatcher interface.

Rename the Manager-typed fields to netMgr/NetMgr and the interface-typed guard
fields to netWatcher, document that the Manager write methods panic on a nil
receiver while the read methods stay nil-safe, and fix the waiteBeforeRetry
typo.

The relay client keeps its netEvents fields: those hold the NetEvents
interface, so the name already matches the type.
This commit is contained in:
Zoltán Papp
2026-08-25 15:09:00 +02:00
parent 1f1bccb674
commit 0a03211903
8 changed files with 64 additions and 57 deletions
+7 -7
View File
@@ -63,9 +63,9 @@ type GrpcClient struct {
connStateCallbackLock sync.RWMutex
serverURL string
// netEvents gates the stream retry loop on OS-reported network
// netMgr gates the stream retry loop on OS-reported network
// availability and sweeps the transport on network change.
netEvents *netevents.Manager
netMgr *netevents.Manager
// syncStreamErr holds the last Sync stream error, or nil while the stream
// is established and healthy. GetServerKey succeeds even when the peer
@@ -121,7 +121,7 @@ type Option func(*GrpcClient)
// WithNetEvents injects the OS network event handling.
func WithNetEvents(events *netevents.Manager) Option {
return func(c *GrpcClient) { c.netEvents = events }
return func(c *GrpcClient) { c.netMgr = events }
}
// NewClient creates a new client to Management service
@@ -142,7 +142,7 @@ func NewClient(ctx context.Context, addr string, ourPrivateKey wgtypes.Key, tlsE
extraOpts = append(extraOpts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxSize)))
log.Infof("management gRPC max receive message size set to %d bytes", maxSize)
}
extraOpts = append(extraOpts, nbgrpc.WithSweeper(c.netEvents))
extraOpts = append(extraOpts, nbgrpc.WithSweeper(c.netMgr))
var conn *grpc.ClientConn
operation := func() error {
@@ -223,12 +223,12 @@ func (c *GrpcClient) withMgmtStream(
ctx context.Context,
handler func(ctx context.Context, serverPubKey wgtypes.Key, backOff backoff.BackOff) error,
) error {
backOff := c.netEvents.QuickRetryBackoff(ctx, defaultBackoff(ctx))
backOff := c.netMgr.QuickRetryBackoff(ctx, defaultBackoff(ctx))
operation := func() error {
// suspend reconnect attempts while the OS reports no usable network.
// Wait only errors on a cancelled context, which means shutdown, so
// stop the loop without reporting a failure.
if waited, err := c.netEvents.Wait(ctx); err != nil {
if waited, err := c.netMgr.Wait(ctx); err != nil {
log.Debugf("management connection context has been canceled while offline, this usually indicates shutdown")
return nil //nolint:nilerr // a cancelled context means shutdown, not a retryable failure
} else if waited {
@@ -264,7 +264,7 @@ func (c *GrpcClient) withMgmtStream(
return handler(ctx, *serverPubKey, backOff)
}
err := nbgrpc.Retry(ctx, operation, backOff, c.netEvents)
err := nbgrpc.Retry(ctx, operation, backOff, c.netMgr)
if err != nil {
log.Warnf("exiting the Management service connection retry loop due to the unrecoverable error: %s", err)
}
+11 -11
View File
@@ -40,8 +40,8 @@ type Guard struct {
// attempts.
maxBackoffInterval time.Duration
// netState gates reconnect attempts on OS-reported network availability.
netState NetworkWatcher
// netWatcher gates reconnect attempts on OS-reported network availability.
netWatcher NetworkWatcher
// lastErr is the error from the most recent failed reconnect attempt,
// surfaced as the home relay status while disconnected.
@@ -50,7 +50,7 @@ type Guard struct {
// NewGuard creates a new guard for the relay client. A non-positive
// maxBackoffInterval falls back to defaultMaxBackoffInterval.
func NewGuard(sp *ServerPicker, maxBackoffInterval time.Duration, netState NetworkWatcher) *Guard {
func NewGuard(sp *ServerPicker, maxBackoffInterval time.Duration, netWatcher NetworkWatcher) *Guard {
if maxBackoffInterval <= 0 {
maxBackoffInterval = defaultMaxBackoffInterval
}
@@ -59,7 +59,7 @@ func NewGuard(sp *ServerPicker, maxBackoffInterval time.Duration, netState Netwo
OnReconnected: make(chan struct{}, 1),
serverPicker: sp,
maxBackoffInterval: maxBackoffInterval,
netState: netState,
netWatcher: netWatcher,
}
return g
}
@@ -100,8 +100,8 @@ func (g *Guard) StartReconnectTrys(ctx context.Context, relayClient *Client) {
select {
case <-ticker.C:
// suspend reconnect attempts while the OS reports no usable network
if g.netState != nil {
if waited, err := g.netState.Wait(ctx); err != nil {
if g.netWatcher != nil {
if waited, err := g.netWatcher.Wait(ctx); err != nil {
return
} else if waited {
ticker.Stop()
@@ -134,16 +134,16 @@ func (g *Guard) tryToQuickReconnect(parentCtx context.Context, rc *Client) bool
return false
}
if g.netState != nil {
if ok := g.netState.WaitSettled(parentCtx, quickReconnectBudget, verdictSettleWindow); !ok {
if g.netWatcher != nil {
if ok := g.netWatcher.WaitSettled(parentCtx, quickReconnectBudget, verdictSettleWindow); !ok {
return false
}
// Still offline after the budget: leave the retry to the ticker.
if !g.netState.IsOnline() {
if !g.netWatcher.IsOnline() {
return false
}
} else {
if cancelled := waiteBeforeRetry(parentCtx); !cancelled {
if cancelled := waitBeforeRetry(parentCtx); !cancelled {
return false
}
}
@@ -210,7 +210,7 @@ func (g *Guard) exponentTicker(ctx context.Context) *backoff.Ticker {
return backoff.NewTicker(bo)
}
func waiteBeforeRetry(ctx context.Context) bool {
func waitBeforeRetry(ctx context.Context) bool {
timer := time.NewTimer(quickReconnectBudget)
defer timer.Stop()
+7 -7
View File
@@ -66,9 +66,9 @@ type GrpcClient struct {
connStateCallback ConnStateNotifier
connStateCallbackLock sync.RWMutex
// netEvents gates the Receive retry loop on OS-reported network
// netMgr gates the Receive retry loop on OS-reported network
// availability and sweeps the transport on network change.
netEvents *netevents.Manager
netMgr *netevents.Manager
onReconnectedListenerFn func()
@@ -98,7 +98,7 @@ type Option func(*GrpcClient)
// WithNetEvents injects the OS network event handling.
func WithNetEvents(events *netevents.Manager) Option {
return func(c *GrpcClient) { c.netEvents = events }
return func(c *GrpcClient) { c.netMgr = events }
}
// NewClient creates a new Signal client
@@ -116,7 +116,7 @@ func NewClient(ctx context.Context, addr string, key wgtypes.Key, tlsEnabled boo
}
var extraOpts []grpc.DialOption
extraOpts = append(extraOpts, nbgrpc.WithSweeper(c.netEvents))
extraOpts = append(extraOpts, nbgrpc.WithSweeper(c.netMgr))
var conn *grpc.ClientConn
operation := func() error {
@@ -186,13 +186,13 @@ func defaultBackoff(ctx context.Context) backoff.BackOff {
// The connection retry logic will try to reconnect for 30 min and if wasn't successful will propagate the error to the function caller.
func (c *GrpcClient) Receive(ctx context.Context, msgHandler func(msg *proto.Message) error) error {
backOff := c.netEvents.QuickRetryBackoff(ctx, defaultBackoff(ctx))
backOff := c.netMgr.QuickRetryBackoff(ctx, defaultBackoff(ctx))
operation := func() error {
// suspend reconnect attempts while the OS reports no usable network.
// Wait only errors on a cancelled context, which means shutdown, so
// stop the loop without reporting a failure.
if waited, err := c.netEvents.Wait(ctx); err != nil {
if waited, err := c.netMgr.Wait(ctx); err != nil {
log.Debugf("signal connection context has been canceled while offline, this usually indicates shutdown")
return nil
} else if waited {
@@ -272,7 +272,7 @@ func (c *GrpcClient) Receive(ctx context.Context, msgHandler func(msg *proto.Mes
return nil
}
err := nbgrpc.Retry(ctx, operation, backOff, c.netEvents)
err := nbgrpc.Retry(ctx, operation, backOff, c.netMgr)
if err != nil {
log.Errorf("exiting the Signal service connection retry loop due to the unrecoverable error: %v", err)
return err