buzz

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2024 License: MIT Imports: 5 Imported by: 0

README

buzz

GoDoc Test

Robust workers for Go

Overview

The concept defined in this package is intended to be quite configurable. Using middleware can be a very powerful design. You may want to define middleware to recover from panics, inject content into the context (like a logger, etc), perform setup or teardown steps, or any number of other things that you can think of!

Documentation

Overview

Example
package main

import (
	"context"
	"log"

	"github.com/thenorthnate/buzz"
)

type logTask struct{}

func (t *logTask) Do(ctx context.Context) error {
	log.Println("message here")
	return nil
}

func main() {
	// This defines some middleware that logs before and after the task runs
	logger := func(ctx context.Context, chain *buzz.CallChain) error {
		// This happens before the task runs
		log.Println("Starting!")
		// This call runs the rest of the middleware and the task
		err := chain.Next(ctx)
		// This runs after the task has completed
		log.Printf("Finished with err=[%v]\n", err)
		return err
	}
	hive := buzz.New()
	worker := buzz.NewWorker(&logTask{}).Use(logger)
	hive.Submit(worker)
	// Some time later... during shutdown
	hive.StopAll()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallChain

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

CallChain represents a linked list that provides the mechanism through which middleware can be implemented.

func NewTestCallChain added in v0.4.3

func NewTestCallChain(exec MiddleFunc) *CallChain

NewTestCallChain creates a new CallChain that simply executes the given MiddleFunc. The provided MiddleFunc will recieve a nil CallChain. This function is a utility to make it easy to test your own middleware.

func (*CallChain) Next

func (chain *CallChain) Next(ctx context.Context) error

Next is used to allow the chain to proceed processing. When it returns, you can assume that all middleware as well as the task itself executed and returned.

type Hive

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

Hive contains all the workers and synchronizes a graceful shutdown of all the workers.

func New

func New() *Hive

New initializes a new *Hive.

func (*Hive) StopAll

func (hive *Hive) StopAll()

StopAll should only be used when you are completely done with the hive. Internal channels are closed and all workers are shutdown.

func (*Hive) Submit

func (hive *Hive) Submit(workers ...*Worker)

Submit starts the workers running and adds them to the hive.

func (*Hive) Use added in v0.2.0

func (hive *Hive) Use(middleFunc ...MiddleFunc)

Use adds the given MiddleFunc's to the hive as default functions. They will get added to each worker that is added to the hive. They are placed as the earliest middleware in the chain, in the same order they are added here. So, if you add A, B, C to the hive, and add a worker that already has D, and E middleware, you will end up with a middleware chain on that worker equivalent to A, B, C, D, E. From that point, it's important to note that any closures that are added as middleware to the hive may behave in unexpected ways since each worker will get the same closure (unless that is your intent!).

type MiddleFunc

type MiddleFunc func(ctx context.Context, chain *CallChain) error

MiddleFunc defines the type of any middleware that can be used in the hive.

type Task

type Task interface {
	// Do should perform the desired work of the Worker. If the context is cancelled, it should
	// return an error. If no error is returned, [Do] is called repeatedly in a loop.
	Do(ctx context.Context) error
}

Task represents the thing that you want accomplished.

type Worker

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

Worker wraps your task with additional context to provide a robust operational environment.

func NewWorker

func NewWorker(task Task) *Worker

NewWorker wraps the task and returns a worker that can be submitted to the hive.

func (*Worker) Stop

func (w *Worker) Stop()

Stop issues a command to the hive to stop this worker from running and remove it.

func (*Worker) Tick

func (w *Worker) Tick(tick time.Duration) *Worker

Tick provides a mechanism through which you can schedule your task to get run on a regular interval. By default the tick time is zero meaning that the task is called repeatedly as fast as the computer executes it.

func (*Worker) Use

func (w *Worker) Use(middleware ...MiddleFunc) *Worker

Use adds the given middleware functions to the Worker.

Jump to

Keyboard shortcuts

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