gp

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 5 Imported by: 3

README

go-gp

Go Report Card MIT license

golang实现的协程池

Usage

go get github.com/Ficoto/go-gp

Create a Pool to use

package main

import "github.com/Ficoto/go-gp"

func main() {
	p := gp.New()
	p.Go(func() {
		// doing something
	})
	p.Close()
}

特性

  • 允许设置异步任务重试
  • 允许设置异步任务执行完成回调方法
  • 调用close方法会等待已提交的任务执行完且回调完才会结束
  • 并发协程到设置的最大协程数时,提交新任务会被堵塞
  • 捕获任务执行panic并将panic栈信息作为error传递给callback

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PoolCloseError    = errors.New("pool is closed")
	HandlerPanicError = errors.New("handler panic")
)

Functions

func NopCallback added in v0.3.0

func NopCallback(msg any, err error)

func NopIsRetry added in v0.3.0

func NopIsRetry(msg any, failTimes int) bool

Types

type LogWriter

type LogWriter interface {
	Println(values ...any)
	Printf(format string, args ...any)
}

LogWriter log writer interface

type Option

type Option func(p *Pool)

func SetLogger

func SetLogger(lw LogWriter) Option

func SetMaxPoolSize

func SetMaxPoolSize(mps int) Option

type Pool

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

func New

func New(options ...Option) *Pool

func (*Pool) Close

func (p *Pool) Close()

func (*Pool) Go

func (p *Pool) Go(f func()) error

func (*Pool) GoWithTask added in v0.2.1

func (p *Pool) GoWithTask(task Task) error

func (*Pool) Size

func (p *Pool) Size() int

type Task

type Task struct {
	Message  any
	Handler  func(msg any) error
	Callback func(msg any, err error)
	IsRetry  func(msg any, failTimes int) bool
}

Jump to

Keyboard shortcuts

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