xtry

package
v0.25.15 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

GoKit - xtry

Retry kits for Golang development.

Installation

go get -u github.com/likexian/gokit

Importing

import (
    "github.com/likexian/gokit/xtry"
)

Documentation

Visit the docs on GoDoc

Example

c := Config{
    Timeout: 5 * time.Minute,
    RetryDelay: 2 * time.Second,
}

ctx := context.Background()
err := c.Run(ctx, func(context.Context) error {
    return doSomething()
})
if err != nil {
    panic(err)
}

License

Copyright 2012-2024 Li Kexian

Licensed under the Apache License 2.0

Donation

If this project is helpful, please share it with friends.

If you want to thank me, you can give me a cup of coffee.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Author

func Author() string

Author returns package author

func License

func License() string

License returns package license

func Retry

func Retry(ctx context.Context, timeout time.Duration, fn func(context.Context) error) error

Retry do retry with timeout

func Version

func Version() string

Version returns package version

Types

type Config

type Config struct {
	// Retry until timeout elapsed, 0 means forever
	Timeout time.Duration

	// MaxTries is max retry times, 0 means forever
	MaxTries int

	// RetryDelay returns dealy time after failed, default is 1s
	RetryDelay func() time.Duration

	// ShouldRetry returns wether error should be retried, default true
	ShouldRetry func(error) bool
}

Config represents a retry config

func (Config) Run

func (c Config) Run(ctx context.Context, fn func(context.Context) error) error

Run calls fn util ctx is cancelled or max retry exhausted

type ExhaustedType

type ExhaustedType string

ExhaustedType is retry exhausted type

const (
	// Timeout is retry exhausted timeout
	Timeout ExhaustedType = "Timeout"
	// MaxTries is retry exhausted max times
	MaxTries ExhaustedType = "MaxTries"
	// Cancelled is retry is cancelled
	Cancelled ExhaustedType = "Cancelled"
	// NonRetry is non retryable
	NonRetry ExhaustedType = "NonRetry"
)

type RetryError

type RetryError struct {
	Err       error
	Retryable bool
}

RetryError is an error with retryable info

func NonRetryableError

func NonRetryableError(err error) *RetryError

NonRetryableError returns a not retryable error

func RetryableError

func RetryableError(err error) *RetryError

RetryableError returns a retryable error

func (*RetryError) Error

func (err *RetryError) Error() string

Error returns string of retry error

type RetryExhaustedError

type RetryExhaustedError struct {
	Err   error
	Type  ExhaustedType
	Times int
}

RetryExhaustedError is max retry exhausted error

func (*RetryExhaustedError) Error

func (err *RetryExhaustedError) Error() string

Error returns string of max retry exhausted error

Jump to

Keyboard shortcuts

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