routinepool

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: Apache-2.0 Imports: 12 Imported by: 2

README

routinepool

import "github.com/ccheers/xpkg/sync/routinepool"

Index

func CtxGo

func CtxGo(ctx context.Context, f RoutineFunc)

CtxGo is preferred than Go.

func Go

func Go(f RoutineFunc)

Go is an alternative to the go keyword, which is able to recover panic.

gopool.Go(func(arg interface{}){
    ...
}(nil))

func RegisterPool

func RegisterPool(p Pool) error

RegisterPool registers a new pool to the global map. GetPool can be used to get the registered pool by name. returns error if the same name is registered.

func SetCap

func SetCap(cap int32)

SetCap is not recommended to be called, this func changes the global pool's capacity which will affect other callers.

func SetPanicHandler

func SetPanicHandler(f func(context.Context, error))

SetPanicHandler sets the panic handler for the global pool.

type Config

Config is used to config pool.

type Config struct {
    // threshold for scale.
    // new goroutine is created if len(task chan) > ScaleThreshold.
    // defaults to defaultScalaThreshold.
    ScaleThreshold int32
}
func NewConfig
func NewConfig() *Config

NewConfig creates a default Config.

type Pool

type Pool interface {
    // Name returns the corresponding pool name.
    Name() string
    // SetCap sets the goroutine capacity of the pool.
    SetCap(cap int32)
    // Go executes f.
    Go(f RoutineFunc) error
    // CtxGo executes f and accepts the context.
    CtxGo(ctx context.Context, f RoutineFunc) error
    // SetPanicHandler sets the panic handler.
    SetPanicHandler(f func(context.Context, error))
    // Stop the Pool graceful
    Stop(ctx context.Context) error
}
func GetPool
func GetPool(name string) Pool

GetPool gets the registered pool by name. Returns nil if not registered.

func NewPool
func NewPool(name string, cap int32, config *Config) Pool

NewPool creates a new pool with the given name, cap and config.

type RoutineFunc

type RoutineFunc func(context.Context)

Generated by gomarkdoc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config is used to config pool.

func NewConfig

func NewConfig(opts ...Option) *Config

NewConfig creates a default Config.

type Option added in v1.2.0

type Option interface {
	// contains filtered or unexported methods
}

func WithErrorHandler added in v1.2.0

func WithErrorHandler(f func(context.Context, error)) Option

WithErrorHandler sets the panic handler.

func WithMeterProvider added in v1.2.0

func WithMeterProvider(mp metric.MeterProvider) Option

WithMeterProvider sets the meter provider.

func WithPanicHandler added in v1.2.0

func WithPanicHandler(f func(context.Context, error)) Option

WithErrorHandler sets the panic handler.

func WithScaleThreshold added in v1.2.0

func WithScaleThreshold(threshold int32) Option

WithScaleThreshold sets the scale threshold.

type Pool

type Pool interface {
	// Name returns the corresponding pool name.
	Name() string
	// SetCap sets the goroutine capacity of the pool.
	SetCap(cap int32)
	// Go executes f.
	Go(f RoutineFunc) error
	// CtxGo executes f and accepts the context.
	CtxGo(ctx context.Context, f RoutineFunc) error
	// Stop the Pool graceful
	Stop(ctx context.Context) error
}

func NewPool

func NewPool(name string, cap int32, config *Config) Pool

NewPool creates a new pool with the given name, cap and config.

func NewPoolWithError added in v1.2.0

func NewPoolWithError(name string, cap int32, config *Config) (Pool, error)

NewPoolWithError creates a new pool with the given name, cap and config. name 必须是 小写英文+下划线

type RoutineFunc

type RoutineFunc func(context.Context)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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