gopool

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CtxGo

func CtxGo(ctx context.Context, f func())

CtxGo runs the given func in background, and it passes ctx to panic handler when happens.

func Go

func Go(f func())

Go runs the given func in background

func SetPanicHandler

func SetPanicHandler(f func(ctx context.Context, r interface{}))

SetPanicHandler sets a func for handling panic cases.

check the comment of (*GoPool).SetPanicHandler for details

Types

type GoPool

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

GoPool represents a simple worker pool which manages goroutines for background tasks.

func NewGoPool

func NewGoPool(name string, o *Option) *GoPool

NewGoPool create a new instance for goroutine worker

func (*GoPool) CtxGo

func (p *GoPool) CtxGo(ctx context.Context, f func())

CtxGo runs the given func in background, and it passes ctx to panic handler when happens.

func (*GoPool) CurrentWorkers

func (p *GoPool) CurrentWorkers() int

func (*GoPool) Go

func (p *GoPool) Go(f func())

Go runs the given func in background

func (*GoPool) SetPanicHandler

func (p *GoPool) SetPanicHandler(f func(ctx context.Context, r interface{}))

SetPanicHandler sets a func for handling panic cases.

Panic handler takes two args, `ctx` and `r`. `ctx` is the one provided when calling CtxGo, and `r` is returned by recover()

By default, GoPool will use log.Printf to record the err and stack.

It's recommended to set your own handler.

type Option

type Option struct {
	// MaxIdleWorkers is the max idle workers keeping in pool for waiting tasks.
	// There workers will exit after `WorkerMaxAge`
	MaxIdleWorkers int

	// WorkerMaxAge is the max age of a worker in pool.
	WorkerMaxAge time.Duration

	// TaskChanBuffer is the size of task queue length.
	// if it's full, we will fall back to use `go` directly without using pool.
	// normally, the queue length should be small,
	// coz we will create new workers to pick tasks if necessary.
	TaskChanBuffer int
}

Option ...

func DefaultOption

func DefaultOption() *Option

DefaultOption returns the default values of Option.

Jump to

Keyboard shortcuts

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