work

package module
v0.0.0-...-571e26d Latest Latest
Warning

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

Go to latest
Published: May 8, 2018 License: MIT Imports: 5 Imported by: 0

README

Work

Copyright 2014 Ardan Studios. All rights reserved.
Use of this source code is governed by a MIT license that can be found in the LICENSE handle.

Package work uses an unbuffered channel to create a pool of goroutines that can perform work. Stats are maintained on the number of pending and active requests in the pool. Goroutines can be added and removed from the pool during pool operation. This allows for the implementation of a control port to adjust the size of the pool for maximum performance.

Ardan Studios
12973 SW 112 ST, Suite 153
Miami, FL 33186
bill@ardanstudios.com

Click To View Documentation

Sample App

// This sample program demostrates how to use the work package
// to use a pool of goroutines to get work done.
package main

import (
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/goinggo/work"
)

// names provides a set of names to display.
var names = []string{
	"steve",
	"bob",
	"mary",
	"therese",
	"jason",
}

// namePrinter provides special support for printing names.
type namePrinter struct {
	name string
}

// Work implements the Worker interface.
func (m *namePrinter) Work(id int) {
	fmt.Println(m.name)
	time.Sleep(time.Second)
}

func logFunc(message string) {
	log.Println(message)
}

// main is the entry point for all Go programs.
func main() {
	// Create a work value with 2 goroutines.
	w, err := work.New(2, time.Second, logFunc)
	if err != nil {
		log.Fatalln(err)
	}

	var wg sync.WaitGroup
	wg.Add(10 * len(names))

	for i := 0; i < 10; i++ {
		// Iterate over the slice of names.
		for _, name := range names {
			// Create a namePrinter and provide the
			// specfic name.
			np := namePrinter{
				name: name,
			}

			go func() {
				// Submit the task to be worked on. When Run
				// returns we know it is being handled.
				w.Run(&np)
				wg.Done()
			}()
		}
	}

	for {
		// Enter a number and hit enter to change the size
		// of the work pool.
		var c int
		fmt.Scanf("%d", &c)
		if c == 0 {
			break
		}

		w.Add(c)
	}

	wg.Wait()

	// Shutdown the work and wait for all existing work
	// to be completed.
	w.Shutdown()
}

Documentation

Overview

Package work 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.

Interface

The Worker interface is how you can provide work to the pool.

// Worker must be implemented by types that want to use this worker processes.
type Worker interface {
	Work(id int)
}

Index

Constants

This section is empty.

Variables

View Source
var ErrorInvalidMinRoutines = errors.New("Invalid minimum number of routines")

ErrorInvalidMinRoutines is the error for the invalid minRoutine parameter.

View Source
var ErrorInvalidStatTime = errors.New("Invalid duration for stat time")

ErrorInvalidStatTime is the error for the invalid stat time parameter.

Functions

This section is empty.

Types

type Pool

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

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

func New

func New(minRoutines int, statTime time.Duration, logFunc func(message string)) (*Pool, error)

New creates a new Worker.

func (*Pool) Add

func (p *Pool) Add(routines int)

Add creates routines to process work or sets a count for routines to terminate.

func (*Pool) Run

func (p *Pool) Run(work Worker)

Run wait for the goroutine pool to take the work to be executed.

func (*Pool) Shutdown

func (p *Pool) Shutdown()

Shutdown waits for all the workers to finish.

func (*Pool) State

func (p *Pool) State() State

Return worker State

type State

type State struct {
	MinRoutines int           // Minumum number of routines always in the pool.
	StatTime    time.Duration // Time to display stats.
	Counter     int           // Maintains a running total number of routines ever created.
	Routines    int64         // Number of routines
	Active      int64         // Active number of routines in the work pool.
	Pending     int64         // Pending number of routines waiting to submit work.
}

State provides a pool state that are submitted.

type Worker

type Worker interface {
	Work(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