goroutine

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrRepeatClose = errors.New("goroutine/goroutine :重复关闭")

Functions

func Delegate

func Delegate(c context.Context, t time.Duration, f func(ctx context.Context) error) error

Delegate 委托执行 一般用于回收函数超时控制

func SetCheckTime added in v1.2.1

func SetCheckTime(d time.Duration) options.Option

SetCheckTime 设置检查时间

func SetIdle added in v1.2.0

func SetIdle(idle int64) options.Option

SetIdle 这是pool闲置goroutine数量

func SetLogger

func SetLogger(logger log.Logger) options.Option

SetLogger 设置日志对象

func SetMax

func SetMax(max int64) options.Option

SetMax 设置pool最大容量

func SetStopTimeout

func SetStopTimeout(d time.Duration) options.Option

SetStopTimeout 设置停止超时时间

Types

type GGroup

type GGroup interface {
	// ChangeMax 更改buffer大小
	ChangeMax(m int64)

	// AddTask 添加需要 `go function`
	AddTask(f func()) bool

	// AddTaskN 异步添加任务,有超时机制
	AddTaskN(ctx context.Context, f func()) bool

	// Shutdown 回收资源
	Shutdown() error

	// Trick debug
	Trick() string
}

func NewGoroutine

func NewGoroutine(ctx context.Context, opts ...options.Option) GGroup

NewGoroutine 实例化方法

Example
// 默认配置
// gGroup = NewGoroutine(context.TODO())

// 可供选择配置选项

// 设置停止超时时间
// SetStopTimeout(time.Second)

// 设置日志对象
// SetLogger(&testLogger{})

// 设置pool最大容量
// SetMax(100)

gGroup = NewGoroutine(context.TODO(),
	SetStopTimeout(time.Second),
	SetLogger(log.DefaultLogger),
	SetMax(100),
)
Output:

type Goroutine

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

func (*Goroutine) AddTask

func (g *Goroutine) AddTask(f func()) bool

AddTask 添加任务 直到添加成功为止

Example
if !gGroup.AddTask(mockFunc()) {
	// 添加任务失败
}
Output:

func (*Goroutine) AddTaskN

func (g *Goroutine) AddTaskN(ctx context.Context, f func()) bool

AddTaskN 添加任务 有超时时间

Example
// 带有超时控制添加任务
if !gGroup.AddTaskN(context.TODO(), mockFunc()) {
	// 添加任务失败
}
Output:

func (*Goroutine) ChangeMax

func (g *Goroutine) ChangeMax(m int64)

ChangeMax 修改pool上限值

Example
// 修改 pool最大容量
gGroup.ChangeMax(1000)
Output:

func (*Goroutine) Shutdown

func (g *Goroutine) Shutdown() error

Shutdown 优雅关闭 符合幂等性

Example
// 回收资源
_ = gGroup.Shutdown()
Output:

func (*Goroutine) Trick added in v1.2.0

func (g *Goroutine) Trick() string

Trick Debug使用

Jump to

Keyboard shortcuts

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