toutloop

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

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

Go to latest
Published: Sep 24, 2019 License: BSD-3-Clause Imports: 4 Imported by: 0

README

A tight running loop that tracks timeouts at scale.

CircleCI GoDoc Go Report Card codecov

The loop uses the container/heap package to track timeouts and deliver them as they expire. User can add, reschedule or remove timeouts. The expired timeouts can be received on the ToutLoop.C channel.

Usage

Import the package using:

import "github.com/anekkanti/toutloop"

Here an example that creates a timeout and receives it.


type tjob struct {
	name string
}

func main() {
	tloop := New(0 /*recieveChanBufferSize*/ )
	tloop.Run()

	j1 := &tjob{name: "j1"}
	err := tloop.Add(j1.name, j1, time.Millisecond*300)
	if err != nil {
		panic(err)
	}

	err = tloop.Reschedule(j1.name, time.Millisecond*400)
	if err != nil {
		panic(err)
	}

	for j := range tloop.C {
		if j.(*tjob) == j1 {
			break
		}
	}
	tloop.Stop()
}

Performance

chart

The loop performs with lower then millisecond delay for events when its scheduling upto 10K events per second. After which the performance degrades and the toutloop can't seem to keep up with expiring timeouts.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ToutLoop

type ToutLoop struct {
	C chan interface{}
	// contains filtered or unexported fields
}

ToutLoop or the timeout loop. The loop uses a heap to track and dispatches events when their timeout's expire Listen to C to receive events

func New

func New(recieveChanBuffer int) *ToutLoop

New returns a new timeout looop

func (*ToutLoop) Add

func (e *ToutLoop) Add(id string, object interface{}, after time.Duration) error

Add object with given id to be returned after given time

func (*ToutLoop) Get

func (e *ToutLoop) Get(id string) (interface{}, error)

Get the object with id if it exists in the loop

func (*ToutLoop) Remove

func (e *ToutLoop) Remove(id string) error

Remove the object with the given id from the loop

func (*ToutLoop) Reschedule

func (e *ToutLoop) Reschedule(id string, after time.Duration) error

Reschedule the object with the given id

func (*ToutLoop) Run

func (e *ToutLoop) Run()

Run the timeout loop

func (*ToutLoop) Stop

func (e *ToutLoop) Stop()

Stop the event loop

Jump to

Keyboard shortcuts

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