deadline

package
v1.1.994 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2018 License: MIT, MIT Imports: 2 Imported by: 0

README

deadline

Build Status GoDoc Code of Conduct

The deadline/timeout resiliency pattern for golang.

Creating a deadline takes one parameter: how long to wait.

dl := deadline.New(1 * time.Second)

err := dl.Run(func(stopper <-chan struct{}) error {
	// do something potentially slow
	// give up when the `stopper` channel is closed (indicating a time-out)
	return nil
})

switch err {
case deadline.ErrTimedOut:
	// execution took too long, oops
default:
	// some other error
}

Documentation

Overview

Package deadline implements the deadline (also known as "timeout") resiliency pattern for Go.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrTimedOut = errors.New("timed out waiting for function to finish")

ErrTimedOut is the error returned from Run when the deadline expires.

Functions

This section is empty.

Types

type Deadline

type Deadline struct {
	// contains filtered or unexported fields
}

Deadline implements the deadline/timeout resiliency pattern.

Example
dl := New(1 * time.Second)

err := dl.Run(func(stopper <-chan struct{}) error {
	// do something possibly slow
	// check stopper function and give up if timed out
	return nil
})

switch err {
case ErrTimedOut:
	// execution took too long, oops
default:
	// some other error
}
Output:

func New

func New(timeout time.Duration) *Deadline

New constructs a new Deadline with the given timeout.

func (*Deadline) Run

func (d *Deadline) Run(work func(<-chan struct{}) error) error

Run runs the given function, passing it a stopper channel. If the deadline passes before the function finishes executing, Run returns ErrTimeOut to the caller and closes the stopper channel so that the work function can attempt to exit gracefully. It does not (and cannot) simply kill the running function, so if it doesn't respect the stopper channel then it may keep running after the deadline passes. If the function finishes before the deadline, then the return value of the function is returned from Run.

Jump to

Keyboard shortcuts

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