Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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
Appearance settings

♻️ The most advanced interruptible mechanism to perform actions repetitively until successful.

License

NotificationsYou must be signed in to change notification settings

kamilsk/retry

Repository files navigation

Functional mechanism based on channels to perform actions repetitively until successful.

AwesomePatreonBuild StatusCode CoverageCode QualityGoDocResearchLicense

Usage

Quick start

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

Console tool for command execution with retries

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

asciicast

See more detailshere.

Create HTTP client with retry

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}

Control database connection

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

Use context for cancellation

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}

Interrupt execution

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

Installation

$ go get github.com/kamilsk/retry$# or use mirror$ egg bitbucket.org/kamilsk/retry

egg1 is anextended go get.

Update

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.


Gitter@kamilsk@octolab

made with ❤️ byOctoLab

About

♻️ The most advanced interruptible mechanism to perform actions repetitively until successful.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp