pool

package
v0.0.0-...-58fa5b2 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2017 License: Apache-2.0 Imports: 6 Imported by: 5

README

pool

import "github.com/ardanlabs/kit/pool"

Overview

Package pool manages a pool of routines to perform work. It does so my providing a Do function that will block when the pool is busy. This also allows the pool to monitor and report pushback. The pool also supports the dynamic re-sizing of the number of routines in the pool.

Worker

type Worker interface {
    Work(ctx context.Context, id int)
}

The Worker interface is how you can provide work to the pool. A user-defined type implements this interface, then values of that type can be passed into the Do function.

Sample Application

https://github.com/ardanlabs/kit/blob/master/examples/pool/main.go

Index

Examples
Package files

doc.go pool.go

Variables

var (
    ErrNilMinRoutines        = errors.New("Invalid (nil) minimum number of routines")
    ErrNilMaxRoutines        = errors.New("Invalid (nil) maximum number of routines")
    ErrInvalidMinRoutines    = errors.New("Invalid minimum number of routines")
    ErrInvalidMaxRoutines    = errors.New("Invalid maximum number of routines")
    ErrInvalidAdd            = errors.New("Invalid number of routines to add")
    ErrInvalidMetricHandler  = errors.New("Invalid metric handler")
    ErrInvalidMetricInterval = errors.New("Invalid metric interval")
)

Set of error variables for start up.

type Config

type Config struct {
    MinRoutines func() int // Initial and minimum number of routines always in the pool.
    MaxRoutines func() int // Maximum number of routines we will ever grow the pool to.

    OptEvent
}

Config provides configuration for the pool.

func (*Config) Event
func (cfg *Config) Event(ctx context.Context, event string, format string, a ...interface{})

Event fires events back to the user for important events.

type OptEvent

type OptEvent struct {
    Event func(ctx context.Context, event string, format string, a ...interface{})
}

OptEvent defines an handler used to provide events.

type Pool

type Pool struct {
    Config
    Name string // Name of this pool.
    // contains filtered or unexported fields
}

Pool provides a pool of routines that can execute any Worker tasks that are submitted.

func New
func New(name string, cfg Config) (*Pool, error)

New creates a new Pool.

func (*Pool) Do
func (p *Pool) Do(ctx context.Context, work Worker)

Do waits for the goroutine pool to take the work to be executed.

func (*Pool) DoCancel
func (p *Pool) DoCancel(ctx context.Context, work Worker) error

DoCancel waits for the goroutine pool to take the work to be executed or gives up if the Context is cancelled. Only use when you want to throw away work and not push back.

func (*Pool) Shutdown
func (p *Pool) Shutdown()

Shutdown waits for all the workers to finish.

func (*Pool) Stats
func (p *Pool) Stats() Stat

Stats returns the current snapshot of the pool stats.

type Stat

type Stat struct {
    Routines    int64 // Current number of routines.
    Pending     int64 // Pending number of routines waiting to submit work.
    Active      int64 // Active number of routines in the work pool.
    Executed    int64 // Number of pieces of work executed.
    MaxRoutines int64 // High water mark of routines the pool has been at.
}

Stat contains information about the pool.

type Worker

type Worker interface {
    Work(ctx context.Context, id int)
}

Worker must be implemented by types that want to use this worker processes.


Generated by godoc2md

Documentation

Overview

Package pool manages a pool of routines to perform work. It does so my providing a Do function that will block when the pool is busy. This also allows the pool to monitor and report pushback. The pool also supports the dynamic re-sizing of the number of routines in the pool.

Worker

type Worker interface {
    Work(ctx context.Context, id int)
}

The Worker interface is how you can provide work to the pool. A user-defined type implements this interface, then values of that type can be passed into the Do function.

Sample Application

https://github.com/ardanlabs/kit/blob/master/examples/pool/main.go

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNilMinRoutines        = errors.New("Invalid (nil) minimum number of routines")
	ErrNilMaxRoutines        = errors.New("Invalid (nil) maximum number of routines")
	ErrInvalidMinRoutines    = errors.New("Invalid minimum number of routines")
	ErrInvalidMaxRoutines    = errors.New("Invalid maximum number of routines")
	ErrInvalidAdd            = errors.New("Invalid number of routines to add")
	ErrInvalidMetricHandler  = errors.New("Invalid metric handler")
	ErrInvalidMetricInterval = errors.New("Invalid metric interval")
)

Set of error variables for start up.

Functions

This section is empty.

Types

type Config

type Config struct {
	MinRoutines func() int // Initial and minimum number of routines always in the pool.
	MaxRoutines func() int // Maximum number of routines we will ever grow the pool to.

	OptEvent
}

Config provides configuration for the pool.

func (*Config) Event

func (cfg *Config) Event(ctx context.Context, event string, format string, a ...interface{})

Event fires events back to the user for important events.

type OptEvent

type OptEvent struct {
	Event func(ctx context.Context, event string, format string, a ...interface{})
}

OptEvent defines an handler used to provide events.

type Pool

type Pool struct {
	Config
	Name string // Name of this pool.
	// contains filtered or unexported fields
}

Pool provides a pool of routines that can execute any Worker tasks that are submitted.

func New

func New(name string, cfg Config) (*Pool, error)

New creates a new Pool.

Example

ExampleNew provides a basic example for using a pool.

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/ardanlabs/kit/pool"
)

// theWork is the customer work type for using the pool.
type theWork struct {
	privateID int
}

// Work implements the DoWorker interface.
func (p *theWork) Work(ctx context.Context, id int) {
	fmt.Printf("Performing Work with privateID %d\n", p.privateID)
}

func main() {
	// Create a configuration.
	cfg := pool.Config{
		MinRoutines: func() int { return 3 },
		MaxRoutines: func() int { return 4 },
	}

	// Create a new pool.
	p, err := pool.New("TheWork", cfg)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Pass in some work to be performed.
	p.Do(context.TODO(), &theWork{})
	p.Do(context.TODO(), &theWork{})
	p.Do(context.TODO(), &theWork{})

	// Wait to the work to be processed.
	time.Sleep(100 * time.Millisecond)

	// Shutdown the pool.
	p.Shutdown()
}
Output:

func (*Pool) Do

func (p *Pool) Do(ctx context.Context, work Worker)

Do waits for the goroutine pool to take the work to be executed.

func (*Pool) DoCancel

func (p *Pool) DoCancel(ctx context.Context, work Worker) error

DoCancel waits for the goroutine pool to take the work to be executed or gives up if the Context is cancelled. Only use when you want to throw away work and not push back.

func (*Pool) Shutdown

func (p *Pool) Shutdown()

Shutdown waits for all the workers to finish.

func (*Pool) Stats

func (p *Pool) Stats() Stat

Stats returns the current snapshot of the pool stats.

type Stat

type Stat struct {
	Routines    int64 // Current number of routines.
	Pending     int64 // Pending number of routines waiting to submit work.
	Active      int64 // Active number of routines in the work pool.
	Executed    int64 // Number of pieces of work executed.
	MaxRoutines int64 // High water mark of routines the pool has been at.
}

Stat contains information about the pool.

type Worker

type Worker interface {
	Work(ctx context.Context, id int)
}

Worker must be implemented by types that want to use this worker processes.

Jump to

Keyboard shortcuts

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