backoff

package
v0.0.0-...-53df4c2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package backoff ...

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff struct {

	// MaxAttempts is the max number of attempts that can occur.
	MaxAttempts uint
	// Factor is the factor at which Min will increase after each failed attempt.
	Factor float64
	// Min is the initial backoff time to wait after the first failed attempt.
	Min time.Duration
	// Max is the maximum time to wait before retrying.
	Max time.Duration

	// NewTimer is used for unit tests.  For actual use, this should be set to
	// time.NewTimer.
	NewTimer func(time.Duration) *time.Timer
	// contains filtered or unexported fields
}

Backoff represents an exponential backoff.

func New

func New(maxAttempts uint, factor float64, min, max time.Duration) *Backoff

New returns a new Backoff instance.

Example
package main

import (
	"context"
	"time"

	"github.com/matthewpi/cosmos/internal/backoff"
)

func main() {
	b := backoff.New(3, 2, 1*time.Second, 5*time.Second)
	for b.Next(context.Background()) {
		// Do something
		// break if successful, continue on failure
	}
}
Output:

func (*Backoff) Attempt

func (b *Backoff) Attempt() uint

Attempt returns the current attempt.

func (*Backoff) Duration

func (b *Backoff) Duration(attempt float64) time.Duration

Duration returns a time.Duration to wait for a specified attempt.

func (*Backoff) Next

func (b *Backoff) Next(ctx context.Context) bool

Next increments the attempt, then waits for the Duration of the attempt to pass, returning true. Next will return false if the attempt will exceed the MaxAttempts limit or if the context has been cancelled.

Jump to

Keyboard shortcuts

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