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

Backoff Algorithm Updates #993

Merged
merged 11 commits into from
Jun 12, 2024
Merged

Backoff Algorithm Updates #993

merged 11 commits into from
Jun 12, 2024

Conversation

v0lkan
Copy link
Contributor

@v0lkan v0lkan commented Jun 12, 2024

Updates and improvements in the exponential backoff algorithm.

I’ll annotate the code to highlight important changes.

v0lkan added 10 commits June 12, 2024 15:23
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
Signed-off-by: Volkan Ozcelik <[email protected]>
@v0lkan v0lkan self-assigned this Jun 12, 2024
@v0lkan v0lkan requested a review from BulldromeQ as a code owner June 12, 2024 21:28
// Default is false
Exponential bool
// Maximum duration to wait between retries (in milliseconds)
// Default is 10 seconds
MaxDuration time.Duration
MaxWait time.Duration
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MaxWait is more understandable than MaxDuration.

// })
// if err != nil {
// fmt.Println("Failed to connect to database after retries:", err)
// }
func Retry(scope string, f func() error, s Strategy) error {
cid := crypto.Id()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

better to pass the id (and scope) through some context; but that can be done as a separate PR.

retryDelay = delay
} else { // otherwise delay is constant for all retries
retryDelay = s.Delay * time.Millisecond
multiplier = math.Pow(2, float64(i))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed the algorithm to multiply the given delay with a exponentially-increasing factor (the former algorighm was just using powers of two instead).

}

sDelayMs := s.Delay.Milliseconds()
if sDelayMs == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

you cannot pass 0 to rand.Intn().

_, _ = fmt.Printf(
"Retrying after %d ms for the scope '%s' -- attempt %d of %d",
retryDelay, scope, i+1, s.MaxRetries+1,
log.TraceLn(&cid, "Retry: will sleep:", delay)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

using the standard logging library, so that things can be managed/forwarded/aggregated from a central configuration (later).

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Useful to list all the environment variables that are used in the code, in alphabetical order.

Signed-off-by: Volkan Ozcelik <[email protected]>
@v0lkan
Copy link
Contributor Author

v0lkan commented Jun 12, 2024

cc: @gurkanguray — I modified the algo a little bit; wanted to ping you since the initial work has been started by you.

@v0lkan v0lkan merged commit 67b81f3 into main Jun 12, 2024
@v0lkan v0lkan deleted the ovolkan/backoff branch June 12, 2024 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant