event

package
v0.0.0-...-38a2eed Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2025 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Overview

Package event provides an abstraction for single worker multi threaded applications. Some applications are multi threaded by design (e.g Kademlia lookup), but having a sequential execution brings many benefits such as deterministic testing, easier debugging, sequential tracing, and sometimes even increased performance.

Index

Constants

View Source
const DefaultChanqueueCapacity = 1024

Variables

View Source
var MaxTime = time.Unix(1<<63-62135596801, 999999999)

MaxTime is the maximum time.Time value

Functions

func Empty

func Empty(q EventQueue) bool

func EnqueueMany

func EnqueueMany(ctx context.Context, q EventQueue, actions []Action)

func RemoveActions

func RemoveActions(ctx context.Context, p ActionPlanner, actions []PlannedAction)

RemoveActions removes multiple actions from the planner.

func RunAll

func RunAll(ctx context.Context, s Scheduler)

RunAll runs all actions in the scheduler's queue and overdue actions from the planner

func RunMany

func RunMany(ctx context.Context, s Scheduler, n int) bool

RunMany runs n actions on the scheduler, returning true if all actions were run, or false if there were less than n actions to run

Types

type Action

type Action interface {
	Run(context.Context)
}

Action is an interface for an action that can be run. It is used as unit by the scheduler (event queue + planner)

func DequeueAll

func DequeueAll(ctx context.Context, q EventQueue) []Action

func DequeueMany

func DequeueMany(ctx context.Context, q EventQueue, n int) []Action

type ActionPlanner

type ActionPlanner interface {
	// ScheduleAction schedules an action to run at a specific time
	ScheduleAction(context.Context, time.Time, Action) PlannedAction
	// RemoveAction removes an action from the planner
	RemoveAction(context.Context, PlannedAction) bool

	// PopOverdueActions returns all actions that are overdue and removes them
	// from the planner
	PopOverdueActions(context.Context) []Action
}

ActionPlanner is an interface for scheduling actions at a specific time.

type AwareActionPlanner

type AwareActionPlanner interface {
	ActionPlanner

	// NextActionTime returns the time of the next action that will be
	// scheduled. If there are no actions scheduled, it returns MaxTime.
	NextActionTime(context.Context) time.Time
}

AwareActionPlanner is an interface for scheduling actions at a specific time and knowing when the next action will be scheduled.

type AwareScheduler

type AwareScheduler interface {
	Scheduler

	// NextActionTime returns the time of the next action in the scheduler's
	// queue or util.MaxTime if the queue is empty
	NextActionTime(context.Context) time.Time
}

AwareScheduler is a scheduler that can return the time of the next scheduled

type BasicAction

type BasicAction func(context.Context)

A BasicAction is the default Action used for event scheduling in the Kademlia implementation.

func (BasicAction) Run

func (a BasicAction) Run(ctx context.Context)

Run executes the action

type ChanQueue

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

ChanQueue is a trivial queue implementation using a channel

func NewChanQueue

func NewChanQueue(capacity int) *ChanQueue

NewChanQueue creates a new queue

func (*ChanQueue) Close

func (q *ChanQueue) Close()

func (*ChanQueue) Dequeue

func (q *ChanQueue) Dequeue(ctx context.Context) Action

Dequeue reads the next element from the queue, note that this operation is blocking

func (*ChanQueue) Empty

func (q *ChanQueue) Empty() bool

Empty returns true if the queue is empty

func (*ChanQueue) Enqueue

func (q *ChanQueue) Enqueue(ctx context.Context, e Action)

Enqueue adds an element to the queue

func (*ChanQueue) Size

func (q *ChanQueue) Size() uint

type EventQueue

type EventQueue interface {
	Enqueue(context.Context, Action)
	Dequeue(context.Context) Action

	Size() uint
	Close()
}

type EventQueueDequeueAll

type EventQueueDequeueAll interface {
	DequeueAll(context.Context) []Action
}

type EventQueueDequeueMany

type EventQueueDequeueMany interface {
	DequeueMany(context.Context, int) []Action
}

type EventQueueEnqueueMany

type EventQueueEnqueueMany interface {
	EventQueue
	EnqueueMany(context.Context, []Action)
}

type EventQueueWithEmpty

type EventQueueWithEmpty interface {
	EventQueue
	Empty() bool
}

type FuncAction

type FuncAction struct {
	Ran bool
	Int int
}

FuncAction is an action that does nothing but tracks whether it was "run" yet. It is used to test the scheduler.

func NewFuncAction

func NewFuncAction(i int) *FuncAction

NewFuncAction returns a new FuncAction

func (*FuncAction) Run

func (a *FuncAction) Run(context.Context)

Run sets Ran to true

type IntAction

type IntAction int

IntAction is an action that does nothing but is used to test the scheduler. An IntAction is equal to another IntAction if they have the same integer

func (IntAction) Run

