imitationPool

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

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

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 9 Imported by: 0

README

imitationPool

临摹ants_pool 从中收获很多

Documentation

Index

Constants

View Source
const (
	// DefaultAntsPoolSize is the default capacity for a default goroutine pool.
	DefaultAntsPoolSize = math.MaxInt32

	// DefaultCleanIntervalTime is the interval time to clean up goroutines.
	DefaultCleanIntervalTime = time.Second
)
View Source
const (
	OPENED = iota
	CLOSED
)

Variables

View Source
var (

	// ErrInvalidPoolSize will be returned when setting a negative number as pool capacity, this error will be only used
	// by pool with func because pool without func can be infinite by setting up a negative capacity.
	ErrInvalidPoolSize = errors.New("invalid size for pool")

	// ErrLackPoolFunc will be returned when invokers don't provide function for pool.
	ErrLackPoolFunc = errors.New("must provide function for pool")

	// ErrInvalidPoolExpiry will be returned when setting a negative number as the periodic duration to purge goroutines.
	ErrInvalidPoolExpiry = errors.New("invalid expiry for pool")

	// ErrPoolClosed will be returned when submitting task to a closed pool.
	ErrPoolClosed = errors.New("this pool has been closed")

	// ErrPoolOverload will be returned when the pool is full and no workers available.
	ErrPoolOverload = errors.New("too many goroutines blocked on submit or Nonblocking is set")

	// ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode.
	ErrInvalidPreAllocSize = errors.New("can not set up a negative capacity under PreAlloc mode")
)

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

Logger is used for logging formatted messages.

type Option

type Option func(opts *Options)

Option 是一个函数 函数式编程

func WithExpiryDuration

func WithExpiryDuration(expiryDuration time.Duration) Option

func WithLogger

func WithLogger(logger Logger) Option

func WithMaxBlockingTasks

func WithMaxBlockingTasks(maxBlockingTasks int) Option

func WithNonblocking

func WithNonblocking(nonblocking bool) Option

func WithOptions

func WithOptions(options *Options) Option

func WithPanicHandle

func WithPanicHandle(panicHandle func(interface{})) Option

func WithPreAlloc

func WithPreAlloc(preAlloc bool) Option

type Options

type Options struct {
	//worker 过期时间
	ExpiryDuration time.Duration

	//初始化pool时是否开辟内存,进行内存预分配
	PreAlloc bool

	// goroutine阻塞池的最大数目。
	// 0(默认值)表示没有这种限制。
	MaxBlockingTasks int

	//从pool中获取goroutine是否是阻塞的,当为true时,不会阻塞,并且MaxBlockingTasks是无效的
	Nonblocking bool

	//panic处理函数
	// PanicHandler用于处理来自每个worker goroutine的恐慌。
	//如果为nil, panic将再次从worker goroutines被抛出。
	PanicHandle func(interface{})

	//日志记录器是用于记录信息的自定义日志记录器,如果没有设置,
	//默认的标准日志从日志包使用。
	Logger Logger
}

Options包含在实例化一个ant池时应用的所有选项

type Pool

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

pool 从客户端接收任务,回收goroutines来控制goroutines数量

func NewPool

func NewPool(size int, options ...Option) (*Pool, error)

创建一个pool对象

func (*Pool) Cap

func (p *Pool) Cap() int

返回平pool的容量

func (*Pool) Free

func (p *Pool) Free() int

返回可用的worker数量

func (*Pool) Reboot

func (p *Pool) Reboot()

重启

func (*Pool) Release

func (p *Pool) Release()

释放pool(关闭)

func (*Pool) Running

func (p *Pool) Running() int

返回运行worker数量

func (*Pool) Submit

func (p *Pool) Submit(task func()) error

Submit submits a task to this pool.

func (*Pool) Tune

func (p *Pool) Tune(size int)

Tune changes the capacity of this pool, this method is noneffective to the infinite pool.

type WorkerStack

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

stack存储数据结构 一级缓存

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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