Help us shape the future of Go! Take the 2026 Go Developer Survey

poligo

module
v0.0.0-...-48c1c54 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2019 License: MIT

README

PoliGo

PoliGo is a Go resilience and fault-handling library to help developers express policies such as Retry in a fluent manner.

Installation

go.mod require github.com/typusomega/poligo

go get go get github.com/typusomega/poligo

How to use

import (
	"context"
	"fmt"
	"time"

	"github.com/typusomega/poligo/pkg/policy"
)

func main() {
	// select kind of errors to handle
	result, err := policy.Handle(func(_ error) bool { return true }).
		// tell you want to retry and how often
		Retry(policy.WithDurations(time.Second, time.Second, time.Second),
			// tell what to do before the next retry
			policy.WithCallback(log)).
		// execute the given action with the created policy
		Execute(context.Background(), doAwesomeStuff)

	fmt.Printf("executed with policy result: '%v', err: '%v'\n", result, err)
}

func log(err error, retryCount int) {
	fmt.Printf("execution failed: '%v' (retry %v)\n", err, retryCount)
}

func doAwesomeStuff() (interface{}, error) {
	return 42, fmt.Errorf("fail")
}
Handle, HandleErrorType

Very often we want to have all kinds of errors handled no matter their reason.

policy.HandleAll().
	Retry().

Sometimes checking errors is as trivial as just switching its type.

policy.HandleErrorType(MyCustomError{}).
	Or(AnotherCustomError{}).

But in some cases we have special needs and need special policies for specific states of a given error. This is when policy.Handle comes into play.

	// only handle errors with `lenghtNegative` with this policy
	pol := policy.Handle(func(err error) bool {
		if err != nil {
			if err, ok := err.(*areaError); ok {
				if err.lengthNegative() {
					return true
				}
			}
		}
		return false
	}).Retry()

PoliGo is strongly inspired by the awesome c# alternative Polly

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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