inorder

package module
v0.0.0-...-be4efe9 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2016 License: MIT Imports: 3 Imported by: 0

README

In Order

Maintains ordering of asynchronous tasks.

Usage

type DummyTask struct {
	block        *inorder.Block
	ExpectedName string
	Name         string
}

func (dt *DummyTask) Wait() chan bool {
	return dt.block.Wait()
}

func NewDummyTask(d time.Duration, name string) *DummyTask {
	t := &DummyTask{
		ExpectedName: name,
		block:        inorder.NewBlock(),
	}
	go func() {
		<-time.NewTimer(d).C
		t.Name = name
		t.block.Done()
	}()
	return t
}

func ExampleUse() {
	a := NewDummyTask(20*time.Millisecond, "a")
	b := NewDummyTask(10*time.Millisecond, "b")
	c := NewDummyTask(15*time.Millisecond, "c")

	inOrder := inorder.NewInOrder(50 * time.Millisecond)
	inOrder.Enqueue(a)
	inOrder.Enqueue(b)
	inOrder.Enqueue(c)

	fmt.Println("first:", (<-inOrder.Done).Task.(*DummyTask).Name)
	fmt.Println("second:", (<-inOrder.Done).Task.(*DummyTask).Name)
	fmt.Println("last:", (<-inOrder.Done).Task.(*DummyTask).Name)
	// Output:
	// first: a
	// second: b
	// last: c
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTaskTimedOut the task didn't complete within the specified time limit
	ErrTaskTimedOut = errors.New("task timed out")
)

Functions

This section is empty.

Types

type Block

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

Block provides a wait mechanic based on whether a task is done

func NewBlock

func NewBlock() *Block

NewBlock creates a new block

func (*Block) Done

func (b *Block) Done()

Done sets the task as done

func (*Block) IsDone

func (b *Block) IsDone() (done bool)

IsDone checks if the task is done

func (*Block) Wait

func (b *Block) Wait() chan bool

Wait returns a channel that is closed when the task is done

type InOrder

type InOrder struct {
	Timeout time.Duration
	Done    chan *Result
	// contains filtered or unexported fields
}

InOrder maintains the order of enqueued jobs

func NewInOrder

func NewInOrder(timeout time.Duration) *InOrder

NewInOrder creates a new orderer

func (*InOrder) Enqueue

func (in *InOrder) Enqueue(task Task)

Enqueue a new task

type Result

type Result struct {
	Error error
	Task  Task
	// contains filtered or unexported fields
}

Result is the result of a task

func (*Result) IsDone

func (r *Result) IsDone() (bool, error)

func (*Result) SetError

func (r *Result) SetError(err error)

type Task

type Task interface {
	Wait() chan bool
	IsDone() bool
}

Task is something that we can wait for

Jump to

Keyboard shortcuts

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