func (a IntAction) Run(context.Context)

Run does nothing

type MultiActionPlanner

type MultiActionPlanner interface {
	ActionPlanner

	// ScheduleActions schedules multiple actions at specific times
	ScheduleActions(context.Context, []time.Time, []Action) []PlannedAction
	// RemoveActions removes multiple actions from the planner
	RemoveActions(context.Context, []PlannedAction)
}

MultiActionPlanner is an interface for scheduling multiple actions at specific times.

type PlannedAction

type PlannedAction interface {
	// Time returns the time at which the action is scheduled to run
	Time() time.Time
	// Action returns the action that is scheduled to run
	Action() Action
}

PlannedAction is an interface for actions that are scheduled to run at a specific time.

func ScheduleActionIn

func ScheduleActionIn(ctx context.Context, s Scheduler, d time.Duration, a Action) PlannedAction

ScheduleActionIn schedules an action to run after a delay

func ScheduleActions

func ScheduleActions(ctx context.Context, p ActionPlanner,
	times []time.Time, actions []Action,
) []PlannedAction

ScheduleActions schedules multiple actions at specific times using a planner.

type RunAllScheduler

type RunAllScheduler interface {
	Scheduler

	// RunAll runs all actions in the scheduler's queue
	RunAll(context.Context)
}

RunAllScheduler is a scheduler that can run all actions in its queue

type RunManyScheduler

type RunManyScheduler interface {
	Scheduler

	// RunMany runs n actions on the scheduler, returning true if all actions
	// were run, or false if there were less than n actions to run
	RunMany(context.Context, int) bool
}

RunManyScheduler is a scheduler that can run multiple actions at once

type Scheduler

type Scheduler interface {
	// Now returns the time of the scheduler's clock
	Clock() clock.Clock

	// EnqueueAction enqueues an action to run as soon as possible
	EnqueueAction(context.Context, Action)
	// ScheduleAction schedules an action to run at a specific time
	ScheduleAction(context.Context, time.Time, Action) PlannedAction
	// RemovePlannedAction removes an action from the scheduler planned actions
	// (not from the queue), does nothing if the action is not in the planner
	RemovePlannedAction(context.Context, PlannedAction) bool

	// RunOne runs one action from the scheduler's queue, returning true if an
	// action was run, false if the queue was empty
	RunOne(context.Context) bool
}

Scheduler is an interface for scheduling actions to run as soon as possible or at a specific time

type SimplePlanner

type SimplePlanner struct {
	Clock clock.Clock

	NextAction *simpleTimedAction
	// contains filtered or unexported fields
}

func NewSimplePlanner

func NewSimplePlanner(clk clock.Clock) *SimplePlanner

func (*SimplePlanner) NextActionTime

func (p *SimplePlanner) NextActionTime(context.Context) time.Time

func (*SimplePlanner) PopOverdueActions

func (p *SimplePlanner) PopOverdueActions(ctx context.Context) []Action

func (*SimplePlanner) RemoveAction

func (p *SimplePlanner) RemoveAction(ctx context.Context, pa PlannedAction) bool

func (*SimplePlanner) ScheduleAction

func (p *SimplePlanner) ScheduleAction(ctx context.Context, t time.Time, a Action) PlannedAction

type SimpleScheduler

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

SimpleScheduler is a simple implementation of the Scheduler interface. It uses a simple planner and a channel-based queue.

func NewSimpleScheduler

func NewSimpleScheduler(clk clock.Clock) *SimpleScheduler

NewSimpleScheduler creates a new SimpleScheduler.

func (*SimpleScheduler) Clock

func (s *SimpleScheduler) Clock() clock.Clock

Now returns the scheduler's current time.

func (*SimpleScheduler) EnqueueAction

func (s *SimpleScheduler) EnqueueAction(ctx context.Context, a Action)

EnqueueAction enqueues an action to be run as soon as possible.

func (*SimpleScheduler) NextActionTime

func (s *SimpleScheduler) NextActionTime(ctx context.Context) time.Time

NextActionTime returns the time of the next action to run, or the current time if there are actions to be run in the queue, or util.MaxTime if there are no scheduled to run.

func (*SimpleScheduler) RemovePlannedAction

func (s *SimpleScheduler) RemovePlannedAction(ctx context.Context, a PlannedAction) bool

RemovePlannedAction removes an action from the scheduler planned actions (not from the queue), does nothing if the action is not in the planner

func (*SimpleScheduler) RunOne

func (s *SimpleScheduler) RunOne(ctx context.Context) bool

RunOne runs one action from the scheduler's queue, returning true if an action was run, false if the queue was empty.

func (*SimpleScheduler) ScheduleAction

func (s *SimpleScheduler) ScheduleAction(ctx context.Context, t time.Time,
	a Action,
) PlannedAction

ScheduleAction schedules an action to run at a specific time.

Jump to

Keyboard shortcuts

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