retry

package module
v0.0.0-...-ed00287 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2020 License: Apache-2.0 Imports: 3 Imported by: 4

README

Retry is a pretty simple library to ensure your work to be done

godoc

Go Report Card cover.run

Features

  • Retry to run a workflow(Ex. rpc or db access)
  • Customize backoff strategy
  • Retry accoding to your type of error

Examples

func ExampleEnsure() {
    r := New()
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()

    err := r.Ensure(ctx, func() error {
        resp, err := http.Get("http://www.example.com")
        // Get error can be retried
        if err != nil {
            log.Println(err)
            return Retriable(err)
        }
        log.Println(resp)

        buf := bytes.NewBuffer(nil)
        resp, err = http.Post("http://example.com/upload", "image/jpeg", buf)
        // Post error should not be retried
        if err != nil {
            return err
        }
        log.Println(resp)
        return nil
    })
    if err != nil {
        log.Fatal(err)
    }
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ensure

func Ensure(ctx context.Context, do func() error) error

Ensure keeps retring until ctx is done, it use a default retry object

Example
r := New()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

err := r.Ensure(ctx, func() error {
	resp, err := http.Get("http://www.example.com")
	// Get error can be retried
	if err != nil {
		log.Println(err)
		return Retriable(err)
	}
	log.Println(resp)

	buf := bytes.NewBuffer(nil)
	resp, err = http.Post("http://example.com/upload", "image/jpeg", buf)
	// Gost error should not be retried
	if err != nil {
		return err
	}
	log.Println(resp)
	return nil
})
if err != nil {
	log.Fatal(err)
}
Output:

func EnsureN

func EnsureN(ctx context.Context, N int, do func() error) error

EnsureN retries N times before do is success, it uses a default retry object

Types

type BackoffStrategy

type BackoffStrategy func(last time.Duration) time.Duration

BackoffStrategy defines the backoff strategy of retry

func Exponential

func Exponential(factor float64) BackoffStrategy

Exponential generates backoff duration by expoential

func Tick

func Tick(tick time.Duration) BackoffStrategy

Tick keeps a constant backoff interval

type Option

type Option func(r *Retry)

Option is an option to new a Retry object

func WithBackoff

func WithBackoff(backoff BackoffStrategy) Option

WithBackoff replace the default backoff function

func WithBaseDelay

func WithBaseDelay(base time.Duration) Option

WithBaseDelay set the first delay duration, default 10ms

type RetriableErr

type RetriableErr struct {
	// contains filtered or unexported fields
}

RetriableErr is an error type which can be retried

func Retriable

func Retriable(err error) *RetriableErr

Retriable makes an error be retriable

type Retry

type Retry struct {
	// contains filtered or unexported fields
}

Retry ensures that the do function will be executed until some condition being satisfied

func New

func New(opts ...Option) *Retry

New a retry object

func (*Retry) Ensure

func (r *Retry) Ensure(ctx context.Context, do func() error) error

Ensure keeps retring until ctx is done

func (*Retry) EnsureN

func (r *Retry) EnsureN(ctx context.Context, N int, do func() error) error

EnsureN retries N times or until ctx is done before do is success

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL