retry

package
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: MIT Imports: 6 Imported by: 0

README

Retry - Hyperf jet middleware

Retry middleware for Hyperf jet.

Usage Example

package main

import (
	"context"
	"errors"
	"log"
	"time"

	"github.com/go-kratos-ecosystem/components/v2/hyperf/jet"
	"github.com/go-kratos-ecosystem/components/v2/hyperf/jet/middleware/retry"
)

var customErr = errors.New("custom error")

func main() {
	client, err := jet.NewClient(
		jet.WithTransporter(nil),
		jet.WithService("Example/User/MoneyService"),
	)
	if err != nil {
		log.Fatal(err)
	}

	// base usage
	client.Use(retry.New())

	// with options
	client.Use(retry.New(
		// allow retry when custom error
		retry.Allow(func(err error) bool {
			return errors.Is(err, customErr)
		}),

		// or: allow retry with OrAllowFuncs
		retry.Allow(retry.OrAllowFuncs(
			retry.DefaultAllow,
			func(err error) bool {
				return errors.Is(err, customErr)
			}),
		// ... more allow
		),

		// retry 3 times
		retry.Attempts(3),

		// retry with backoff
		retry.Backoff(retry.LinearBackoff(100*time.Second)),
		// or: retry with NoBackoff
		retry.Backoff(retry.NoBackoff()),
		// or: retry with ExponentialBackoff
		retry.Backoff(retry.ExponentialBackoff(100*time.Second)),
		// or: retry with ConstantBackoff
		retry.Backoff(retry.ConstantBackoff(100*time.Second)),
	))

	// call service
	client.Invoke(context.Background(), "service", []any{"..."}, nil)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultBackoff = LinearBackoff(100 * time.Millisecond) //nolint:mnd

Functions

func IsError

func IsError(err error) bool

func New

func New(opts ...Option) jet.Middleware

Types

type AllowFunc

type AllowFunc func(err error) bool
var DefaultAllow AllowFunc = func(err error) bool {
	return jet.IsHTTPTransporterServerError(err) &&
		errors.Is(err, timeout.ErrTimeout)
}

func OrAllowFuncs

func OrAllowFuncs(fs ...AllowFunc) AllowFunc

type BackoffFunc

type BackoffFunc func(attempt int) time.Duration

func ConstantBackoff

func ConstantBackoff(delay time.Duration) BackoffFunc

ConstantBackoff returns a backoff function that always returns the same delay.

func ExponentialBackoff

func ExponentialBackoff(delay time.Duration) BackoffFunc

ExponentialBackoff returns a backoff function that increases the delay exponentially.

func LinearBackoff

func LinearBackoff(delay time.Duration) BackoffFunc

LinearBackoff returns a backoff function that increases the delay linearly.

func NoBackoff

func NoBackoff() BackoffFunc

type Error

type Error struct {
	Attempts int
	Start    time.Time
	End      time.Time
	Err      error
}

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Option

type Option func(o *options)

func Allow

func Allow(f AllowFunc) Option

func Attempts

func Attempts(attempts int) Option

func Backoff

func Backoff(f BackoffFunc) Option

Jump to

Keyboard shortcuts

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