taker

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

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

Go to latest
Published: Nov 15, 2016 License: MIT Imports: 4 Imported by: 0

README

👷 taker

Build Status Go Documentation

taker provides simple, modular functions for working with asynchronous Golang code.

Why?

Do you often find yourself writing repetitive error handling checks in Go?

if err := wakeUp(); err != nil {
  return err
}
if err := boilWater(); err != nil {
  return err
}
if err := makeSandwich(); err != nil {
  return err
}
if err := brushTeeth(); err != nil {
  return err
}

Doesn't this kind of code just make you want to use promises, try / catch or something like async?

Fear no more! taker allows you to get rid of the redundant error handling by providing a set of common utility functions used for dealing with asynchronous code.

In the above example, we could wrap our imperative functions into smaller tasks and chain them using Series (each of the above functions would return a task):

return taker.Series(
  taker.Wrap(wakeUp),
  taker.Wrap(boilWater),
  taker.Wrap(makeSandwich),
  taker.Wrap(brushTeeth),
)

taker is based on the idea of "tasks". A task has to implement a Run() error method. Instead of implementing our own tasks, we can also wrap preexisting, functions into tasks using taker.Wrap(myFunc) where myFunc implements func() error.

For detailed docs, see Godocs.

Install

Using go get:

$ go get github.com/alexanderGugel/taker

Dependencies

taker currently uses govendor for vendoring dependencies.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWhilst

func DoWhilst(task Task, test Test) error

DoWhilst runs the task as long as the test passes. This is the post-check version of whilst. This means the task will be run at least once.

func ForceSeries

func ForceSeries(tasks ...Task) error

ForceSeries runs the supplied tasks in series. All tasks will be run, regardless of any errors. If there is an error, it will be of type *multierror.Error.

func Forever

func Forever(task Task) error

Forever runs the passed in task until it returns an error. If it never returns an error, it will be run indefinitely.

func Parallel

func Parallel(tasks ...Task) error

Parallel runs the supplied tasks in parallel. The function returns once all tasks have been run. If there is an error, it will be of type *multierror.Error.

func ParallelLimit

func ParallelLimit(limit int, tasks ...Task) error

ParallelLimit runs the supplied tasks in parallel. The function returns once all tasks have been run. If there is an error, it will be of type *multierror.Error.

func Race

func Race(tasks ...Task) error

Race runs the supplied tasks at the same time. If any of the tasks returns an error, the error will be returned and subsequent errors of the remaining tasks will be ignored. Equivalent to Promise.race.

func Retry

func Retry(task Task, max int) error

Retry runs the passed in task until it succeeds, but a total maximum of max times. In case of constantly failing tasks, the last returned error will be returned.

func Series

func Series(tasks ...Task) error

Series runs the supplied tasks in series. If a task returns an error, the remaining tasks won't be run and the error returned.

func Timeout

func Timeout(t Task, limit time.Duration) error

Timeout sets a time limit on an asynchronous task. If the task takes longer than the specified duration, an error of type TimeoutError will be returned.

func Times

func Times(n int, task Task) error

Times runs the passed in task n times. If the task returns an error, the execution will be stopped and the error returned.

func Until

func Until(test Test, task Task) error

Until is the inverse of While. It runs the specified task until it returns true or the task fails. If the task returns an error, the execution will be stopped and the error returned.

func Whilst

func Whilst(test Test, task Task) error

Whilst repeatedly runs the task as long as test returns true and the task succeeds. If the task returns an error, the execution will be stopped and the error returned.

Types

type InvalidLimitError

type InvalidLimitError struct {
	Limit int
}

InvalidLimitError represents a runtime error that occurs when passing an invalid limit to ParallelLimit.

func (*InvalidLimitError) Error

func (e *InvalidLimitError) Error() string

Error returns an error string.

type Semaphore

type Semaphore chan empty

Semaphore implements a semaphore using an empty channel with specified buffer size.

func NewSemaphore

func NewSemaphore(n int) Semaphore

NewSemaphore creates a new semaphore of the specified capacity.

func (Semaphore) Down

func (s Semaphore) Down(n int)

Down releases n resources.

func (Semaphore) Up

func (s Semaphore) Up(n int)

Up acquires n resources.

type Task

type Task interface {
	// Run synchronously executes the task.
	// In order to simulate multiple return values while guaranteeing
	// type-safety, amend the interface with additional properties.
	Run() error
}

Task represents a job that can be run. You are encouraged to amend this interface accordingly in order to reference results of the task.

func Wrap

func Wrap(run func() error) Task

Wrap creates a new task from a run function. This is useful in case you want to wrap an arbitrary function into a task.

type Test

type Test func() bool

Test is a truth test that will be performed before each execution of the underlying task.

func (Test) Negate

func (t Test) Negate() Test

Negate returns the inverse of the underlying test function. If the test returns true, Negate would return false and vice-versa.

type TimeoutError

type TimeoutError struct {
	Limit time.Duration
}

TimeoutError is a description of a timeout error that will be returned once a specified time duration has been exceeded.

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

Error returns an error string.

type TimeoutTask

type TimeoutTask struct {
	// Limit is the duration that the task is allowed to take.
	Limit time.Duration
}

TimeoutTask represents a task that will return a TimeoutError once a specified time limit has been exceeded.

func (*TimeoutTask) Run

func (t *TimeoutTask) Run() error

Run starts the execution of the task and returns an error once the time limit has passed.

Jump to

Keyboard shortcuts

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