runner

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package runner provides an API for generic goroutine scheduling. It is broken up into three concepts:

  1. Task: a unit of work to perform
  2. Worker: a goroutine dedicated to doing a specific Task
  3. 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

func New[TaskType Task](newWorker func(t TaskType) Worker) *Runner[TaskType]

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

func (s *Runner[TaskType]) ApplyTasks(ctx context.Context, tt []TaskType) error

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.

func (*Runner[TaskType]) Tasks

func (s *Runner[TaskType]) Tasks() []TaskType

Tasks returns the current set of Tasks. Tasks are included even if their associated Worker has terminated.

func (*Runner[TaskType]) Workers

func (s *Runner[TaskType]) Workers() []Worker

Workers returns the current set of Workers. Workers are included even if they have terminated.

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.

Jump to

Keyboard shortcuts

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