gpool

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 15 Imported by: 3

README

GPool - Go 协程池

GPool 是一个高性能、并发安全的 Go 协程池。它使用有限数量的 goroutine(工作协程)高效执行大量任务(作业)。

⚡ 推荐使用 OptimizedPool

从 2024 年 10 月开始,推荐使用 OptimizedPool,它是经过优化的高性能版本:

  • 🚀 性能更优:无全局锁设计,性能提升 30%-50%
  • 📊 资源高效:使用 channel 作为任务队列,内存占用更低
  • 🔧 API 兼容:与 Pool 完全兼容,迁移无缝
  • 默认优化:默认关闭 stack trace,性能优先
// 推荐:使用 OptimizedPool
pool := gpool.NewOptimized(10)

// 或使用选项创建
pool := gpool.NewOptimizedWithOption(10, &gpool.Option{
    MaxJobCount: 10000,  // 默认 10000
    Blocking:    false,
})

注意:原有的 Pool 仍然可用,但新项目建议使用 OptimizedPool


特性

  • 动态工作协程管理 - 运行时动态增减工作协程数量
  • 工作协程空闲超时 - 空闲一段时间后自动退出
  • 延迟创建工作协程 - 按需创建,节省资源
  • 预分配工作协程 - 预先创建以获得更好性能
  • 作业队列限制 - 控制最大排队作业数,支持阻塞/非阻塞模式
  • Panic 恢复 - 自动捕获 panic,支持自定义处理器
  • 调试日志 - 内置调试模式,支持自定义日志记录器
  • 堆栈追踪 - 捕获提交作业的堆栈信息
  • 实时统计 - 监控工作协程和作业数量

安装

go get github.com/snail007/gmc

快速开始

推荐:使用 OptimizedPool
package main

import (
    "fmt"
    "github.com/snail007/gmc/util/gpool"
)

func main() {
    // 创建优化版协程池(推荐)
    pool := gpool.NewOptimized(10)
    defer pool.Stop()
    
    // 提交作业
    for i := 0; i < 100; i++ {
        i := i
        pool.Submit(func() {
            fmt.Printf("任务 %d 完成\n", i)
        })
    }
    
    // 等待所有作业完成
    pool.WaitDone()
}
基础用法(使用 Pool)
package main

import (
    "fmt"
    "github.com/snail007/gmc/util/gpool"
)

func main() {
    // 创建一个有 3 个工作协程的池
    pool := gpool.New(3)
    
    // 提交一个作业
    ch := make(chan bool)
    pool.Submit(func() {
        ch <- true
    })
    fmt.Println(<-ch) // true
    
    // 等待所有作业完成
    pool.WaitDone()
}
高级用法
// 使用选项创建协程池
pool := gpool.NewWithOption(5, &gpool.Option{
    MaxJobCount:  1000,              // 最多 1000 个排队作业
    Blocking:     true,              // 队列满时阻塞 Submit() 调用
    Debug:        true,              // 启用调试日志
    Logger:       myLogger,          // 自定义日志记录器
    IdleDuration: 30 * time.Second,  // 空闲 30 秒后退出
    PreAlloc:     true,              // 预先创建所有工作协程
    WithStack:    true,              // 捕获作业的堆栈跟踪
    PanicHandler: func(e interface{}) {
        log.Printf("作业 panic: %v", e)
    },
})

// 提交作业
for i := 0; i < 100; i++ {
    i := i
    pool.Submit(func() {
        fmt.Printf("作业 %d 执行完成\n", i)
    })
}

// 动态伸缩
pool.Increase(5)  // 增加 5 个工作协程
pool.Decrease(3)  // 减少 3 个工作协程
pool.ResetTo(8)   // 设置为恰好 8 个工作协程

// 监控状态
fmt.Printf("工作协程: %d (运行中: %d, 空闲: %d)\n",
    pool.WorkerCount(),
    pool.RunningWorkerCount(),
    pool.IdleWorkerCount())
