Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Retries ¶ added in v0.298.0
func Retries[U FailureCount | StartedAt](ctx context.Context, rp RetryPolicy[U]) iter.Seq[FailureCount]
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/resilience" ) func main() { var ( ctx = context.Background() rp = resilience.ExponentialBackoff{} ) for range resilience.Retries(ctx, rp) { // on success, break out from retries break } }
Example (WithFailureCountBasedRetryPolicy) ¶
package main import ( "context" "go.llib.dev/frameless/pkg/resilience" ) func main() { var ( ctx = context.Background() rp = resilience.ExponentialBackoff{} ) for range resilience.Retries(ctx, rp) { // on success, break out from retries break } }
Example (WithFailureCountRangeArgument) ¶
package main import ( "context" "go.llib.dev/frameless/pkg/resilience" ) func main() { var ( ctx = context.Background() rp = resilience.ExponentialBackoff{} ) for failureCount := range resilience.Retries(ctx, rp) { _ = failureCount // starts from zero // on success, break out from retries break } }
Example (WithTimeDelayBasedRetryPolicy) ¶
package main import ( "context" "time" "go.llib.dev/frameless/pkg/resilience" ) func main() { var ( ctx = context.Background() rp = resilience.Waiter{Timeout: time.Minute} ) for range resilience.Retries(ctx, rp) { // on success, break out from retries break } }
Types ¶
type ExponentialBackoff ¶
type ExponentialBackoff struct { // Delay is the time duration being waited. // Initially, it serves as the starting wait duration, // and then it increases based on the exponential backoff formula calculation. // // Default: 1/2 Second Delay time.Duration // Timeout is the time within the RetryPolicy is attempting further retries. // If the total waited time is greater than the Timeout, ExponentialBackoff will stop further attempts. // When Timeout is given, but MaxRetries is not, ExponentialBackoff will continue to retry until the calculated deadline is reached. // // Default: ignored Timeout time.Duration // Attempts is the amount of retry which is allowed before giving up the application. // // Default: 5 if Timeout is not set. Attempts int }
ExponentialBackoff is a RetryPolicy implementation.
ExponentialBackoff will answer if retry can be made. It waits as well the amount of time based on the failure count. The waiting time before returning is doubled for each failed attempts This ensures that the system gets progressively more time to recover from any issues.
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/resilience" ) func main() { ctx := context.Background() rs := resilience.ExponentialBackoff{} for i := 0; rs.ShouldTry(ctx, i); i++ { // do an action // return on success } // return failure }
func (ExponentialBackoff) ShouldTry ¶
func (rs ExponentialBackoff) ShouldTry(ctx context.Context, failureCount FailureCount) bool
type FailureCount ¶
type FailureCount = int
type FixedDelay ¶
type FixedDelay struct { // Delay is the time duration waited between attempts. // // Default: 1/2 Second Delay time.Duration // Timeout is the time within the RetryPolicy is attempting further retries. // If the total waited time is greater than the Timeout, ExponentialBackoff will stop further attempts. // When Timeout is given, but MaxRetries is not, ExponentialBackoff will continue to retry until a calculated deadline is reached. // // Default: ignored Timeout time.Duration // Attempts is the amount of retry attempt which is allowed before giving up the application. // // Default: 5 if Timeout is not set. Attempts int }
FixedDelay is a RetryPolicy implementation.
FixedDelay will make retries with fixed delays between them. It is a lineral waiting time based retry policy.
Example ¶
package main import ( "context" "time" "go.llib.dev/frameless/pkg/resilience" ) func main() { ctx := context.Background() rs := resilience.FixedDelay{ Delay: 10 * time.Second, Timeout: 5 * time.Minute, } for i := 0; rs.ShouldTry(ctx, i); i++ { // do an action // return/break on success } // return failure }
func (FixedDelay) ShouldTry ¶
func (rs FixedDelay) ShouldTry(ctx context.Context, failureCount FailureCount) bool
type Jitter ¶
type Jitter struct { // Delay is the maximum time duration that the Jitter is willing to wait between attempts. // There is no guarantee that it will wait the full duration. // // Default: 5 Second Delay time.Duration // Attempts is the amount of retry that is allowed before giving up the application. // // Default: 5 Attempts int }
Jitter is a RetryPolicy implementation.
Jitter is a random variation added to the backoff time. This helps to distribute the retry attempts evenly over time, reducing the risk of overwhelming the system and avoiding synchronization between multiple clients that might be retrying simultaneously.
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/resilience" ) func main() { ctx := context.Background() rs := resilience.Jitter{} for i := 0; rs.ShouldTry(ctx, i); i++ { // do an action // return on success } // return failure }
type Rate ¶
type RateLimitPolicy ¶
type RetryPolicy ¶
type RetryPolicy[U FailureCount | StartedAt] interface { // ShouldTry will tell if retry should be attempted after a given number of failed attempts. ShouldTry(ctx context.Context, u U) bool }
type SlidingWindow ¶
type SlidingWindow struct { Rate Rate // contains filtered or unexported fields }
Example ¶
package main import ( "context" "time" "go.llib.dev/frameless/pkg/resilience" ) func main() { var ( ctx = context.Background() rl = resilience.SlidingWindow{Rate: resilience.Rate{N: 100, Per: time.Minute}} ) if err := rl.RateLimit(ctx); err != nil { // err could be like context cancellation _ = err // return err } }
type Waiter ¶
type Waiter struct { // Timeout refers to the maximum duration we can wait // before a retry attempt is deemed unreasonable. // // Default: 30 seconds Timeout time.Duration // WaitDuration is the time how lone Waiter.Wait should wait between attempting a new retry during Waiter.While. // // Default: 1ms WaitDuration time.Duration }
Waiter is a RetryPolicy implementation.
Waiter will check if a retry attempt should be made compared to when an operation was initially started.
func (Waiter) ShouldTry ¶
Example ¶
package main import ( "context" "time" "go.llib.dev/frameless/pkg/resilience" ) func main() { var ( ctx = context.Background() rs = resilience.Waiter{Timeout: time.Minute} now = time.Now() ) for rs.ShouldTry(ctx, now) { // do an action // return on success } // return failure }