Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/ retrier Public archive

A simple Go (Golang) library for retries featuring generics.

License

Notifications You must be signed in to change notification settings

craigpastro/retrier

Repository files navigation

retrier

Go Reference Go Report Card CI codecov

A simple Go (Golang) library for retries featuring generics. Backoff is configurable. The most useful is probably exponential backoff with full jitter.

For usage see the godoc.

Usage

Making an HTTP request

body, err := retrier.DoWithData(func() ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	return body, nil
}, retrier.NewExponentialBackoff())

Connecting to a DB

err := retrier.Do(func() error {
	// db of type *sql.DB
	if err = db.PingContext(context.Background()); err != nil {
		return err
	}
	return nil
}, retrier.NewExponentialBackOff())
if err != nil {
	return nil, fmt.Errorf("error pinging the db: %w", err)
}

Contributions

Contributions are welcome! Please create an issue for significant changes.

Inspired By