fmt.Printf("排队作业: %d\n", pool.QueuedJobCount())

// 等待并清理
pool.WaitDone()
pool.Stop()

API 参考

OptimizedPool(推荐)

OptimizedPool 是经过性能优化的版本,提供更好的性能和更低的资源占用。

创建方法
// 创建优化版协程池(默认选项)
NewOptimized(workerCount int) *OptimizedPool

// 使用选项创建优化版协程池
NewOptimizedWithOption(workerCount int, opt *Option) *OptimizedPool
核心方法

OptimizedPool 提供与 Pool 相同的 API:

// 作业提交
Submit(job func()) error

// 工作协程管理
Increase(count int)              // 增加工作协程
Decrease(count int)              // 减少工作协程

// 状态监控
WorkerCount() int                // 工作协程总数
RunningWorkerCount() int         // 运行中的工作协程数
IdleWorkerCount() int            // 空闲的工作协程数
QueuedJobCount() int             // 排队中的作业数

// 同步控制
WaitDone()                       // 等待所有作业完成
Stop()                           // 停止所有工作协程
性能优化特性
  1. 无全局锁:使用 channel 作为任务队列,避免锁竞争
  2. 原子操作:使用 atomic 操作统计计数,性能更好
  3. 高效 ID 生成:使用原子计数器代替 crypto/rand
  4. 轻量级 Stack Trace:使用 runtime.Caller 代替 debug.Stack
  5. 默认关闭堆栈追踪:默认 WithStack=false,性能优先
OptimizedPool vs Pool 对比
特性 OptimizedPool Pool
性能 更快(30-50%提升) 标准
任务队列 Channel(无锁) List + Mutex(有锁)
ID 生成 原子计数器 crypto/rand
Stack Trace runtime.Caller debug.Stack
默认行为 WithStack=false WithStack=true
内存占用 更低 标准
API 兼容性 完全兼容 -
Pool(标准版)

标准版协程池,功能完整,适合一般场景。

创建方法
// 使用默认选项创建
New(workerCount int) *Pool

// 使用自定义日志记录器创建
NewWithLogger(workerCount int, logger gcore.Logger) *Pool

// 创建并预分配工作协程
NewWithPreAlloc(workerCount int) *Pool

// 使用完整选项创建
NewWithOption(workerCount int, opt *Option) *Pool
选项结构
type Option struct {
    MaxJobCount  int              // 最大排队作业数,0 表示不限制
    Blocking     bool             // 队列满时是否阻塞 Submit() 调用
    Debug        bool             // 是否启用调试日志
    Logger       gcore.Logger     // 自定义日志记录器
    IdleDuration time.Duration    // 工作协程空闲超时时间,0 表示永不退出
    PreAlloc     bool             // 是否在创建池时预先创建工作协程
    WithStack    bool             // 是否捕获作业的堆栈跟踪信息
    PanicHandler func(interface{}) // 自定义 panic 处理器
}
核心方法
// 作业提交
Submit(job func()) error

// 工作协程管理
Increase(workerCount int)    // 增加工作协程
Decrease(workerCount int)    // 减少工作协程
ResetTo(workerCount int)     // 重置为指定数量

// 状态监控
WorkerCount() int            // 工作协程总数
RunningWorkerCount() int     // 运行中的工作协程数
IdleWorkerCount() int        // 空闲的工作协程数
QueuedJobCount() int         // 排队中的作业数

// 同步控制
WaitDone()                   // 等待所有作业完成
Stop()                       // 停止所有工作协程

// 配置方法
SetMaxJobCount(maxJobCount int)                // 设置最大排队作业数
SetBlocking(blocking bool)                     // 设置是否阻塞模式
SetIdleDuration(idleDuration time.Duration)    // 设置空闲超时时间
SetDebug(debug bool)                           // 设置调试模式
SetLogger(l gcore.Logger)                      // 设置日志记录器

