goroutine_pool

package module
v0.0.0-...-271fa94 Latest Latest
Warning

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

Go to latest
Published: May 13, 2021 License: MIT Imports: 3 Imported by: 0

README

goroutine_pool

A goroutine pool in go.

Why use it

With same request count, we provide a 20x-less mem useage than create a goroutine per request.

How to use
    pool := goroutine_pool.NewPool(

        // set goroutine max Capacity.
        // if set -1, will create goroutine per request with no limit.
        goroutine_pool.WithCapacity(20000),

        // set whether get a worker need block, default false
        // if set true, pool.RunTask() would be blocked while no idle goroutine is in pool.
        goroutine_pool.WithBlock(true),
    )
    // close pool`s workers
    defer pool.Close()

    // run a task with no return value
    err := pool.RunTask(func() {
        time.Sleep(10 * time.Millisecond)
    })
    if err != nil {
        log.Error(err.Error())
    }

    // run a task with a return value
    retCh, err := pool.RunTaskWithRet(func() interface{} {
        time.Sleep(10 * time.Millisecond)
        return "retString"
    })
    if err == nil {
        // read chan would be blocked until the task has been finished by any worker.
        // what`s more, the chan has been closed by worker after put return-Value in it. 
        fmt.Println(<-retCh)
    } else {
        log.Error(err.Error())
    } 
    
    // get pool`s close state
    isClosed := pool.IsClosed()
    fmt.Println(isClosed)
    
    // get how many workers are running user-task now.
    cnt := pool.RunningCount()
    fmt.Println(cnt)
  

Documentation

Index

Constants

View Source
const (

	// pool state
	PoolOpen = iota
	PoolClose
)

Variables

View Source
var (
	// ErrorPoolArriveCapacity would be return if submit a task to a pool who does not has more capacity to run a new goroutine.
	ErrorPoolArriveCapacity = errors.New("error cause of the pool is running to many goroutine")
	// ErrorPoolClosed would be return if submit a task to a closed pool.
	ErrorPoolClosed = errors.New("error cause of the pool is closed")
)

Functions

This section is empty.

Types

type Option

type Option func(*goPool)

func WithBlock

func WithBlock(isBlock bool) Option

WithBlock set whether get a worker need block

func WithCapacity

func WithCapacity(cap int32) Option

WithShardCount set max capacity

type Pool

type Pool interface {
	// RunTask submit a user-task to a goroutine,
	// and would return an error when creating a goWorker failed
	RunTask(func()) error
	// RunTaskWithRet submit a user-task which has a return value,
	// and would return a closed-chan that func`s result Value would be pushed in,
	// or a nil-chan if some error happend while creating a goWorker.
	RunTaskWithRet(func() interface{}) (<-chan interface{}, error)
	// Close Stop the pool and close all goroutines.
	Close()
	// IsClosed return whether the pool has been closed.
	IsClosed() bool
	// RunningCount return workers count which are running.
	RunningCount() int32
}

func NewPool

func NewPool(opts ...Option) Pool

NewPool create a goroutine goPool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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