Skip to content
forked from coder/retry

A tiny retry package for Go.

License

Notifications You must be signed in to change notification settings

WinPooh32/retry

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

retry

An exponentially backing off retry package for Go.

GoDoc

go get github.com/WinPooh32/retry@latest

retry promotes control flow using for/goto instead of callbacks.

Examples

Wait for connectivity to google.com, checking at most once every second:

func pingGoogle(ctx context.Context) error {
	var err error

	r := retry.New(time.Second, time.Second*10);

	// Jitter is useful when the majority of clients to a service use
	// the same backoff policy.
	//
	// It is provided as a standard deviation.
	r.Jitter = 0.1

  retry:
	_, err = http.Get("https://google.com")
	if err != nil {
		if r.Wait(ctx) {
			goto retry
		}
		return err
	}

	return nil
}

Wait for connectivity to google.com, checking at most 10 times:

func pingGoogle(ctx context.Context) error {
	var err error
	
	for r := retry.New(time.Second, time.Second*10, retry.Attempts(10)); r.Wait(ctx); {
		_, err = http.Get("https://google.com")
		if err != nil {
			continue
		}
		break
	}
	return err
}

About

A tiny retry package for Go.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Go 100.0%