retrool

package module
v0.0.0-...-58ca56b Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2022 License: MIT Imports: 1 Imported by: 0

README

Retrool

Simple tool to repeat (or retry) some actions after some time for GoLang.

The tool determines whether an action should be retried by the action's return value

GoDoc Release License

Content

Installation

To install the package just run:

go get -u github.com/makssof/retrool

Usage

import "github.com/makssof/retrool"

func main() {
    rand.Seed(time.Now().UnixNano())

    // tryOptions := retrool.DefaultTryOptions
    tryOptions := &retrool.TryOptions{
        StartInterval:        1,
        Addition:             1,
        AdditionCoefficient:  0.5,
        MaxTries:             10, 
        FailureDecisionMaker: retrool.DefaultDecisionMaker,
    }

    var n int

    retrool.Try(tryOptions, func (i int) bool {
        n = rand.Intn(101)

        return n < 10
    })
}

Contribution

The tool is open-sourced under the MIT license.

If you will find some error, want to add something or ask a question - feel free to create an issue and/or make a pull request.

Any contribution is welcome.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultTryOptions = &TryOptions{
		StartInterval:        1,
		Addition:             1,
		AdditionCoefficient:  0.5,
		MaxTries:             5,
		FailureDecisionMaker: DefaultDecisionMaker,
	}
)

Functions

func DefaultDecisionMaker

func DefaultDecisionMaker(res interface{}) bool

DefaultDecisionMaker is the default function to detect failed attempt It expects a boolean value as input and returns true if the input value is true

func Try

func Try(opts *TryOptions, fnc func(int) bool) bool

The Try behavior is described via parameters. See TryOptions

Types

type TryOptions

type TryOptions struct {
	// StartInterval is the initial delay after the first failed
	// attempt (if the first attempt fails). The base number to which
	// Addition will be added with multiplication by AdditionCoefficient.
	StartInterval float32

	// With each new attempt, Addition (multiplied by AdditionCoefficient) will be added to StartInterval
	Addition float32

	// With each new attempt, Addition multiplied by AdditionCoefficient will be added to StartInterval.
	AdditionCoefficient float32

	// How many attempts to make before it is considered completely failed
	MaxTries int

	// The FailureDecisionMaker function takes as input the result of executing the passed function
	// and determines whether the result is a failure. If yes, it returns false, otherwise true.
	// If this function returns false, the attempts will continue (if the counter does not exceed MaxTries)
	//
	// There is a default function DefaultDecisionMaker. If no other function is specified, this one will be used.
	// It expects a boolean value as input and returns true if the input value is true
	FailureDecisionMaker func(interface{}) bool
}

The TryOptions structure describes how attempts should be made to perform an action. There are default options: DefaultTryOptions

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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