Skip to content

Re-execution for functions within configurable limits

License

Notifications You must be signed in to change notification settings

da440dil/go-trier

Repository files navigation

go-trier

Build Status Coverage Status Go Reference Go Report Card

Re-execution for functions within configurable limits.

Example usage:

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/da440dil/go-trier"
)

func main() {
	// Create trier.
	tr := trier.NewTrier(
		// Use linear algorithm to create delay between retries.
		trier.Linear(time.Millisecond*10),
		// Set maximum number of retries.
		trier.WithMaxRetries(5),
		// Set maximum duration randomly added to or extracted from delay
		// between retries to improve performance under high contention
		trier.WithJitter(time.Millisecond*5),
	)
	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200)
	defer cancel()
	for i := 0; i < 3; i++ {
		ok, err := tr.Try(ctx, func(ctx context.Context) (bool, error) {
			return i == 0, nil
		})
		fmt.Printf("{ i: %v, ok: %v, err: %v }\n", i, ok, err)
	}
	// Output:
	// { i: 0, ok: true, err: <nil> }
	// { i: 1, ok: false, err: <nil> }
	// { i: 2, ok: false, err: context deadline exceeded }
}

About

Re-execution for functions within configurable limits

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages