retry

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

Retry Build Status GoDoc License

Provide some retry policies to call a function, supporting Go1.7+.

Installation

$ go get -u github.com/xgfone/go-retry

Example

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/xgfone/go-retry"
)

func main() {
	num1, num2 := 1, 2
	var result int

	retry1 := retry.NewPeriodicIntervalRetry(1, time.Second)
	err := retry1.Run(context.TODO(), func(ctx context.Context) (success bool, err error) {
		result = num1 + num2
		return true, nil
	})
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%d + %d = %v\n", num1, num2, result)
	}

	// Output:
	// 1 + 2 = 3
}

Documentation

Overview

Package retry provides some retry policies to call a function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Retry

type Retry interface {
	// If f returns true or nil, it terminates retry.
	Run(c context.Context, f func(context.Context) (success bool, err error)) error
}

Retry is an interface to retry the function until a condition reaches.

func NewPeriodicIntervalRetry added in v0.2.0

func NewPeriodicIntervalRetry(number int, interval time.Duration) Retry

NewPeriodicIntervalRetry returns a new retry to call a runner function periodically until the context is done or it reaches the number.

number is the times to recall a function, which should be positive. If 0 or negative, it does nothing.

interval is the interval duration between two callings. If 0, it immediately retries to call.

Jump to

Keyboard shortcuts

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