Skip to content
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
23 changes: 11 additions & 12 deletions backend/pkg/transport/network/tcp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,25 @@ func (client *Client) Dial() (net.Conn, error) {
var err error
var conn net.Conn
client.logger.Info().Msg("dialing")
// Reset retry counter when starting a new dial attempt
client.currentRetries = 0


for client.config.MaxConnectionRetries <= 0 || client.currentRetries < client.config.MaxConnectionRetries {
// Increment retry counter and calculate backoff
client.currentRetries++

backoffDuration := client.config.ConnectionBackoffFunction(client.currentRetries)
client.logger.Error().Stack().Err(err).Dur("backoff", backoffDuration).Int("retry", client.currentRetries).Msg("retrying after backoff")

// Sleep for backoff duration
time.Sleep(backoffDuration)

conn, err = client.config.DialContext(client.config.Context, "tcp", client.address)

if err == nil {
client.logger.Info().Msg("connected")
client.currentRetries = 0
return conn, nil
}

// Check if context was cancelled
if client.config.Context.Err() != nil {
client.logger.Error().Stack().Err(client.config.Context.Err()).Msg("canceled")
Expand All @@ -62,14 +69,6 @@ func (client *Client) Dial() (net.Conn, error) {
client.logger.Error().Stack().Err(err).Msg("failed with non-retryable error")
return nil, err
}

// Increment retry counter and calculate backoff
client.currentRetries++
backoffDuration := client.config.ConnectionBackoffFunction(client.currentRetries)
client.logger.Error().Stack().Err(err).Dur("backoff", backoffDuration).Int("retry", client.currentRetries).Msg("retrying after backoff")

// Sleep for backoff duration
time.Sleep(backoffDuration)
}

client.logger.Debug().Int("max", client.config.MaxConnectionRetries).Msg("max connection retries exceeded")
Expand Down
Loading