grpool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2017 License: MIT Imports: 1 Imported by: 273

README

grpool

Build Status

Lightweight Goroutine pool

Clients can submit jobs. Dispatcher takes job, and sends it to first available worker. When worker is done with processing job, will be returned back to worker pool.

Number of workers and Job queue size is configurable.

Docs

https://godoc.org/github.com/ivpusic/grpool

Installation

go get github.com/ivpusic/grpool

Simple example

package main

import (
  "fmt"
  "runtime"
  "time"

  "github.com/ivpusic/grpool"
)

func main() {
  // number of workers, and size of job queue
  pool := grpool.NewPool(100, 50)

  // release resources used by pool
  defer pool.Release()

  // submit one or more jobs to pool
  for i := 0; i < 10; i++ {
    count := i

    pool.JobQueue <- func() {
      fmt.Printf("I am worker! Number %d\n", count)
    }
  }

  // dummy wait until jobs are finished
  time.Sleep(1 * time.Second)
}

Example with waiting jobs to finish

package main

import (
  "fmt"
  "runtime"

  "github.com/ivpusic/grpool"
)

func main() {
  // number of workers, and size of job queue
  pool := grpool.NewPool(100, 50)
  defer pool.Release()

  // how many jobs we should wait
  pool.WaitCount(10)

  // submit one or more jobs to pool
  for i := 0; i < 10; i++ {
    count := i

    pool.JobQueue <- func() {
      // say that job is done, so we can know how many jobs are finished
      defer pool.JobDone()

      fmt.Printf("hello %d\n", count)
    }
  }

  // wait until we call JobDone for all jobs
  pool.WaitAll()
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job func()

Represents user request, function which should be executed in some worker.

type Pool

type Pool struct {
	JobQueue chan Job
	// contains filtered or unexported fields
}

func NewPool

func NewPool(numWorkers int, jobQueueLen int) *Pool

Will make pool of gorouting workers. numWorkers - how many workers will be created for this pool queueLen - how many jobs can we accept until we block

Returned object contains JobQueue reference, which you can use to send job to pool.

func (*Pool) JobDone

func (p *Pool) JobDone()

In case you are using WaitAll fn, you should call this method every time your job is done.

If you are not using WaitAll then we assume you have your own way of synchronizing.

func (*Pool) Release

func (p *Pool) Release()

Will release resources used by pool

func (*Pool) WaitAll

func (p *Pool) WaitAll()

Will wait for all jobs to finish.

func (*Pool) WaitCount

func (p *Pool) WaitCount(count int)

How many jobs we should wait when calling WaitAll. It is using WaitGroup Add/Done/Wait

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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