gpool

package module
v0.0.0-...-7e71f17 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2021 License: MIT Imports: 7 Imported by: 0

README

gpool

支持Goroutine复用和数量动态调节的Goroutine池

Documentation

Index

Constants

View Source
const (
	MinTaskWorkerCount = 0
	MaxTaskWorkerCount = (1 << (maxCountHighBit - maxCountLowBit + 1)) - 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// 初始TaskWorker的个数
	InitialTaskWorkerCount uint32

	// 并发运行的TaskWorker个数的最大值
	MaxTaskWorkerCount uint32

	// 每个TaskWorker的最大连续空闲时间长, 连续空闲时长超过此值的TaskWorker会结束退出
	KeepaliveTime time.Duration

	// 处理Task执行过程中未捕获的panic, 为nil时不会进行调用
	HandleTaskPanic func(err interface{})

	// GoroutinePool状态周期巡查的时间间隔, 为0时标识关闭Goroutine状态周期巡查
	MonitorTimeInterval time.Duration

	// 每轮GoroutinePool状态周期巡查完成后将调用MonitorCallback, 如果MonitorCallback返回true将启动Monitor Tasker
	// Monitor Tasker的作用是循环从taskChan获取TaskElem执行, 直到收到结束Monitor Tasker的信号后退出
	MonitorCallback func(map[uint64]TaskInfo) bool
}

type ConfigIllegalError

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

func (ConfigIllegalError) Error

func (err ConfigIllegalError) Error() string

type GoroutinePoolConfigModifyingError

type GoroutinePoolConfigModifyingError struct{}

func (GoroutinePoolConfigModifyingError) Error

type GoroutinePoolNotInitError

type GoroutinePoolNotInitError struct{}

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< error定义 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

func (GoroutinePoolNotInitError) Error

type GoroutinePoolStoppedError

type GoroutinePoolStoppedError struct{}

func (GoroutinePoolStoppedError) Error

type Pool

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

Pool支持:

1:动态调整执行任务的Goroutine的个数(TaskWorkerCount)
2:动态调节Goroutine空闲超时自动退出的时间(KeepaliveTime)
3:Goroutine状态周期巡查(可用于监控任务执行时间, 发现和破除协程池死锁)

func NewDefaultPool

func NewDefaultPool(maxTaskWorkerCount int) *Pool

func NewPool

func NewPool(c Config) (*Pool, error)

func (*Pool) AddTask

func (p *Pool) AddTask(task func(), data interface{}, withBlock bool) (success bool, err error)

withBlock为true表示阻塞的将task放入p中执行, 为false表示非阻塞的放入, 非阻塞时如果p不存在空闲的TaskWorker, task将放入失败

func (*Pool) ChangeKeepaliveTime

func (p *Pool) ChangeKeepaliveTime(newKeepaliveTime time.Duration) (time.Duration, error)

修改p的KeepaliveTime为newKeepaliveTime, 修改成功时返回(oldKeepaliveTime, nil)

func (*Pool) ChangeTaskWorkerCount

func (p *Pool) ChangeTaskWorkerCount(newMaxCount uint32) error

修改p允许并发执行的TaskWorker的最大个数为newMaxCount

func (*Pool) GetStatus

func (p *Pool) GetStatus() (TaskWorkerStatus, error)

返回TaskWorker p的状态信息

func (*Pool) Stop

func (p *Pool) Stop() error

type TaskInfo

type TaskInfo struct {
	// 任务开始执行的时间
	StartTimestamp time.Time

	// AddTask时携带的Data
	Data interface{}
}

type TaskWorkerStatus

type TaskWorkerStatus struct {
	// 处于运行中状态的TaskWorker的个数
	RunningWorkerCount uint64

	// 允许并发执行的TaskWorker的最大个数
	MaxWorkerCount uint64

	// 每个TaskWorker的最大连续空闲时间
	KeepaliveTime time.Duration
}

Jump to

Keyboard shortcuts

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