Documentation ¶
Overview ¶
Package runner provides an API for generic goroutine scheduling. It is broken up into three concepts:
- Task: a unit of work to perform
- Worker: a goroutine dedicated to doing a specific Task
- Runner: manages the set of Workers, one per unique Task
An example of a Task and Worker pair would be a Task which describes an endpoint to poll for health. The Task would then be assigned to a Worker to perform the polling.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Runner ¶
type Runner[TaskType Task] struct { // contains filtered or unexported fields }
The Runner manages a set of running Workers based on an active set of tasks.
func New ¶
New creates a new Runner which manages workers for a given Task type. The newWorker function is called whenever a new Task is received that is not managed by any existing Worker.
func (*Runner[TaskType]) ApplyTasks ¶
ApplyTasks updates the Tasks tracked by the Runner to the slice specified by t. t should be the entire set of tasks that workers should be operating against. ApplyTasks will launch new Workers for new tasks and terminate previous Workers for tasks which are no longer found in tt.
ApplyTasks will block until Workers for stale Tasks have terminated. If the provided context is canceled, ApplyTasks will still finish synchronizing the set of Workers but will not wait for stale Workers to exit.
func (*Runner[TaskType]) Stop ¶
func (s *Runner[TaskType]) Stop()
Stop the Scheduler and all running Workers. Close blocks until all running Workers exit.
type Task ¶
type Task interface { // Hash should return a hash which represents this Task. Hash() uint64 // Equals should determine if two Tasks are equal. It is only called when two // Tasks have the same Hash. Equals(other Task) bool }
A Task is a payload that determines what a Worker should do. For example, a Task may be a struct including an address for a Worker to poll.
type Worker ¶
type Worker interface { // Run starts a Worker, blocking until the provided ctx is canceled or a // fatal error occurs. Run is guaranteed to be called exactly once for any // given Worker. Run(ctx context.Context) }
A Worker is a goroutine which performs business logic for a Task which is assigned to it. Each Worker is responsible for a single Task.