feat: Add context-aware goroutine pool for safer concurrent operations

This commit is contained in:
1808837298@qq.com
2025-03-04 18:42:34 +08:00
parent eb46b71a71
commit cbdf26bf2c
2 changed files with 31 additions and 2 deletions

25
common/gopool.go Normal file
View File

@@ -0,0 +1,25 @@
package common
import (
"context"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"math"
)
var relayGoPool gopool.Pool
func init() {
relayGoPool = gopool.NewPool("gopool.RelayPool", math.MaxInt32, gopool.NewConfig())
relayGoPool.SetPanicHandler(func(ctx context.Context, i interface{}) {
//check ctx.Value("stop_chan").(chan bool)
if stopChan, ok := ctx.Value("stop_chan").(chan bool); ok {
SafeSendBool(stopChan, true)
}
SysError(fmt.Sprintf("panic in gopool.RelayPool: %v", i))
})
}
func CtxGo(ctx context.Context, f func()) {
relayGoPool.CtxGo(ctx, f)
}