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.
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 (*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) 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 ¶
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 (*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 ¶
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.