Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork14
♻️ The most advanced interruptible mechanism to perform actions repetitively until successful.
License
kamilsk/retry
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Functional mechanism based on channels to perform actions repetitively until successful.
var (response*http.Responseaction retry.Action=func(_uint)error {varerrerrorresponse,err=http.Get("https://github.com/kamilsk/retry")returnerr})iferr:=retry.Retry(retry.WithTimeout(time.Minute),action,strategy.Limit(10));err!=nil {// handle errorreturn}// work with response
This example shows how to repeat console command until successful.
$ retry --infinite -timeout 10m -backoff=lin:500ms -- /bin/sh -c'echo "trying..."; exit $((1 + RANDOM % 10 > 5))'
See more detailshere.
This example shows how to extend standard http.Client with retry under the hood.
typeclientstruct {base*http.Clientstrategies []strategy.Strategy}funcNew(timeout time.Duration,strategies...strategy.Strategy)*client {return&client{base:&http.Client{Timeout:timeout},strategies:strategies,}}func (c*client)Get(deadline<-chanstruct{},urlstring) (*http.Response,error) {varresponse*http.Responseerr:=retry.Retry(deadline,func(uint)error {resp,err:=c.base.Get(url)iferr!=nil {returnerr}response=respreturnnil},c.strategies...)returnresponse,err}
This example shows how to use retry to restore database connection bydatabase/sql/driver.Pinger
.
MustOpen:=func()*sql.DB {db,err:=sql.Open("stub","stub://test")iferr!=nil {panic(err)}returndb}gofunc(db*sql.DB,ctx context.Context,shutdownchan<-struct{},frequency time.Duration,strategies...strategy.Strategy) {deferfunc() {ifr:=recover();r!=nil {shutdown<-struct{}{}}}()ping:=func(uint)error {returndb.Ping()}for {iferr:=retry.Retry(ctx.Done(),ping,strategies...);err!=nil {panic(err)}time.Sleep(frequency)}}(MustOpen(),context.Background(),shutdown,time.Millisecond,strategy.Limit(1))
This example shows how to use context and retry together.
communication:=make(chanerror)goservice.Listen(communication)action:=func(uint)error {communication<-nil// pingreturn<-communication// pong}ctx:=retry.WithContext(context.Background(),retry.WithTimeout(time.Second))iferr:=retry.Retry(ctx.Done(),action,strategy.Delay(time.Millisecond));err!=nil {// the service does not respond within one second}
interrupter:=retry.Multiplex(retry.WithTimeout(time.Second),retry.WithSignal(os.Interrupt),)iferr:=retry.Retry(interrupter,func(uint)error {time.Sleep(time.Second);returnnil });err==nil {panic("press Ctrl+C")}// successful interruption
$ go get github.com/kamilsk/retry$# or use mirror$ egg bitbucket.org/kamilsk/retry
This library is usingSemVer for versioning, and it is notBC-safe. Therefore, do not usego get -u
to update it,usedep,glide or something similar for this purpose.
1 The project is still in prototyping.↩
made with ❤️ byOctoLab
About
♻️ The most advanced interruptible mechanism to perform actions repetitively until successful.
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.