backoff

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-backoff

Package backoff provides wrapper functions for "github.com/cenkalti/backoff/v4".

Documentation

Overview

Package backoff provides wrapper functions for "github.com/cenkalti/backoff/v4".

Example
package main

import (
	"context"
	"fmt"
	"time"

	backoff "github.com/takumakei/go-backoff"
)

func main() {
	// mockAPI takes 2 parameters, returns 2 values.
	mockAPI := func(ctx context.Context, name string) (string, error) { return name, nil }

	ctx := context.Background()

	// - P2 means that the target function takes 2 parameters.
	// - R2 means that the target function returns 2 values.
	// - types of result and err is inferred by the target function.
	result, err := backoff.RetryP2R2(
		// the target function to retry when an error occurs
		mockAPI,
		// the 1st argument for the target function
		ctx,
		// the 2nd argument for the target function
		"hello",

		// parameters for ExponentialBackOff
		backoff.MaxInterval(7*time.Second),
		backoff.Stop(7*time.Second),
		backoff.Context(ctx),
		backoff.MaxRetries(7),
	)
	if err != nil {
		fmt.Printf("error: %v", err)
	} else {
		fmt.Println(result)
	}

}
Output:

hello

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(exp *backoff.ExponentialBackOff, options ...Option) backoff.BackOff

Apply applies options to exp, returns it as backoff.BackOff.

func New

func New(options ...Option) backoff.BackOff

New creates an backoff.ExponentialBackOff, applies options to it, returns it as backoff.BackOff.

func RetryP0R1

func RetryP0R1(fn func() error, options ...Option) error

RetryP0R1 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has zero parameter. (P0)
  • fn returns one value of error. (R1)

func RetryP0R2

func RetryP0R2[R0 any](fn func() (R0, error), options ...Option) (r0 R0, err error)

RetryP0R2 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has zero parameter. (P0)
  • fn returns 2 values. (R2)

func RetryP0R3

func RetryP0R3[R0, R1 any](fn func() (R0, R1, error), options ...Option) (r0 R0, r1 R1, err error)

RetryP0R3 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has zero parameter. (P0)
  • fn returns 3 values. (R3)

func RetryP1R1

func RetryP1R1[P0 any](fn func(P0) error, p0 P0, options ...Option) error

RetryP1R1 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has one parameter. (P1)
  • fn returns one value. (R1)

func RetryP1R2

func RetryP1R2[P0, R0 any](fn func(P0) (R0, error), p0 P0, options ...Option) (R0, error)

RetryP1R2 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has one parameter. (P1)
  • fn returns 2 values. (R2)

func RetryP1R3

func RetryP1R3[P0, R0, R1 any](fn func(P0) (R0, R1, error), p0 P0, options ...Option) (R0, R1, error)

RetryP1R3 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has one parameter. (P1)
  • fn returns 3 values. (R3)

func RetryP2R1

func RetryP2R1[P0, P1 any](fn func(P0, P1) error, p0 P0, p1 P1, options ...Option) error

RetryP2R1 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has 2 parameters. (P2)
  • fn returns one value. (R1)

func RetryP2R2

func RetryP2R2[P0, P1, R0 any](fn func(P0, P1) (R0, error), p0 P0, p1 P1, options ...Option) (R0, error)

RetryP2R2 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has 2 parameters. (P2)
  • fn returns 2 values. (R2)

func RetryP2R3

func RetryP2R3[P0, P1, R0, R1 any](fn func(P0, P1) (R0, R1, error), p0 P0, p1 P1, options ...Option) (R0, R1, error)

RetryP2R3 calls fn under backoff.Retry with ExponentialBackOff with options.

  • fn has 2 parameters. (P2)
  • fn returns 3 values. (R3)

Types

type Option

type Option func(*builder)

Option is type of setting parameters of ExponentialBackOff.

func Clock

func Clock(clock backoff.Clock) Option

Clock uses clock as Clock.

see: https://pkg.go.dev/github.com/cenkalti/backoff/v4#ExponentialBackOff

func Context

func Context(ctx context.Context) Option

Context applies backoff.WithContext with ctx.

see: https://pkg.go.dev/github.com/cenkalti/backoff/v4#WithContext

func InitialInterval

func InitialInterval(d time.Duration) Option

InitialInterval uses d as InitialInterval.

see: https://pkg.go.dev/github.com/cenkalti/backoff/v4#ExponentialBackOff

func MaxElapsedTime

func MaxElapsedTime(d time.Duration) Option

MaxElapsedTime uses t as MaxElapsedTime.

see: https://pkg.go.dev/github.com/cenkalti/backoff/v4#ExponentialBackOff

func MaxInterval

func MaxInterval(d time.Duration) Option

MaxInterval uses d as MaxInterval.

see: https://pkg.go.dev/github.com/cenkalti/backoff/v4#ExponentialBackOff

func MaxRetries

func MaxRetries(max uint64) Option

MaxRetries applies backoff.WithMaxRetries with max.

see: https://pkg.go.dev/github.com/cenkalti/backoff/v4#WithMaxRetries

func Multiplier

func Multiplier(f float64) Option

Multiplier uses f as Multiplier.

see: https://pkg.go.dev/github.com/cenkalti/backoff/v4#ExponentialBackOff

func RandomizationFactor

func RandomizationFactor(f float64) Option

RandomizationFactor uses f as RandomizationFactor.

see: https://pkg.go.dev/github.com/cenkalti/backoff/v4#ExponentialBackOff

Jump to

Keyboard shortcuts

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