retry

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: MIT Imports: 6 Imported by: 0

README

Package retry

retry

Usage

package main

import (
	"errors"
	"log"
	"time"

	"github.com/QisFj/godry/retry"
)

func main() {
	r := retry.New(retry.Option{
		MaxRunTime:    5,
		RetryInterval: time.Second,
		F: func(r *retry.Retry) error {
			log.Printf("Run: %d\n", r.RunCount()+1)
			return errors.New("always run")
		},
		ResultSize: 3,
		ErrorWrapper: func(r *retry.Retry, err error) error {
			if err != nil { // error may be nil
				log.Printf("error: %s\n", err)
			}
			return err
		},
	})
	r.Start().Wait() // Start & Wait
	for i := 1; i <= 5; i++ {
		result := r.Result(i) // Start From 1
		if !result.Valid {
			log.Printf("Run %d: No Valid Result\n", i)
		} else if result.Error == nil {
			log.Printf("Run %d: Result: success\n", i)
		} else {
			log.Printf("Run %d: Result: error: %s\n", i, result.Error)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StopRetryWithError

func StopRetryWithError(err error) error

Types

type ErrorWrapper

type ErrorWrapper func(r *Retry, err error) error

func ErrorWrapperChain

func ErrorWrapperChain(wrappers ...ErrorWrapper) ErrorWrapper

func FmtErrorWrapper

func FmtErrorWrapper(format string) ErrorWrapper

func SkipNoErrorWrap

func SkipNoErrorWrap(wrapper ErrorWrapper) ErrorWrapper

type Func

type Func func(r *Retry) error

type Option

type Option struct {
	MaxRunTime    uint64 // 0: always return; 1: execute only once, without retry
	RetryInterval time.Duration
	F             Func
	ResultSize    int // non-positive number means unlimited
	ErrorWrapper  ErrorWrapper
}

type Result

type Result struct {
	Valid bool
	Error error
}

type Retry

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

func New

func New(option Option) *Retry

func (*Retry) HasDone

func (r *Retry) HasDone() bool

func (*Retry) LatestResult

func (r *Retry) LatestResult() Result

func (*Retry) Result

func (r *Retry) Result(runTime int) Result

func (*Retry) RunCount

func (r *Retry) RunCount() uint64

func (*Retry) Start

func (r *Retry) Start() *Retry

func (*Retry) Stop

func (r *Retry) Stop()

func (*Retry) Wait

func (r *Retry) Wait() *Retry

Jump to

Keyboard shortcuts

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