work

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2020 License: BSD-2-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package work contains routines to asynchronously dispatch jobs to a fixed set of Goroutines. Use this package to e.g. create a worker with 10 Goroutines and distribute work among them.

Example
package main

import (
	"fmt"
	"strconv"
	"time"

	"e13.dev/golib/work"
)

func main() {

	// create a worker with 3 goroutines that can process jobs in parallel
	worker := work.NewWorker(3, func(p work.Payload) interface{} {
		// this is our worker function that is called for every new job
		fmt.Println(p.Data)
		time.Sleep(200 * time.Millisecond)
		return nil
	}, false)

	// create 100 jobs and dispatch them to the worker.
	for i := 0; i < 100; i++ {
		// this call will block when all 3 goroutines are currently busy.
		err := worker.Dispatch(work.Payload{Data: strconv.Itoa(i)})
		if err != nil {
			return // the worker has already been shut down
		}
	}

	// this call makes sure that the worker stops all goroutines as soon as
	// they have processed all remaining jobs.
	worker.Quit()

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Payload

type Payload struct {
	Data interface{}
}

Payload wraps the data that represents a job to be processed.

type Result

type Result struct {
	Input  interface{}
	Output interface{}
}

Result wraps the result of the worker function for each input.

type Worker

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

Worker represents a worker unit that distributes jobs between its working Goroutines.

func NewWorker

func NewWorker(workerCount int, workerFunc func(Payload) interface{}, strictCompletions bool) *Worker

NewWorker creates a new worker that maintains exactly workerCount Goroutines. Each Goroutine calls workerFunc for processing the given data.

If strictCompletions is true, then the result of every job is sent to the completions channel blockingly; else it is sent there non-blockingly which might result in in the loss of the result. You must read from the channel returned by Completions when you set strictCompletions to true, otherwise you'll have a deadlock.

func (*Worker) Completions

func (w *Worker) Completions() <-chan Result

Completions returns a channel that is sent a value to every time when a job is completed. This lets you keep track of completed jobs. Be aware that you need to start reading from this channel before dispatching jobs to the Worker, otherwise you would not receive all completions. The returned channel is closed when you call Quit on the Worker.

func (*Worker) Dispatch

func (w *Worker) Dispatch(job Payload) error

Dispatch feeds a new job to the Worker. If no idle Goroutine is available, this function blocks until the job can be processed. If the worker has already been shut down with Quit, an error is returned.

func (*Worker) JobCount

func (w *Worker) JobCount() int

JobCount returns the number of jobs that are running concurrently at the moment.

func (*Worker) Quit

func (w *Worker) Quit()

Quit waits for all workers to complete their jobs and returns afterwards. After this function returns, all worker routines are stopped and you cannot use this worker, anymore. Be aware that the job queue filled with Dispatch is not drained when you call Quit so that jobs might get lost. If you want to make sure that all jobs that are dispatched are also completed, read from the completions channel returned by Completions and only call Quit after you have received all results.

Jump to

Keyboard shortcuts

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