Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Retry a function until is succeeds

License

NotificationsYou must be signed in to change notification settings

arsham/retry

Repository files navigation

LicenseGitHub go.mod Go versiongo.dev referenceBuild StatusCoverage StatusGo Report Card

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})

License

Use of this source code is governed by the Apache 2.0 license. License can befound in theLICENSE file.


[8]ページ先頭

©2009-2025 Movatter.jp