async

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyStarted = errors.New("already_started")

Functions

This section is empty.

Types

type Func added in v1.2.0

type Func func(<-chan bool) // Async function run by the pool

type Group added in v1.1.0

type Group[T any] interface {
	OnStart(PoolContext[T])         // Called upon pool startup, use PoolContext.Run to register a goroutine
	Handle(context.Context, T) bool // Try to handle the given job, returns true if the job was handled, false otherwise
}

Async group of runners which can actually handle jobs.

func GroupFunc added in v1.2.0

func GroupFunc[T any](size int, handlerFunc HandlerFunc[T], canHandle SelectorFunc[T]) Group[T]

Builds a new runners group which will process jobs satisfying the canHandle function.

type HandlerFunc added in v1.1.0

type HandlerFunc[T any] func(context.Context, T) error // Process a single job

type PollerFunc added in v1.2.0

type PollerFunc[T any] func(context.Context) ([]T, error)

type Pool added in v1.1.0

type Pool[T any] interface {
	Start() error // Start the pool
	Stop()        // Wait for the current pool to complete running jobs and returns
}

Represents an asynchronous pool which can process multiple jobs simultaneously.

func NewPool added in v1.1.0

func NewPool[T any](
	logger log.Logger,
	puller Puller[T],
	groups ...Group[T],
) Pool[T]

Builds a new pool with a specific Puller to feed registered groups with actual jobs to process.

The idea behind groups is that some jobs need more time to complete and I don't want to hold back the other ones such as (in the future), sending emails, checking stuff, etc.

type PoolContext added in v1.2.0

type PoolContext[T any] interface {
	Queue(context.Context, ...T) // Queue one or multiple jobs to be processed
	Logger() log.Logger          // Returns the logger used by the pool
	Run(Func)                    // Launch a function in a new goroutine which MUST listen for the done signal and exit when it receives it
}

Pool context as seen by puller, groups and internal stuff.

type Puller added in v1.2.0

type Puller[T any] interface {
	OnStart(PoolContext[T]) // Called upon pool startup, use PoolContext.Run to register a goroutine
}

Puller interface used to retrieve jobs to process from somewhere.

func Poll added in v1.2.0

func Poll[T any](interval time.Duration, pollFn PollerFunc[T]) Puller[T]

Simple puller wich will call the given function at the given interval.

type SelectorFunc added in v1.2.0

type SelectorFunc[T any] func(context.Context, T) bool // Returns true if the group can handle the job

Jump to

Keyboard shortcuts

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