Skip to content

Commit

Permalink
dev: make zookeeper driver retry on failed attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsingerus committed Nov 29, 2024
1 parent 5f1f1b8 commit 5389882
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pkg/model/zookeeper/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ func (c *Connection) retry(ctx context.Context, fn func(*zk.Conn) error) error {

for i := 0; i < c.MaxRetriesNum; i++ {
if i > 0 {
time.Sleep(1*time.Second + time.Duration(rand.Int63n(int64(1*time.Second))))
// Progressive delay before each retry
time.Sleep(time.Duration(i)*time.Second + time.Duration(rand.Int63n(int64(1*time.Second))))
}

connection, err := c.ensureConnection(ctx)
Expand Down
16 changes: 8 additions & 8 deletions pkg/model/zookeeper/connection_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package zookeeper
import "time"

const (
maxRetriesNum = 3
maxConcurrentRequests int64 = 32
defaultMaxRetriesNum = 25
defaultMaxConcurrentRequests int64 = 32

timeoutConnect = 30 * time.Second
timeoutKeepAlive = 30 * time.Second
defaultTimeoutConnect = 30 * time.Second
defaultTimeoutKeepAlive = 30 * time.Second
)

type ConnectionParams struct {
Expand All @@ -42,16 +42,16 @@ func (p *ConnectionParams) Normalize() *ConnectionParams {
p = &ConnectionParams{}
}
if p.MaxRetriesNum == 0 {
p.MaxRetriesNum = maxRetriesNum
p.MaxRetriesNum = defaultMaxRetriesNum
}
if p.MaxConcurrentRequests == 0 {
p.MaxConcurrentRequests = maxConcurrentRequests
p.MaxConcurrentRequests = defaultMaxConcurrentRequests
}
if p.TimeoutConnect == 0 {
p.TimeoutConnect = timeoutConnect
p.TimeoutConnect = defaultTimeoutConnect
}
if p.TimeoutKeepAlive == 0 {
p.TimeoutKeepAlive = timeoutKeepAlive
p.TimeoutKeepAlive = defaultTimeoutKeepAlive
}
return p
}

0 comments on commit 5389882

Please sign in to comment.