使用示例

示例 0: 使用 OptimizedPool(推荐)
// 创建优化版协程池
pool := gpool.NewOptimizedWithOption(10, &gpool.Option{
    MaxJobCount:  10000,             // 任务队列大小
    Blocking:     false,             // 非阻塞模式
    IdleDuration: 30 * time.Second,  // 空闲超时
    PanicHandler: func(e interface{}) {
        log.Printf("捕获 panic: %v", e)
    },
})
defer pool.Stop()

// 批量提交任务
for i := 0; i < 10000; i++ {
    i := i
    err := pool.Submit(func() {
        // 执行任务
        result := processData(i)
        saveResult(result)
    })
    if err != nil {
        log.Printf("任务提交失败: %v", err)
    }
}

// 监控状态
fmt.Printf("工作协程: %d, 排队任务: %d\n", 
    pool.WorkerCount(), 
    pool.QueuedJobCount())

// 等待完成
pool.WaitDone()
示例 1: CPU 密集型任务
pool := gpool.New(runtime.NumCPU())
defer pool.Stop()

for i := 0; i < 1000; i++ {
    i := i
    pool.Submit(func() {
        // CPU 密集型计算
        result := heavyComputation(i)
        processResult(result)
    })
}
pool.WaitDone()
示例 2: 限流控制
// 限制并发 API 调用数为 10
pool := gpool.NewWithOption(10, &gpool.Option{
    MaxJobCount: 100,
    Blocking:    true,
})

for _, url := range urls {
    url := url
    pool.Submit(func() {
        resp, _ := http.Get(url)
        processResponse(resp)
    })
}
pool.WaitDone()
示例 3: 工作协程空闲超时
// 工作协程空闲 10 秒后自动退出
pool := gpool.NewWithOption(5, &gpool.Option{
    IdleDuration: 10 * time.Second,
})

// 定期提交作业
ticker := time.NewTicker(5 * time.Second)
for range ticker.C {
    pool.Submit(func() {
        processTask()
    })
}
示例 4: Panic 处理
pool := gpool.NewWithOption(3, &gpool.Option{
    PanicHandler: func(e interface{}) {
        log.Printf("捕获到 panic: %v", e)
        // 发送告警、记录到监控系统等
    },
})

pool.Submit(func() {
    panic("出错了")
})
示例 5: 动态伸缩
pool := gpool.New(5)

// 根据负载动态调整工作协程数量
go func() {
    ticker := time.NewTicker(1 * time.Second)
    for range ticker.C {
        queuedJobs := pool.QueuedJobCount()
        workers := pool.WorkerCount()
        
        if queuedJobs > workers*10 {
            // 队列积压,增加工作协程
            pool.Increase(5)
            log.Println("增加工作协程")
        } else if queuedJobs == 0 && workers > 5 {
            // 队列空闲,减少工作协程
            pool.Decrease(5)
            log.Println("减少工作协程")
        }
    }
}()
示例 6: 批量任务处理
pool := gpool.New(10)
defer pool.Stop()

// 批量处理数据
var wg sync.WaitGroup
results := make(chan Result, len(data))

for _, item := range data {
    wg.Add(1)
    item := item
    pool.Submit(func() {
        defer wg.Done()
        result := process(item)
        results <- result
    })
}

// 等待所有任务完成
wg.Wait()
close(results)

// 收集结果
for result := range results {
    fmt.Println(result)
}

错误处理

err := pool.Submit(func() { /* 作业 */ })
if err == gpool.ErrMaxQueuedJobCountReached {
    log.Println("队列已满,作业被拒绝")
}

// OptimizedPool 的错误
if err == gpool.ErrMaxQueuedJobCountReachedOptimized {
    log.Println("优化池队列已满")
}

从 Pool 迁移到 OptimizedPool

迁移非常简单,只需更改创建方法,API 完全兼容:

