pool

package module
v0.0.0-...-3a7e39c Latest Latest
Warning

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

Go to latest
Published: May 24, 2022 License: MIT Imports: 8 Imported by: 0

README

pool

goroutine pool

注意

池中的任务需要考虑通用性,所以使用了反射(reflect),也就会有一定的性能损失。

如果对性能要求较高,建议根据实际需求将任务函数和参数重写。

如:

type handle func (a, b string) string

type Task struct {
    handle handle
    a string
    b string
}

func NewTask(f handle, a, b string) Task {
    return Task{
        handle: f,
        a: a,
        b: b
    }
}

...

for task := range p.chTask {
    task.handle(task.a, task.b)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// return if pool size <= 0
	ErrInvalidPoolCap = errors.New("invalid pool cap")
	// put task but pool already closed
	ErrPoolAlreadyClosed = errors.New("pool already closed")
	// only the error type can be captured and processed
	ErrUnkownType = errors.New("recover only allows error type, but an unknown type is received")
	// thrown when `Handle` is not a function type
	ErrNotFunc = errors.New("`Handle` only accepts function types")
)

errors

Functions

This section is empty.

Types

type Pool

type Pool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Pool task pool

func NewPool

func NewPool(capacity uint32) (*Pool, error)

NewPool init pool

func (*Pool) BlockPanic

func (p *Pool) BlockPanic(yes bool)

BlockPanic decides whether to panic when a goroutine throws an exception

func (*Pool) Capacity

func (p *Pool) Capacity() uint32

Capacity return the capacity of the `Pool`

func (*Pool) Close

func (p *Pool) Close()

Close close pool graceful

func (*Pool) GetRunningWorkers

func (p *Pool) GetRunningWorkers() uint32

GetRunningWorkers get running workers

func (*Pool) Put

func (p *Pool) Put(task *Task) error

Put put a task to pool

func (*Pool) SetLogger

func (p *Pool) SetLogger(log *log.Logger)

SetLogger sets `logger` for `Pool`

type Status

type Status uint8
const (
	STOPED Status = iota
	RUNNING
)

running status

type Task

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

Task task to-do

func NewTask

func NewTask(handle interface{}, args ...interface{}) Task

NewTask creates a new pool task

Jump to

Keyboard shortcuts

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