- Notifications
You must be signed in to change notification settings - Fork0
Retry a function until is succeeds
License
arsham/retry
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This library supportsGo >= 1.22
by gettinggithub.com/arsham/retry/v3
. Forolder versions (Go >= 1.20
) importgithub.com/arsham/retry/v2
, or older Goversions importgithub.com/arsham/retry
.
Retry
calls your function, and if it errors it calls it again with a delay.Eventually it returns the last error or nil if one call is successful.
r:=&retry.Retry{Attempts:666,Delay:time.Millisecond,}err:=r.Do(func()error {// do some work.returnnil})
You can provide multiple functions:
err:=r.Do(func()error {returnnil},func()error {returnnil}}
You can use theDoContext
and pass a context object to stop when the contextis cancelled:
err:=r.DoContext(ctx,func()error {returnnil})
If you want to stop retrying you can return a special error:
err:=r.Do(func()error {ifspecialCase {return&retry.StopError{Err:errors.New("a special stop"),}}returnnil})
The standard behaviour is to delay the amount you set. You can pass any functionwith this signature to change the delay behaviour:
func(attemptint,delay time.Duration) time.Duration
You can also pass theretry.IncrementalDelay
function that would increase thedelay with a jitter to preventThunderingherd.
r:=&retry.Retry{Attempts:666,Delay:10*time.Millisecond,Method:retry.IncrementalDelay,}err:=r.Do(func()error {ifspecialCase {return&retry.StopError{Err:errors.New("a special stop"),}}returnnil})
Use of this source code is governed by the Apache 2.0 license. License can befound in theLICENSE file.
About
Retry a function until is succeeds