sandbox

package module
v0.0.0-...-0f199af Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2021 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package exp contains experimental unstable features.

Example
//go:build go1.13
// +build go1.13

package main

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"math/rand"
	"net"
	"time"

	"github.com/kamilsk/retry/sandbox"

	"github.com/kamilsk/retry/v5"
	"github.com/kamilsk/retry/v5/backoff"
	"github.com/kamilsk/retry/v5/jitter"
	"github.com/kamilsk/retry/v5/strategy"
)

var generator = rand.New(rand.NewSource(0))

func main() {
	what := SendRequest

	how := retry.How{
		strategy.Limit(5),
		strategy.BackoffWithJitter(
			backoff.Fibonacci(10*time.Millisecond),
			jitter.NormalDistribution(
				rand.New(rand.NewSource(time.Now().UnixNano())),
				0.25,
			),
		),

		// experimental
		sandbox.CheckError(
			sandbox.NetworkError(sandbox.Skip),
			DatabaseError(),
		),
	}

	breaker, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	if err := retry.Do(breaker, what, how...); err != nil {
		panic(err)
	}
	fmt.Println("success communication")
}

func SendRequest(ctx context.Context) error {
	select {
	case <-ctx.Done():
		return ctx.Err()
	default:
	}
	if generator.Intn(5) > 3 {
		return &net.DNSError{Name: "unknown host", IsTemporary: true}
	}
	return nil
}

func DatabaseError() func(error) bool {
	deprecated := []error{sql.ErrNoRows, sql.ErrConnDone, sql.ErrTxDone}
	return func(err error) bool {
		for _, deprecated := range deprecated {
			if errors.Is(err, deprecated) {
				return false
			}
		}
		return true
	}
}
Output:

success communication

Index

Examples

Constants

View Source
const (
	Skip = true
	Stop = false
)

Variables

This section is empty.

Functions

func CheckError

func CheckError(handlers ...func(error) bool) func(Breaker, uint, error) bool

CheckError creates a Strategy that checks an error and returns if an error is retriable or not. Otherwise, it returns the defaults.

func NetworkError

func NetworkError(defaults bool) func(error) bool

NetworkError creates an error Handler that checks an error and returns true if an error is the temporary network error. The Handler returns the defaults if an error is not a network error.

Types

type Breaker

type Breaker = interface {
	// Done returns a channel that's closed when a cancellation signal occurred.
	Done() <-chan struct{}
	// If Done is not yet closed, Err returns nil.
	// If Done is closed, Err returns a non-nil error.
	// After Err returns a non-nil error, successive calls to Err return the same error.
	Err() error
}

A Breaker carries a cancellation signal to interrupt an action execution.

It is a subset of the built-in context and github.com/kamilsk/breaker interfaces.

type ErrorHandler

type ErrorHandler = func(error) bool

ErrorHandler defines a function that CheckError calls to determine whether it should make the next attempt or not. Returning true allows for the next attempt to be made. Returning false halts the retrying process and returns the last error returned by the called Action.

Jump to

Keyboard shortcuts

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