WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions shared/management/client/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ import (
"github.com/netbirdio/netbird/util/wsproxy"
)

const ConnectTimeout = 10 * time.Second
const (
ConnectTimeout = 10 * time.Second

RegistrationRetryTimeout = 180 * time.Second
RegistrationRetryInterval = 5 * time.Second
RegistrationRetryMaxDelay = 30 * time.Second

LoginRetryTimeout = 45 * time.Second
LoginRetryInterval = 3 * time.Second
LoginRetryMaxDelay = 15 * time.Second
)

const (
errMsgMgmtPublicKey = "failed getting Management Service public key: %s"
Expand Down Expand Up @@ -312,6 +322,38 @@ func (c *GrpcClient) IsHealthy() bool {
return true
}

// newRegistrationBackoff creates a backoff strategy for peer registration operations
func newRegistrationBackoff(ctx context.Context) backoff.BackOff {
return backoff.WithContext(&backoff.ExponentialBackOff{
InitialInterval: RegistrationRetryInterval,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: RegistrationRetryMaxDelay,
MaxElapsedTime: RegistrationRetryTimeout,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}, ctx)
}

// newLoginBackoff creates a backoff strategy for peer login operations
func newLoginBackoff(ctx context.Context) backoff.BackOff {
return backoff.WithContext(&backoff.ExponentialBackOff{
InitialInterval: LoginRetryInterval,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: LoginRetryMaxDelay,
MaxElapsedTime: LoginRetryTimeout,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}, ctx)
}

// isRegistrationRequest determines if a login request is actually a registration.
// Returns true if either the setup key or the JWT token is present in the request.
func isRegistrationRequest(req *proto.LoginRequest) bool {
return req.SetupKey != "" || req.JwtToken != ""
}

func (c *GrpcClient) login(serverKey wgtypes.Key, req *proto.LoginRequest) (*proto.LoginResponse, error) {
if !c.ready() {
return nil, errors.New(errMsgNoMgmtConnection)
Expand All @@ -334,17 +376,31 @@ func (c *GrpcClient) login(serverKey wgtypes.Key, req *proto.LoginRequest) (*pro
Body: loginReq,
})
if err != nil {
// retry only on context canceled
if s, ok := gstatus.FromError(err); ok && s.Code() == codes.Canceled {
return err
if s, ok := gstatus.FromError(err); ok {
if s.Code() == codes.Canceled {
log.Debugf("login canceled")
return backoff.Permanent(err)
}
if s.Code() == codes.DeadlineExceeded {
log.Debugf("login timeout, retrying...")
return err
}
Comment on lines +384 to +387
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log message 'login timeout, retrying...' doesn't distinguish between registration and login operations. Since these now have different timeout configurations, the message should indicate which operation timed out to aid debugging. Consider using a message like 'operation timeout, retrying...' or including the operation type.

Copilot uses AI. Check for mistakes.
}
return backoff.Permanent(err)
}

return nil
}

err = backoff.Retry(operation, nbgrpc.Backoff(c.ctx))
isRegistration := isRegistrationRequest(req)
var backoffStrategy backoff.BackOff
if isRegistration {
backoffStrategy = newRegistrationBackoff(c.ctx)
} else {
backoffStrategy = newLoginBackoff(c.ctx)
}

err = backoff.Retry(operation, backoffStrategy)
if err != nil {
log.Errorf("failed to login to Management Service: %v", err)
return nil, err
Expand Down
Loading