pool

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

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

Go to latest
Published: Apr 15, 2014 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-pool

go-pool implements a basic worker pool for Go

Installation

go get github.com/davidnarayan/go-pool

Quick Start

package main

import (
    "fmt"
    "net/http"

    "github.com/davidnarayan/go-pool"
)

// 5 URLs to fetch
var urls = []string{
    "https://example.com/1",
    "https://example.com/2",
    "https://example.com/3",
    "https://example.com/4",
    "https://example.com/5",
}

// Worker function to GET an URL
func worker(args ...interface{}) (interface{}, error) {
    url := args[0].(string)
    resp, err := http.Get(url)

    return resp, err
}

// Handle results from workers
func handleResult(job *pool.Job) {
    if job.Error != nil {
        fmt.Println("Error running job: ", job.Error)
    } else {
        fmt.Println(job.Result)
    }
}

func main() {
    // Create a new pool with 4 workers
    mypool := pool.NewPool(4)
    defer close(wp.In)

    // Keep track of the number of jobs
    var numJobs int

    // Add tasks to the pool using the worker function and the list of URLs
    for _, url := range urls {
        go mypool.Add(worker, url)
        numJobs++
    }

    // Print out the results as they become available
    for i := 0; i < numJobs; i++ {
        go handleResult(wp.Result())
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IdFunc

type IdFunc func() string

IdFunc represents a user-defined function to assign Job IDs

type Job

type Job struct {
	Id     string
	F      JobFunc
	Args   []interface{}
	Result interface{}
	Error  error
}

Job holds everything required for a single unit of work

type JobFunc

type JobFunc func(...interface{}) (interface{}, error)

JobFunc represents the user-defined function to do some type of work.

type Pool

type Pool struct {
	In  chan *Job
	Out chan *Job

	Stats struct {
		Submitted int64
		Pending   int64
		Running   int64
		Completed int64
	}
	// contains filtered or unexported fields
}

Pool manages and distributes work to the workers

func NewPool

func NewPool(size int) *Pool

NewPool creates a new pool with a fixed number of workers

func (*Pool) Add

func (p *Pool) Add(fn JobFunc, args ...interface{})

Add submits creates a new job for the pool

func (*Pool) Result

func (p *Pool) Result() *Job

Return one result

func (*Pool) SetIdFunc

func (p *Pool) SetIdFunc(fn IdFunc)

SetIdFunc adds an id generator to the pool

func (*Pool) String

func (p *Pool) String() string

String provides statistics about the pool and its jobs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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