-
Notifications
You must be signed in to change notification settings - Fork 26
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
Conversation
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]>
// Default is false | ||
Exponential bool | ||
// Maximum duration to wait between retries (in milliseconds) | ||
// Default is 10 seconds | ||
MaxDuration time.Duration | ||
MaxWait time.Duration |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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]>
cc: @gurkanguray — I modified the algo a little bit; wanted to ping you since the initial work has been started by you. |
Updates and improvements in the exponential backoff algorithm.
I’ll annotate the code to highlight important changes.