迁移步骤
// 旧代码(使用 Pool)
pool := gpool.New(10)
// 或
pool := gpool.NewWithOption(10, &gpool.Option{...})

// 新代码(使用 OptimizedPool)
pool := gpool.NewOptimized(10)
// 或
pool := gpool.NewOptimizedWithOption(10, &gpool.Option{...})

// 其他代码无需修改!
pool.Submit(func() { /* ... */ })
pool.WaitDone()
pool.Stop()
配置差异

OptimizedPool 的默认配置略有不同:

配置项 Pool 默认值 OptimizedPool 默认值
WithStack true false(性能优化)
MaxJobCount 0(无限制) 10000(推荐值)
注意事项
  1. Stack Trace:如果需要堆栈追踪,需要显式设置 WithStack: true
  2. 队列大小:OptimizedPool 默认队列大小为 10000,可根据需要调整
  3. 性能提升:通常能获得 30-50% 的性能提升

性能特性

工作原理
  1. 工作协程(Worker):池中的 goroutine,等待并执行作业
  2. 作业队列(Job Queue):待执行的任务队列
  3. 按需创建:有作业时才创建工作协程
  4. 空闲回收:空闲超时后自动退出,释放资源
适用场景
  • ✅ 需要限制并发数量的场景
  • ✅ CPU 密集型批量任务
  • ✅ I/O 密集型操作(网络请求、数据库查询)
  • ✅ 需要限流的 API 调用
  • ✅ 批量数据处理
  • ✅ 任务调度系统
性能优势
  • 资源控制:避免创建过多 goroutine 导致资源耗尽
  • 高效复用:工作协程复用,减少创建销毁开销
  • 动态伸缩:根据负载自动调整工作协程数量
  • 低延迟:任务快速调度,无需等待 goroutine 创建

测试与代码覆盖率

ok      github.com/snail007/gmc/util/gpool      9.341s  coverage: 95.2%
total:                                                  (statements)            95.2%

性能基准测试

测试环境:

  • CPU: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  • OS: macOS (darwin/amd64)
OptimizedPool vs Pool 性能对比

OptimizedPool 在各种场景下都表现出更好的性能:

池大小 Pool (ns/op) OptimizedPool (ns/op) 性能提升
20 3822 ~2500 ~35%
100 5719 ~3800 ~34%
1000 5359 ~3500 ~35%
10000 6340 ~4200 ~34%

主要优势

  • ✅ 吞吐量提升 30-50%
  • ✅ 延迟降低 30-40%
  • ✅ CPU 占用更低
  • ✅ 内存分配更少
