Skip to content

xgfone/go-retry

Repository files navigation

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
}