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
- Variables
- func Empty(q EventQueue) bool
- func EnqueueMany(ctx context.Context, q EventQueue, actions []Action)
- func RemoveActions(ctx context.Context, p ActionPlanner, actions []PlannedAction)
- func RunAll(ctx context.Context, s Scheduler)
- func RunMany(ctx context.Context, s Scheduler, n int) bool
- type Action
- type ActionPlanner
- type AwareActionPlanner
- type AwareScheduler
- type BasicAction
- type ChanQueue
- type EventQueue
- type EventQueueDequeueAll
- type EventQueueDequeueMany
- type EventQueueEnqueueMany
- type EventQueueWithEmpty
- type FuncAction
- type IntAction
- type MultiActionPlanner
- type PlannedAction
- type RunAllScheduler
- type RunManyScheduler
- type Scheduler
- type SimplePlanner
- func (p *SimplePlanner) NextActionTime(context.Context) time.Time
- func (p *SimplePlanner) PopOverdueActions(ctx context.Context) []Action
- func (p *SimplePlanner) RemoveAction(ctx context.Context, pa PlannedAction) bool
- func (p *SimplePlanner) ScheduleAction(ctx context.Context, t time.Time, a Action) PlannedAction
- type SimpleScheduler
- func (s *SimpleScheduler) Clock() clock.Clock
- func (s *SimpleScheduler) EnqueueAction(ctx context.Context, a Action)
- func (s *SimpleScheduler) NextActionTime(ctx context.Context) time.Time
- func (s *SimpleScheduler) RemovePlannedAction(ctx context.Context, a PlannedAction) bool
- func (s *SimpleScheduler) RunOne(ctx context.Context) bool
- func (s *SimpleScheduler) ScheduleAction(ctx context.Context, t time.Time, a Action) PlannedAction
Constants ¶
const DefaultChanqueueCapacity = 1024
Variables ¶
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.
Types ¶
type Action ¶
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 ¶
A BasicAction is the default Action used for event scheduling in the Kademlia implementation.
type ChanQueue ¶
type ChanQueue struct {
// contains filtered or unexported fields
}
ChanQueue is a trivial queue implementation using a channel
func (*ChanQueue) Dequeue ¶
Dequeue reads the next element from the queue, note that this operation is blocking
type EventQueue ¶
type EventQueueDequeueAll ¶
type EventQueueDequeueMany ¶
type EventQueueEnqueueMany ¶
type EventQueueEnqueueMany interface { EventQueue EnqueueMany(context.Context, []Action) }
type EventQueueWithEmpty ¶
type EventQueueWithEmpty interface { EventQueue Empty() bool }
type FuncAction ¶
FuncAction is an action that does nothing but tracks whether it was "run" yet. It is used to test the scheduler.
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
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 ¶
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.