worker

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(cap int, opts ...Option)

Init 初始化默认的全局Pool

Types

type Mode

type Mode int
const (
	Sync  Mode = 1
	Async Mode = 2
)

type Option

type Option func(*pool)

协程池选项

func WithIdleTimeout

func WithIdleTimeout(duration time.Duration) Option

WithIdleTimeout 协程闲置超时时长,默认:60s

func WithPanicHandler

func WithPanicHandler(fn PanicFn) Option

WithPanicHandler 任务Panic处理方法

func WithPrefill

func WithPrefill(n int) Option

WithPrefill 预填充协程数量

func WithQueueCap

func WithQueueCap(cap int) Option

WithQueueCap 任务队列缓冲容量,默认无缓冲

type PanicFn

type PanicFn func(ctx context.Context, err any, stack []byte)

PanicFn 处理Panic方法

type Pool

type Pool interface {
	// Sync 同步模式:没有闲置协程时会等待
	//
	// 通常需要 context.WithoutCancel(ctx)
	Sync(ctx context.Context, fn func(ctx context.Context))

	// Async 异步模式:没有闲置协程时会放人全局链表缓存
	//
	// 通常需要 context.WithoutCancel(ctx)
	Async(ctx context.Context, fn func(ctx context.Context))

	// Close 关闭资源
	Close()
}

Pool 协程并发复用,降低CPU和内存负载

func NewPool

func NewPool(cap int, opts ...Option) Pool

NewPool 生成一个新的Pool

func P

func P() Pool

P 返回默认的全局Pool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