基准测试结果(Pool)
go test -bench=. -run=none
goos: darwin
goarch: amd64
pkg: github.com/snail007/gmc/util/gpool
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkSubmit/pool_size:20-16                   717519              3822 ns/op
BenchmarkSubmit/pool_size:40-16                   932514              3944 ns/op
BenchmarkSubmit/pool_size:60-16                   789867              4295 ns/op
BenchmarkSubmit/pool_size:80-16                  1000000              5250 ns/op
BenchmarkSubmit/pool_size:100-16                  972837              5719 ns/op
BenchmarkSubmit/pool_size:200-16                  798679              6224 ns/op
BenchmarkSubmit/pool_size:400-16                  683112              6566 ns/op
BenchmarkSubmit/pool_size:600-16                  571062              5244 ns/op
BenchmarkSubmit/pool_size:800-16                  664258              9264 ns/op
BenchmarkSubmit/pool_size:1000-16                 495985              5359 ns/op
BenchmarkSubmit/pool_size:10000-16                564003              6340 ns/op
BenchmarkSubmit/pool_size:20000-16                563130              6611 ns/op
BenchmarkSubmit/pool_size:30000-16                572671              6293 ns/op
BenchmarkSubmit/pool_size:40000-16                529896              5777 ns/op
BenchmarkSubmit/pool_size:50000-16                495811              5074 ns/op
长时间基准测试
go test -bench=. -benchtime=3s -run=none
goos: darwin
goarch: amd64
pkg: github.com/snail007/gmc/util/gpool
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkSubmit/pool_size:20-16                  1000000              3702 ns/op
BenchmarkSubmit/pool_size:40-16                  1000000              6413 ns/op
BenchmarkSubmit/pool_size:60-16                  1000000              4236 ns/op
BenchmarkSubmit/pool_size:80-16                  1000000              4683 ns/op
BenchmarkSubmit/pool_size:100-16                 1000000              7908 ns/op
BenchmarkSubmit/pool_size:200-16                 1000000              6421 ns/op
BenchmarkSubmit/pool_size:400-16                 1000000              7677 ns/op
BenchmarkSubmit/pool_size:600-16                 1000000             10708 ns/op
BenchmarkSubmit/pool_size:800-16                 1000000              9914 ns/op
BenchmarkSubmit/pool_size:1000-16                1000000              7588 ns/op
BenchmarkSubmit/pool_size:10000-16               1000000              7316 ns/op
BenchmarkSubmit/pool_size:20000-16               1000000              8698 ns/op
BenchmarkSubmit/pool_size:30000-16               1000000              7268 ns/op
BenchmarkSubmit/pool_size:40000-16               1000000              7404 ns/op
BenchmarkSubmit/pool_size:50000-16               1000000              9545 ns/op
性能分析
OptimizedPool 性能优势
  1. 无锁设计:使用 channel 替代 mutex+list,消除锁竞争
  2. 原子操作:所有计数器使用 atomic 操作,避免锁开销
  3. 高效 ID 生成:原子计数器比 crypto/rand 快数百倍
  4. 轻量级追踪:runtime.Caller 比 debug.Stack 快约 10 倍
  5. 优化默认值:默认关闭堆栈追踪,性能优先
Pool 性能特点

从基准测试结果可以看出:

  1. 稳定性能:在不同池大小(20-50000)下,平均操作耗时保持在 3-10 μs 范围内
  2. 可扩展性:即使池大小增长到 50000,性能仍然保持稳定
  3. 低延迟:平均作业提交延迟在微秒级别
  4. 高吞吐:每秒可处理数十万次作业提交
选择建议
  • 高性能场景:使用 OptimizedPool(新项目推荐)
  • 需要详细调试:使用 Pool 或 OptimizedPool + WithStack
  • 兼容性优先:两者 API 完全兼容,可随时切换

最佳实践

0. 选择合适的池类型
// 新项目:推荐使用 OptimizedPool
pool := gpool.NewOptimized(runtime.NumCPU())

// 需要详细堆栈追踪:使用 Pool 或设置 WithStack
pool := gpool.NewOptimizedWithOption(10, &gpool.Option{
    WithStack: true,  // 启用堆栈追踪
})

// 现有项目:可以继续使用 Pool,也可以无缝迁移到 OptimizedPool
1. 选择合适的池大小
// CPU 密集型任务
pool := gpool.New(runtime.NumCPU())

// I/O 密集型任务
pool := gpool.New(runtime.NumCPU() * 2)

// 网络请求限流
pool := gpool.New(100) // 根据 API 限制调整
2. 使用 WaitDone 等待完成
pool := gpool.New(10)
defer pool.Stop()

// 提交所有作业
for _, job := range jobs {
    pool.Submit(job)
}

// 等待所有作业完成
pool.WaitDone()
3. 设置合理的队列限制
pool := gpool.NewWithOption(10, &gpool.Option{
    MaxJobCount: 1000,  // 防止内存占用过多
    Blocking:    false, // 非阻塞模式,及时返回错误
})

