Skip to content

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

License

Notifications You must be signed in to change notification settings

typusomega/poligo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published