guard

package module
v0.0.0-...-09bed4a Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2017 License: MIT Imports: 5 Imported by: 0

README

guard

GoDoc Build Status

guard guards external resources (and your application) from your application.

Documentation

Overview

Package guard wraps context based process and manage it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	// NextInterval returns the next interval.
	NextInterval() time.Duration

	// Reset creates the clone of the current strategy with an initialized state.
	Reset() Backoff
}

Backoff is a strategy of backoff interval.

func NewConstantBackoff

func NewConstantBackoff(d time.Duration) Backoff

ConstantBackoff creates Backoff with a constant interval. NextInterval() always returns given parameter d.

func NewExponentialBackoff

func NewExponentialBackoff(options ...ExponentialBackoffOption) Backoff

ExponentialBackoff creates Backoff with an exponential backoff.

Let N be a retry count of the process, the value of NextInterval(N) is calculated by following formula.

NextInterval(N) = BaseInterval(N) * [1-RandomizationFactor, 1+RandomizationFactor)
BaseInterval(N) = min(BaseInterval(N-1) * Multiplier, MaxInterval)
BaseInterval(1) = min(InitialInterval, MaxInterval)

The default parameters.

InitialInterval:     200 (ms)
MaxInterval:         1 (min)
Multiplier:          2
RandomizationFactor: 0.2
Randomizer:          rand.New(rand.NewSource(time.Now().Unix()))

Example intervals.

+----+----------------------+----------------------+
| N  | BaseInterval(N) (ms) | NextInterval(N) (ms) |
+----+----------------------+----------------------+
|  1 |                  200 | [160, 240)           |
|  2 |                  400 | [320, 480)           |
|  3 |                  800 | [640, 960)           |
|  4 |                 1600 | [1280, 1920)         |
|  5 |                 3200 | [2560, 3840)         |
|  6 |                 6400 | [5120, 7680)         |
|  7 |                12800 | [10240, 15360)       |
|  8 |                25600 | [20480, 30720)       |
|  9 |                51200 | [40960, 61440)       |
| 10 |                60000 | [48000, 72000)       |
| 11 |                60000 | [48000, 72000)       |
+----+----------------------+----------------------+

Note: MaxInterval effects only the base interval. The actual interval may exceed MaxInterval depending on RandomizationFactor.

func NewNoBackoff

func NewNoBackoff() Backoff

NoBackoff creates Backoff without an interval. NextInterval() always returns 0.

type ExponentialBackoffOption

type ExponentialBackoffOption func(*exponentialBackoff)

ExponentialBackoffOption is the optional parameter for ExponentialBackoff.

func WithInitialInterval

func WithInitialInterval(d time.Duration) ExponentialBackoffOption

WithInitialInterval set the initial interval of ExponentialBackoff.

func WithMaxInterval

func WithMaxInterval(d time.Duration) ExponentialBackoffOption

WithMaxInterval set the maximum interval of ExponentialBackoff.

func WithMultiplier

func WithMultiplier(f float64) ExponentialBackoffOption

WithMultiplier set the multiplier of ExponentialBackoff.

func WithRandomizationFactor

func WithRandomizationFactor(f float64) ExponentialBackoffOption

WithRandomizationFactor set the randomization factor of ExponentialBackoff.

func WithRandomizer

func WithRandomizer(r Randomizer) ExponentialBackoffOption

WithRandomizer set the randomizer of ExponentialBackoff.

type Guard

type Guard interface {
	// Run runs the function with its own capability.
	Run(ctx context.Context, f func(context.Context) error) error
}

Guard is a process manager that runs and manages the process.

func Compose

func Compose(guards ...Guard) Guard

Compose composes multiple Guards ans create a single Guard.

type GuardFunc

type GuardFunc func(ctx context.Context, f func(context.Context) error) error

GuardFunc is an adapter to use a function as Guard.

func (GuardFunc) Run

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

Run implements Guard.

type Randomizer

type Randomizer interface {
	// Float64 returns random floating point number in [0.0,1.0).
	Float64() float64
}

Randomizer generates random numbers.

Directories

Path Synopsis
Package circuitbreaker provide a circuitbreaker pattern failure management.
Package circuitbreaker provide a circuitbreaker pattern failure management.
Package panicguard provide a panic-safe process management.
Package panicguard provide a panic-safe process management.
Package rate limit provides a manager to control the execution rate of the process.
Package rate limit provides a manager to control the execution rate of the process.
Package retry provides a failure management with retry for the process.
Package retry provides a failure management with retry for the process.
Package semaphore provides a semaphore that limits a number of concurrent processes.
Package semaphore provides a semaphore that limits a number of concurrent processes.

Jump to

Keyboard shortcuts

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