Skip to content
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

add WithNoRetryHosts optionFunc to handle all-hosts-invalid case better #1

Merged
merged 1 commit into from
Feb 10, 2023
Merged
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
24 changes: 20 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Conn struct {
xid uint32
sessionTimeoutMs int32 // session timeout in milliseconds
passwd []byte
noRetryHosts bool // if true, will abort when all hosts have been tried (once) and failed

dialer Dialer
hostProvider HostProvider
Expand Down Expand Up @@ -271,6 +272,12 @@ func WithEventCallback(cb EventCallback) connOption {
}
}

func WithNoRetryHosts() connOption {
return func(c *Conn) {
c.noRetryHosts = true
}
}

// WithMaxBufferSize sets the maximum buffer size used to read and decode
// packets received from the Zookeeper server. The standard Zookeeper client for
// Java defaults to a limit of 1mb. For backwards compatibility, this Go client
Expand Down Expand Up @@ -375,10 +382,19 @@ func (c *Conn) connect() error {

if retryStart {
c.flushUnsentRequests(ErrNoServer)
select {
case <-time.After(time.Second):
// pass
case <-c.shouldQuit:

shouldQuit := c.noRetryHosts

if !shouldQuit {
select {
case <-time.After(time.Second):
// pass
case <-c.shouldQuit:
shouldQuit = true
}
}

if shouldQuit {
c.setState(StateDisconnected)
c.flushUnsentRequests(ErrClosing)
return ErrClosing
Expand Down