dispatcher

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Acceptor

type Acceptor interface {
	Accept(Keyer) error
	// Close is called when the Acceptor is no longer required.
	Close()
}

Acceptor is the interface that Lazy will route Keyers to.

type Keyer

type Keyer interface {
	Key() string
}

Keyer is the interface of items that Lazy accepts. Key should be a pure function, i.e. it should always return the same key for the same item, regardless of state.

type Lazy

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

func NewLazy

func NewLazy(
	factory func(string) (Acceptor, error), windowSize, keyCardinality int,
) *Lazy

NewLazy creates a lazy dispatcher. It accepts items, obtains a key for each item by calling its Key method, then sends it to the Acceptor for that key. If an Acceptor does not exist in Lazy yet, it is created using the factory function. Once an Acceptor has been idle for windowSize number of items, it is closed and removed from Lazy.

factory takes the key name and returns either the new Acceptor or an error. The error is returned to the caller of Lazy.Accept. keyCardinality is a guess for how many unique keys there are; it may be 0, in which case a default is used.

factory may be called from multiple goroutines at once. Also, Lazy may create and then close an Acceptor without ever using it.

func (*Lazy) Accept

func (ld *Lazy) Accept(item Keyer) error

Accept accepts a keyable item for dispatching. Any error from the acceptor is returned.

func (*Lazy) Close

func (ld *Lazy) Close()

Close closes the dispatcher. Accept must not be called after Close.

Jump to

Keyboard shortcuts

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