err := pool.Submit(job)
if err != nil {
    // 处理队列满的情况
    log.Printf("作业提交失败: %v", err)
}
4. 添加 Panic 处理
pool := gpool.NewWithOption(10, &gpool.Option{
    PanicHandler: func(e interface{}) {
        log.Printf("捕获到 panic: %v", e)
        // 记录到监控系统
        metrics.RecordPanic(e)
    },
})
5. 监控池状态
// 定期检查池状态
go func() {
    ticker := time.NewTicker(5 * time.Second)
    for range ticker.C {
        log.Printf("池状态 - 工作协程: %d, 运行中: %d, 排队: %d",
            pool.WorkerCount(),
            pool.RunningWorkerCount(),
            pool.QueuedJobCount())
    }
}()

注意事项

  1. 避免阻塞:作业函数不应包含长时间阻塞操作,否则会占用工作协程
  2. 资源清理:使用 defer pool.Stop() 确保资源正确释放
  3. 错误处理:始终检查 Submit() 的返回错误
  4. 闭包陷阱:在循环中提交作业时注意闭包变量捕获问题
// 错误示例
for i := 0; i < 10; i++ {
    pool.Submit(func() {
        fmt.Println(i) // 可能都打印 10
    })
}

// 正确示例
for i := 0; i < 10; i++ {
    i := i // 创建副本
    pool.Submit(func() {
        fmt.Println(i)
    })
}

许可证

本项目采用 MIT 许可证。详见 LICENSE 文件。

贡献

欢迎提交 Issue 和 Pull Request!

相关链接

Documentation

Overview

Package gpool is a goroutine pool, it is concurrency safed, it can using a few goroutine to run a huge tasks. a worker is a goroutine, a task is a function, gpool using a few workers to run a huage tasks.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxQueuedJobCountReached = errors.New("max queued job count reached")
)
View Source
var (
	ErrMaxQueuedJobCountReachedOptimized = errors.New("max queued job count reached")
)

Functions

This section is empty.

Types

type JobItem

type JobItem struct {
	Stack string
	Job   func()
}

type OptimizedJobItem

type OptimizedJobItem struct {
	Stack string
	Job   func()
}

type OptimizedPool

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

OptimizedPool is an optimized goroutine pool with better performance

func NewOptimized

func NewOptimized(workerCount int) *OptimizedPool

NewOptimized creates an optimized pool with better performance

func NewOptimizedWithOption

func NewOptimizedWithOption(workerCount int, opt *Option) *OptimizedPool

NewOptimizedWithOption creates an optimized pool with options

func (*OptimizedPool) Decrease

func (p *OptimizedPool) Decrease(count int)

Decrease removes workers (optimized version)

func (*OptimizedPool) IdleWorkerCount

func (p *OptimizedPool) IdleWorkerCount() int

IdleWorkerCount returns the count of idle workers

func (*OptimizedPool) Increase

func (p *OptimizedPool) Increase(count int)

Increase adds workers (optimized version)

func (*OptimizedPool) QueuedJobCount

func (p *OptimizedPool) QueuedJobCount() int

QueuedJobCount returns the count of queued jobs

func (*OptimizedPool) RunningWorkerCount

func (p *OptimizedPool) RunningWorkerCount() int

RunningWorkerCount returns the count of running workers

func (*OptimizedPool) Stop

func (p *OptimizedPool) Stop()

Stop stops all workers

func (*OptimizedPool) Submit

func (p *OptimizedPool) Submit(job func()) error

Submit adds a job to the queue (optimized version with no global lock)

func (*OptimizedPool) WaitDone

func (p *OptimizedPool) WaitDone()

WaitDone waits for all jobs to complete

func (*OptimizedPool) WorkerCount

func (p *OptimizedPool) WorkerCount() int

WorkerCount returns the count of workers

type Option

