Categorygithub.com/bsm/workhorse
repositorypackage
0.1.0
Repository: https://github.com/bsm/workhorse.git
Documentation: pkg.go.dev

# README

Workhorse

Build Status GoDoc License

A simple worker abstraction on top of errgroup with custom middlewares.

Examples

import (
	"context"
	"fmt"
	"sync/atomic"

	"github.com/bsm/workhorse"
)

func main() {
	// define root context
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	var count uint32

	// init a worker
	w := workhorse.New(ctx)

	// schedule task "one"
	w.Go("one", func(_ context.Context) error {
		for i := 0; i < 1000; i++ {
			atomic.AddUint32(&count, 1)
		}
		return nil
	})

	// schedule task "two"
	w.Go("two", func(_ context.Context) error {
		for i := 0; i < 1000; i++ {
			atomic.AddUint32(&count, 2)
		}
		return nil
	})

	// wait for both tasks to complete
	if err := w.Wait(); err != nil {
		panic(err)
	}

	fmt.Println(count)
	// Output:
	// 3000}

Documentation

Full documentation is available on GoDoc