workpool

package module
v0.0.0-...-d435240 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2020 License: BSD-3-Clause Imports: 6 Imported by: 0

README

go-workpool

Go Report Card GoDoc

import github.com/raidancampbell/go-workpool

go-workpool provides a pool-of-work pattern, to synchronize events before passing them to an asynchronous/parallel processing pipeline.

For example, if you have 3 events:

  1. an account creation for account a
  2. an account update for account a
  3. an account update for account b

Events 1 and 3 can be processed simultaneously, while event 2 must wait until event 1 is complete.

Usage

A workpool is instantiated via workpool.New(). The workpool expects submitted work to implement the Work interface. This interface has a Key() function to return a string ("a" or "b" in the above example), and has a Do() function to perform whatever work is required. The workpool_test.go file contains some simple examples.

Documentation

Overview

Package workpool implements a workpool synchronized on a work item's Key. in the course of the workpool's life, two times the number of unique keys can be created one goroutine per key max for parallel processing another goroutine per key max for work queue management

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Work

type Work interface {
	// Key should return a value that identifies what the work is being performed on
	//For example:
	//If account "a" has a creation event, followed by an update, then a cancellation event
	//All three events should return "a".  This will cause the creation event to process, and the update/cancellation events to queue
	Key() string

	// Do should perform the actual work required.  Do is called in its own goroutine
	Do()
}

Work is the interface for callers to use this library. Each unit of work (such as an event) must implement the Work interface

type Workpool

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

Workpool manages work delivery. Work is delivered via the Submit function

func New

func New() *Workpool

New instantiates a default Workpool

func (*Workpool) Submit

func (wp *Workpool) Submit(w Work)

Submit submits the given work to the workpool. If other work is already in place with the same key, then this work will be queued. Order is guaranteed as a FIFO queue.

Jump to

Keyboard shortcuts

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