gopool

package
v0.0.0-...-21fc7a1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: Apache-2.0 Imports: 7 Imported by: 49

README

gopool

Introduction

gopool is a high-performance goroutine pool which aims to reuse goroutines and limit the number of goroutines.

It is an alternative to the go keyword.

Features

  • High Performance
  • Auto-recovering Panics
  • Limit Goroutine Numbers
  • Reuse Goroutine Stack

QuickStart

Just replace your go func(){...} with gopool.Go(func(){...}).

old:

go func() {
	// do your job
}()

new:

gopool.Go(func(){
	/// do your job
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CtxGo

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

CtxGo is preferred than Go.

func Go

func Go(f func())

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, interface{}))

SetPanicHandler sets the panic handler for the global pool.

func WorkerCount

func WorkerCount() int32

WorkerCount returns the number of global default pool's running workers

Types

type Config

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

Config is used to config pool.

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 func())
	// CtxGo executes f and accepts the context.
	CtxGo(ctx context.Context, f func())
	// SetPanicHandler sets the panic handler.
	SetPanicHandler(f func(context.Context, interface{}))
	// WorkerCount returns the number of running workers
	WorkerCount() int32
}

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.

Jump to

Keyboard shortcuts

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