2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-07-27 15:48:49 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-07-27 15:48:49 +08:00
|
|
|
|
2019-01-16 13:02:59 +08:00
|
|
|
// Package grpool implements a goroutine reusable pool.
|
2018-07-27 15:48:49 +08:00
|
|
|
package grpool
|
|
|
|
|
|
|
|
import (
|
2021-10-30 18:09:58 +08:00
|
|
|
"context"
|
2022-11-08 19:00:16 +08:00
|
|
|
"time"
|
2019-06-22 15:05:15 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/glist"
|
|
|
|
"github.com/gogf/gf/v2/container/gtype"
|
2021-11-15 20:49:02 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2022-11-08 19:00:16 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gtimer"
|
|
|
|
"github.com/gogf/gf/v2/util/grand"
|
2018-07-27 15:48:49 +08:00
|
|
|
)
|
|
|
|
|
2021-10-30 18:09:58 +08:00
|
|
|
// Func is the pool function which contains context parameter.
|
|
|
|
type Func func(ctx context.Context)
|
|
|
|
|
2022-06-20 20:34:59 +08:00
|
|
|
// RecoverFunc is the pool runtime panic recover function which contains context parameter.
|
|
|
|
type RecoverFunc func(ctx context.Context, err error)
|
|
|
|
|
2021-09-28 19:04:36 +08:00
|
|
|
// Pool manages the goroutines using pool.
|
2018-07-27 15:48:49 +08:00
|
|
|
type Pool struct {
|
2019-06-19 09:06:52 +08:00
|
|
|
limit int // Max goroutine count limit.
|
|
|
|
count *gtype.Int // Current running goroutine count.
|
2022-11-08 19:00:16 +08:00
|
|
|
list *glist.List // List for asynchronous job adding purpose.
|
2019-06-19 09:06:52 +08:00
|
|
|
closed *gtype.Bool // Is pool closed or not.
|
2018-07-27 15:48:49 +08:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:00:16 +08:00
|
|
|
type localPoolItem struct {
|
2021-10-30 18:09:58 +08:00
|
|
|
Ctx context.Context
|
|
|
|
Func Func
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:00:16 +08:00
|
|
|
const (
|
|
|
|
minTimerDuration = 500 * time.Millisecond
|
|
|
|
maxTimerDuration = 1500 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// Default goroutine pool.
|
2021-10-30 18:09:58 +08:00
|
|
|
var (
|
|
|
|
pool = New()
|
|
|
|
)
|
2018-07-27 15:48:49 +08:00
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// New creates and returns a new goroutine pool object.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `limit` is used to limit the max goroutine count,
|
2019-06-01 19:34:03 +08:00
|
|
|
// which is not limited in default.
|
2019-06-19 09:06:52 +08:00
|
|
|
func New(limit ...int) *Pool {
|
|
|
|
p := &Pool{
|
|
|
|
limit: -1,
|
|
|
|
count: gtype.NewInt(),
|
2019-07-23 23:20:27 +08:00
|
|
|
list: glist.New(true),
|
2019-06-19 09:06:52 +08:00
|
|
|
closed: gtype.NewBool(),
|
|
|
|
}
|
|
|
|
if len(limit) > 0 && limit[0] > 0 {
|
|
|
|
p.limit = limit[0]
|
|
|
|
}
|
2022-11-08 19:00:16 +08:00
|
|
|
timerDuration := grand.D(minTimerDuration, maxTimerDuration)
|
|
|
|
gtimer.Add(context.Background(), timerDuration, p.supervisor)
|
2019-06-19 09:06:52 +08:00
|
|
|
return p
|
2018-07-27 15:48:49 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// Add pushes a new job to the pool using default goroutine pool.
|
|
|
|
// The job will be executed asynchronously.
|
2021-10-30 18:09:58 +08:00
|
|
|
func Add(ctx context.Context, f Func) error {
|
|
|
|
return pool.Add(ctx, f)
|
2018-07-27 15:48:49 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 23:53:05 +08:00
|
|
|
// AddWithRecover pushes a new job to the pool with specified recover function.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The optional `recoverFunc` is called when any panic during executing of `userFunc`.
|
|
|
|
// If `recoverFunc` is not passed or given nil, it ignores the panic from `userFunc`.
|
2020-08-12 23:53:05 +08:00
|
|
|
// The job will be executed asynchronously.
|
2022-11-08 19:00:16 +08:00
|
|
|
func AddWithRecover(ctx context.Context, userFunc Func, recoverFunc RecoverFunc) error {
|
|
|
|
return pool.AddWithRecover(ctx, userFunc, recoverFunc)
|
2020-08-12 23:53:05 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// Size returns current goroutine count of default goroutine pool.
|
2018-07-27 15:48:49 +08:00
|
|
|
func Size() int {
|
2019-06-22 15:05:15 +08:00
|
|
|
return pool.Size()
|
2018-07-27 15:48:49 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// Jobs returns current job count of default goroutine pool.
|
2018-07-27 15:48:49 +08:00
|
|
|
func Jobs() int {
|
2019-06-22 15:05:15 +08:00
|
|
|
return pool.Jobs()
|
2018-07-27 15:48:49 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// Add pushes a new job to the pool.
|
|
|
|
// The job will be executed asynchronously.
|
2021-10-30 18:09:58 +08:00
|
|
|
func (p *Pool) Add(ctx context.Context, f Func) error {
|
2019-06-22 15:05:15 +08:00
|
|
|
for p.closed.Val() {
|
2022-11-08 19:00:16 +08:00
|
|
|
return gerror.NewCode(
|
|
|
|
gcode.CodeInvalidOperation,
|
|
|
|
"goroutine pool is already closed",
|
|
|
|
)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2022-11-08 19:00:16 +08:00
|
|
|
p.list.PushFront(&localPoolItem{
|
2021-10-30 18:09:58 +08:00
|
|
|
Ctx: ctx,
|
|
|
|
Func: f,
|
|
|
|
})
|
2022-11-08 19:00:16 +08:00
|
|
|
// Check and fork new worker.
|
|
|
|
p.checkAndFork()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkAndFork checks and creates a new goroutine worker.
|
|
|
|
// Note that the worker dies if the job function panics and the job has no recover handling.
|
|
|
|
func (p *Pool) checkAndFork() {
|
2019-10-28 17:16:50 +08:00
|
|
|
// Check whether fork new goroutine or not.
|
2019-06-22 15:05:15 +08:00
|
|
|
var n int
|
|
|
|
for {
|
|
|
|
n = p.count.Val()
|
|
|
|
if p.limit != -1 && n >= p.limit {
|
2019-10-28 17:16:50 +08:00
|
|
|
// No need fork new goroutine.
|
2022-11-08 19:00:16 +08:00
|
|
|
return
|
2019-06-22 15:05:15 +08:00
|
|
|
}
|
|
|
|
if p.count.Cas(n, n+1) {
|
2019-10-28 17:16:50 +08:00
|
|
|
// Use CAS to guarantee atomicity.
|
2019-06-22 15:05:15 +08:00
|
|
|
break
|
|
|
|
}
|
2019-06-03 09:09:40 +08:00
|
|
|
}
|
2022-11-08 19:00:16 +08:00
|
|
|
// Create job function in goroutine.
|
|
|
|
go func() {
|
|
|
|
defer p.count.Add(-1)
|
|
|
|
|
|
|
|
var (
|
|
|
|
listItem interface{}
|
|
|
|
poolItem *localPoolItem
|
|
|
|
)
|
|
|
|
for !p.closed.Val() {
|
|
|
|
listItem = p.list.PopBack()
|
|
|
|
if listItem == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
poolItem = listItem.(*localPoolItem)
|
|
|
|
poolItem.Func(poolItem.Ctx)
|
|
|
|
}
|
|
|
|
}()
|
2019-06-22 15:05:15 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 23:53:05 +08:00
|
|
|
// AddWithRecover pushes a new job to the pool with specified recover function.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The optional `recoverFunc` is called when any panic during executing of `userFunc`.
|
|
|
|
// If `recoverFunc` is not passed or given nil, it ignores the panic from `userFunc`.
|
2020-08-12 23:53:05 +08:00
|
|
|
// The job will be executed asynchronously.
|
2022-11-08 19:00:16 +08:00
|
|
|
func (p *Pool) AddWithRecover(ctx context.Context, userFunc Func, recoverFunc RecoverFunc) error {
|
2021-10-30 18:09:58 +08:00
|
|
|
return p.Add(ctx, func(ctx context.Context) {
|
2020-08-12 23:53:05 +08:00
|
|
|
defer func() {
|
2021-07-20 23:02:02 +08:00
|
|
|
if exception := recover(); exception != nil {
|
2022-11-08 19:00:16 +08:00
|
|
|
if recoverFunc != nil {
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := exception.(error); ok && gerror.HasStack(v) {
|
2022-11-08 19:00:16 +08:00
|
|
|
recoverFunc(ctx, v)
|
2021-07-20 23:02:02 +08:00
|
|
|
} else {
|
2022-11-08 19:00:16 +08:00
|
|
|
recoverFunc(ctx, gerror.Newf(`%+v`, exception))
|
2021-07-20 23:02:02 +08:00
|
|
|
}
|
2020-08-12 23:53:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-10-30 18:09:58 +08:00
|
|
|
userFunc(ctx)
|
2020-08-12 23:53:05 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-22 15:05:15 +08:00
|
|
|
// Cap returns the capacity of the pool.
|
|
|
|
// This capacity is defined when pool is created.
|
2019-10-28 17:16:50 +08:00
|
|
|
// It returns -1 if there's no limit.
|
2019-06-22 15:05:15 +08:00
|
|
|
func (p *Pool) Cap() int {
|
|
|
|
return p.limit
|
2018-07-27 15:48:49 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// Size returns current goroutine count of the pool.
|
2018-07-27 15:48:49 +08:00
|
|
|
func (p *Pool) Size() int {
|
2019-06-19 09:06:52 +08:00
|
|
|
return p.count.Val()
|
2018-07-27 15:48:49 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// Jobs returns current job count of the pool.
|
2019-10-28 17:16:50 +08:00
|
|
|
// Note that, it does not return worker/goroutine count but the job/task count.
|
2018-07-27 15:48:49 +08:00
|
|
|
func (p *Pool) Jobs() int {
|
2019-06-19 09:06:52 +08:00
|
|
|
return p.list.Size()
|
2018-07-27 15:48:49 +08:00
|
|
|
}
|
|
|
|
|
2019-06-22 15:05:15 +08:00
|
|
|
// IsClosed returns if pool is closed.
|
|
|
|
func (p *Pool) IsClosed() bool {
|
|
|
|
return p.closed.Val()
|
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// Close closes the goroutine pool, which makes all goroutines exit.
|
2018-07-27 15:48:49 +08:00
|
|
|
func (p *Pool) Close() {
|
2019-06-01 15:11:32 +08:00
|
|
|
p.closed.Set(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|