Go to file
Marvin Preuss 29e4822b86
All checks were successful
continuous-integration/drone/push Build is passing
ci: slack depends_on
2021-09-27 13:45:56 +02:00
githooks docs: readme 2021-09-27 10:46:34 +02:00
vendor repo: adds goreadme 2021-09-24 18:29:05 +02:00
.drone.yml ci: slack depends_on 2021-09-27 13:45:56 +02:00
.gitignore repo: ignoring dist 2021-09-24 17:37:56 +02:00
.goreleaser.yml build: ignore docs commits 2021-09-27 13:33:36 +02:00
example_test.go Start doesnt take context no more. also use NewJob for creating workload 2021-09-27 10:44:58 +02:00
go.mod repo: adds goreadme 2021-09-24 18:29:05 +02:00
go.sum repo: adds goreadme 2021-09-24 18:29:05 +02:00
LICENSE Initial commit 2021-09-24 14:19:51 +02:00
Makefile docs: readme 2021-09-27 10:46:34 +02:00
README.gif docs: readme 2021-09-27 10:46:34 +02:00
README.md docs: readme 2021-09-27 11:06:57 +02:00
tools.go docs: readme 2021-09-27 10:46:34 +02:00
workgroups_test.go Start doesnt take context no more. also use NewJob for creating workload 2021-09-27 10:44:58 +02:00
workgroups.go docs: readme 2021-09-27 11:06:44 +02:00

workgroups

Package workgroups is a little helper for creating workers with the help of sync.Errgroup.

build coverage report

readme

Variables

ErrInWorker is an error that gets returned if there is a error in the work function.

var ErrInWorker = errors.New("received error in worker")

Types

type Dispatcher

type Dispatcher struct { ... }

Dispatcher carries the job queue, the errgroup and the number of workers to start.

func (*Dispatcher) Append

func (d *Dispatcher) Append(job Job)

Append adds a job to the work queue.

func (*Dispatcher) Close

func (d *Dispatcher) Close()

Close closes the queue channel.

func (*Dispatcher) Start

func (d *Dispatcher) Start()

Start starts the configured number of workers and waits for jobs.

func (*Dispatcher) Wait

func (d *Dispatcher) Wait() error

Wait for all jobs to finnish.

type Job

type Job struct { ... }

Job carries a job with everything it needs. I know know that contexts shouldnt be stored in a struct. Here is an exception, because its a short living object. The context is only used as argument for the Work function. Please use the NewJob function to get around this context in struct shenanigans.

type Work

type Work func(ctx context.Context) error

Work is a type that defines worker work.

Examples

package main

import (
	"context"
	"fmt"
	"go.xsfx.dev/workgroups"
	"log"
	"runtime"
)

func main() {
	d, ctx := workgroups.NewDispatcher(
		context.Background(),
		runtime.GOMAXPROCS(0), // This starts as much worker as maximal processes are allowed for go.
	)

	work := func(ctx context.Context) error {
		// Check if context already expired.
		// Return if its the case, else just go forward.
		select {
		case <-ctx.Done():
			return fmt.Errorf("got error from context: %w", ctx.Err())
		default:
		}

		// Some wannebe work... printing something.
		fmt.Print("hello world from work")

		return nil
	}

	// Starting up the workers.
	d.Start()

	// Feeding the workers some work.
	d.Append(workgroups.NewJob(ctx, work))

	// Closing the channel for work.
	d.Close()

	// Waiting to finnish everything.
	if err := d.Wait(); err != nil {
		log.Fatal(err)
	}

}

Output:

hello world from work