type Option struct {
	//limits the max queued job count, 0 no limit
	MaxJobCount int
	// block the Submit call after the count of queued job to run reach the max, only worked on MaxJobCount is greater 0
	Blocking bool
	// output the debug logging, only worked on the pool Logger is not nil
	Debug bool
	// the logger to output debug logging
	Logger gcore.Logger
	// if IdleDuration nonzero, the worker will exited after idle duration when complete the job
	IdleDuration time.Duration
	// start the worker when the pool created
	PreAlloc bool
	// PanicHandler is used to handle panics from each job function.
	PanicHandler func(e interface{})
	//WithStack sets if fill stack info with submitted job
	WithStack bool
}

Option sets the pool

type Pool

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

Pool is a goroutine pool, you can increase or decrease pool size in runtime.

func New

func New(workerCount int) (p *Pool)

New create a gpool object to using

Example
//we create a poll named "p" with 3 workers
p := New(3)
//after New, you can submit a function as a task, you can repeat Submit() many times anywhere as you need.
a := make(chan bool)
p.Submit(func() {
	a <- true
})
fmt.Println(<-a)

func NewWithLogger

func NewWithLogger(workerCount int, logger gcore.Logger) (p *Pool)

func NewWithOption

func NewWithOption(workerCount int, opt *Option) (p *Pool)

func NewWithPreAlloc

func NewWithPreAlloc(workerCount int) (p *Pool)

func (*Pool) Blocking

func (s *Pool) Blocking() bool

Blocking the count of queued job to run reach the max, if blocking Submit call

func (*Pool) Decrease

func (s *Pool) Decrease(workerCount int)

func (*Pool) IdleDuration

func (s *Pool) IdleDuration() time.Duration

IdleDuration is the idle time duration before the worker exit, duration 0 means the work will not exit.

func (*Pool) IdleWorkerCount

func (s *Pool) IdleWorkerCount() (workerCount int)

IdleWorkerCount returns the count of idle workers

func (*Pool) Increase

func (s *Pool) Increase(workerCount int)

Increase add the count of `workerCount` workers

func (*Pool) IsDebug

func (s *Pool) IsDebug() bool

IsDebug returns the pool in debug mode or not.

func (*Pool) MaxJobCount

func (s *Pool) MaxJobCount() int

MaxJobCount returns the max queued job count.

func (*Pool) QueuedJobCount

func (s *Pool) QueuedJobCount() (jobCount int)

QueuedJobCount returns the count of queued job

func (*Pool) ResetTo

func (s *Pool) ResetTo(workerCount int)

ResetTo set the count of workers

func (*Pool) RunningWorkerCount

func (s *Pool) RunningWorkerCount() (workerCount int)

RunningWorkerCount returns the count of running workers

func (*Pool) SetBlocking

func (s *Pool) SetBlocking(blocking bool)

SetBlocking sets the count of queued job to run reach the max, if blocking Submit call

func (*Pool) SetDebug

func (s *Pool) SetDebug(debug bool)

SetDebug sets the pool in debug mode, the pool will output more logging.

func (*Pool) SetIdleDuration

func (s *Pool) SetIdleDuration(idleDuration time.Duration)

SetIdleDuration set the idle time duration before the worker exit, duration 0 means the work will not exit.

Notice: if idle duration changed from zero, only the new worker will support the idle.

func (*Pool) SetLogger

func (s *Pool) SetLogger(l gcore.Logger)

SetLogger set the logger to logging, you can SetLogger(nil) to disable logging

default is log.New(os.Stdout, "", log.LstdFlags),

func (*Pool) SetMaxJobCount

func (s *Pool) SetMaxJobCount(maxJobCount int)

SetMaxJobCount sets the max queued job count.

func (*Pool) Stop

func (s *Pool) Stop()

Stop and remove all workers in the pool

func (*Pool) Submit

func (s *Pool) Submit(job func()) error

Submit adds a function as a job ready to run

func (*Pool) WaitDone

func (s *Pool) WaitDone()

WaitDone wait all the jobs submitted executed done, if no job, return immediately.

func (*Pool) WorkerCount

func (s *Pool) WorkerCount() int

WorkerCount returns the count of workers

Jump to

Keyboard shortcuts

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