gf/os/grpool/grpool.go

97 lines
2.7 KiB
Go
Raw Normal View History

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,
// 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 (
"context"
"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"
"github.com/gogf/gf/v2/os/gtimer"
"github.com/gogf/gf/v2/util/grand"
2018-07-27 15:48:49 +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.
2023-09-11 10:18:44 +08:00
type RecoverFunc func(ctx context.Context, exception error)
2022-06-20 20:34:59 +08:00
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.
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
}
2023-09-11 10:18:44 +08:00
// localPoolItem is the job item storing in job list.
type localPoolItem struct {
2023-09-11 10:18:44 +08:00
Ctx context.Context // Context.
Func Func // Job function.
}
const (
2023-09-11 10:18:44 +08:00
minSupervisorTimerDuration = 500 * time.Millisecond
maxSupervisorTimerDuration = 1500 * time.Millisecond
)
2019-06-01 19:34:03 +08:00
// Default goroutine pool.
var (
2023-09-11 10:18:44 +08:00
defaultPool = 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.
// 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 {
2023-09-11 10:18:44 +08:00
var (
pool = &Pool{
limit: -1,
count: gtype.NewInt(),
list: glist.New(true),
closed: gtype.NewBool(),
}
timerDuration = grand.D(
minSupervisorTimerDuration,
maxSupervisorTimerDuration,
)
)
2019-06-19 09:06:52 +08:00
if len(limit) > 0 && limit[0] > 0 {
2023-09-11 10:18:44 +08:00
pool.limit = limit[0]
2019-06-19 09:06:52 +08:00
}
2023-09-11 10:18:44 +08:00
gtimer.Add(context.Background(), timerDuration, pool.supervisor)
return pool
2018-07-27 15:48:49 +08:00
}
2023-09-11 10:18:44 +08:00
// Add pushes a new job to the default goroutine pool.
2019-06-01 19:34:03 +08:00
// The job will be executed asynchronously.
func Add(ctx context.Context, f Func) error {
2023-09-11 10:18:44 +08:00
return defaultPool.Add(ctx, f)
2018-07-27 15:48:49 +08:00
}
2023-09-11 10:18:44 +08:00
// AddWithRecover pushes a new job to the default pool with specified recover function.
//
// 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`.
// The job will be executed asynchronously.
func AddWithRecover(ctx context.Context, userFunc Func, recoverFunc RecoverFunc) error {
2023-09-11 10:18:44 +08:00
return defaultPool.AddWithRecover(ctx, userFunc, recoverFunc)
}
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 {
2023-09-11 10:18:44 +08:00
return defaultPool.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 {
2023-09-11 10:18:44 +08:00
return defaultPool.Jobs()
2019-06-19 09:06:52 +08:00
}