errguard

package module
v0.0.0-...-ad26f7f Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2017 License: MIT Imports: 4 Imported by: 0

README

errguard GoDoc License Build Status Coverage Status GoReportCard

Package errguard makes it easy to retry error conditions that have a reasonable chance of succeeding after a short pause. These error conditions include optimistic locking and deadlock.

Read the package documentation for more information.

Licence

MIT

Documentation

Overview

Package errguard makes it easy to retry error conditions that have a resonable chance of succeeding after a short pause. These error conditions include optimistic locking and deadlock.

Example
package main

import (
	"context"
	"errors"
	"log"
	"math/rand"
)

func main() {
	ctx := context.TODO()
	var guard Guard
	guard.Logger = loggerFunc(func(v ...interface{}) error {
		log.Println(v...)
		return nil
	})

	guard.Run(ctx, func() error {
		return doSomethingWith(ctx)
	})
}

func doSomethingWith(ctx context.Context) error {
	log.Println("doing something")
	// has a 90% chance of failure
	if rand.Intn(10) > 0 {
		// Retry will mark this error as being retryable
		return Retry(errors.New("error condition"))
	}
	return nil
}

type loggerFunc func(v ...interface{}) error

func (f loggerFunc) Log(v ...interface{}) error {
	return f(v...)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

func Retry(err error) error

Retry wraps err to return an error that indicates the operation should be retried by the guard. If err is nil returns nil.

Types

type Guard

type Guard struct {
	// ShouldRetry returns true if the guard should retry
	// after encountering error err. If nil, the default
	// logic is used.
	ShouldRetry func(err error) bool

	// If logger is set, the guard will log a message every
	// time the guard encounters and error and retries.
	Logger Logger
}

Guard is used to retry error conditions that have a reasonable chance of succeeding.

func (*Guard) Run

func (g *Guard) Run(ctx context.Context, f func() error) error

Run function f and keep retrying while it returns a retryable error.

type Logger

type Logger interface {
	Log(v ...interface{}) error
}

Logger is the interface recognised by the guard.

var (
	// ShouldRetry is the default test for whether
	// a guard should retry after encountering error err.
	// This value is used if not specified for an individual
	// guard.
	ShouldRetry func(err error) bool

	// DefaultLogger is the default logger to use if
	// not specified for an individual guard.
	DefaultLogger Logger
)

Directories

Path Synopsis
cmd
Package gen generates code for errguard.
Package gen generates code for errguard.

Jump to

Keyboard shortcuts

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