协程类

package
v0.0.0-...-2910145 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

包grpool实现了goroutine可复用池。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(ctx context.Context, f Func) error

Add 向默认的goroutine池中添加一个新的任务。 该任务将会被异步执行。

func AddWithRecover

func AddWithRecover(ctx context.Context, userFunc Func, recoverFunc RecoverFunc) error

AddWithRecover 将带有指定恢复函数的新任务推送到默认工作池。

可选的 `recoverFunc` 参数在执行 `userFunc` 函数期间发生任何 panic 时被调用。 如果未传递或给定 nil,则忽略来自 `userFunc` 的 panic。 该任务将被异步执行。

func Jobs

func Jobs() int

Jobs 返回默认goroutine池当前的任务数量。

func Size

func Size() int

Size 返回默认协程池中当前的协程数量。

Types

type Func

type Func func(ctx context.Context)

Func 是一个包含上下文参数的池函数。

type Pool

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

Pool 通过使用 goroutine 池来管理 goroutines。

func New

func New(limit ...int) *Pool

New 创建并返回一个新的 goroutine 池对象。 参数 `limit` 用于限制最大goroutine数量,默认情况下不限制。

func (*Pool) Add

func (p *Pool) Add(ctx context.Context, f Func) error

Add 向任务池中添加一个新的任务。 该任务将会被异步执行。

func (*Pool) AddWithRecover

func (p *Pool) AddWithRecover(ctx context.Context, userFunc Func, recoverFunc RecoverFunc) error

AddWithRecover 向池中添加一个带有指定恢复函数的新任务。

可选的 `recoverFunc` 参数会在执行 `userFunc` 函数期间发生 panic 时被调用。 如果未传递 `recoverFunc` 或其值为 nil,则会忽略来自 `userFunc` 的 panic。 该任务将被异步执行。

func (*Pool) Cap

func (p *Pool) Cap() int

Cap 返回池的容量。 这个容量在创建池时定义。 如果没有限制,则返回 -1。

func (*Pool) Close

func (p *Pool) Close()

Close 关闭 goroutine 池,这将使所有 goroutine 退出。

func (*Pool) IsClosed

func (p *Pool) IsClosed() bool

IsClosed 返回 pool 是否已关闭。

func (*Pool) Jobs

func (p *Pool) Jobs() int

Jobs 返回当前工作池中的任务数量。 注意,它返回的是任务/任务计数,而不是工作者/协程计数。

func (*Pool) Size

func (p *Pool) Size() int

Size 返回当前 goroutine 池中的goroutine数量。

type RecoverFunc

type RecoverFunc func(ctx context.Context, exception error)

RecoverFunc 是包含上下文参数的运行时 panic 恢复函数,用于池化组件。

Jump to

Keyboard shortcuts

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