deathrow

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2022 License: MIT Imports: 5 Imported by: 0

README

codecov

DeathRow

DeathRow is a time-based priority queue, which only pops items from Prison after they reached their deadline.

Why?

Slightly more efficient checking of TTLs in caches or work queues

Documentation

Index

Constants

View Source
const DEFAULT_POPPER_RESOLUTION = 100 * time.Millisecond

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item[K comparable] interface {
	ID() K
	Deadline() time.Time
	ShouldExecute() bool
	Prolong(ttl time.Duration)

	Index() int
	SetIndex(int)
}

func NewItem

func NewItem[K comparable](id K, ttl time.Duration) Item[K]

type Prison

type Prison[K comparable] struct {
	// contains filtered or unexported fields
}

Prison takes care of its prisoners (items) and their executions (timeouts). Prison is the main structure in this package. It contains a priority queue based on the deadlines of its items as well as backreferences to the items in the queue, which makes accessing the dead items as well as specific items easy and efficient

func NewPrison

func NewPrison[K comparable]() *Prison[K]

NewPrison creates new Prison without any items

func (*Prison[K]) Drop

func (p *Prison[K]) Drop(itemID K)

Drop removes an item from the Prison. It doesn't have to be expired

func (*Prison[K]) Pop

func (p *Prison[K]) Pop() (items []Item[K])

Pop pops all expired items in Prison. If there are no such items, it returns empty slice

func (*Prison[K]) Popper

func (p *Prison[K]) Popper(ctx context.Context) <-chan Item[K]

Popper is the same as PopperWithResolution but with the default resolution.

func (*Prison[K]) PopperWithResolution

func (p *Prison[K]) PopperWithResolution(ctx context.Context, resolution time.Duration) <-chan Item[K]

PopperWithResolution returns a new channel into which newly expired popped items are periodically (every `resolution`) pushed. This loop ends when `ctx` is cancelled

func (*Prison[K]) Push

func (p *Prison[K]) Push(itemID K, ttl time.Duration)

Push adds new item to the Prison. If the item already exists, its TTL is prolonged by `ttl`

Jump to

Keyboard shortcuts

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