Skip to content

RSDK-6155 - add initialDialAttempts to robot client options #4833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
20 changes: 18 additions & 2 deletions robot/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,24 @@ func New(ctx context.Context, address string, clientLogger logging.ZapCompatible
rc.dialOptions = append(rc.dialOptions, rpc.WithUnaryClientInterceptor(inter.UnaryClientInterceptor))
}

if err := rc.Connect(ctx); err != nil {
return nil, err
numAttempts := 3
if rOpts.initialConnectionAttempts != nil {
numAttempts = *rOpts.initialConnectionAttempts
}

if numAttempts == 0 {
numAttempts = -1
}

for {
if err := rc.Connect(ctx); err != nil {
numAttempts--
if numAttempts == 0 {
return nil, err
}
} else {
break
}
}

// If running in a testing environment, wait for machine to report a state of
Expand Down
14 changes: 14 additions & 0 deletions robot/client/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type robotClientOpts struct {
// controls whether or not sessions are disabled.
disableSessions bool

// initialConnectionAttempts indicates the number of times to try dialing when making
// initial connection to a machine. Defaults to three. If set to zero or a negative
// value, will attempt to connect forever.
initialConnectionAttempts *int

modName string
}

Expand Down Expand Up @@ -74,6 +79,15 @@ func WithRefreshEvery(refreshEvery time.Duration) RobotClientOption {
})
}

// WithInitialDialAttempts sets the number of times to attempt to connect to a robot when
// initially dialing. Defaults to 3 attempts. If set to zero or a negative value, will
// attempt to connect forever.
func WithInitialDialAttempts(attempts int) RobotClientOption {
return newFuncRobotClientOption(func(o *robotClientOpts) {
o.initialConnectionAttempts = &attempts
})
}

// WithCheckConnectedEvery returns a RobotClientOption for how often to check connection to the robot.
func WithCheckConnectedEvery(checkConnectedEvery time.Duration) RobotClientOption {
return newFuncRobotClientOption(func(o *robotClientOpts) {
Expand Down
Loading