diff --git a/common/go-channel.go b/common/go-channel.go index 3fc86537..f9168fc4 100644 --- a/common/go-channel.go +++ b/common/go-channel.go @@ -1,22 +1,9 @@ package common import ( - "fmt" - "runtime/debug" "time" ) -func SafeGoroutine(f func()) { - go func() { - defer func() { - if r := recover(); r != nil { - SysError(fmt.Sprintf("child goroutine panic occured: error: %v, stack: %s", r, string(debug.Stack()))) - } - }() - f() - }() -} - func SafeSendBool(ch chan bool, value bool) (closed bool) { defer func() { // Recover from panic if one occured. A panic would mean the channel was closed. diff --git a/common/logger.go b/common/logger.go index e72a73af..86d15fa4 100644 --- a/common/logger.go +++ b/common/logger.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/bytedance/gopkg/util/gopool" "github.com/gin-gonic/gin" "io" "log" @@ -80,9 +81,9 @@ func logHelper(ctx context.Context, level string, msg string) { if logCount > maxLogCount && !setupLogWorking { logCount = 0 setupLogWorking = true - go func() { + gopool.Go(func() { SetupLogger() - }() + }) } } diff --git a/main.go b/main.go index 68dae8f4..495057cf 100644 --- a/main.go +++ b/main.go @@ -119,9 +119,9 @@ func main() { } if os.Getenv("ENABLE_PPROF") == "true" { - go func() { + gopool.Go(func() { log.Println(http.ListenAndServe("0.0.0.0:8005", nil)) - }() + }) go common.Monitor() common.SysLog("pprof enabled") } diff --git a/relay/relay-text.go b/relay/relay-text.go index f9d1bd03..0d1bf44c 100644 --- a/relay/relay-text.go +++ b/relay/relay-text.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/bytedance/gopkg/util/gopool" "io" "math" "net/http" @@ -307,14 +308,14 @@ func preConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo func returnPreConsumedQuota(c *gin.Context, relayInfo *relaycommon.RelayInfo, userQuota int, preConsumedQuota int) { if preConsumedQuota != 0 { - go func() { + gopool.Go(func() { relayInfoCopy := *relayInfo err := service.PostConsumeQuota(&relayInfoCopy, -preConsumedQuota, 0, false) if err != nil { common.SysError("error return pre-consumed quota: " + err.Error()) } - }() + }) } } diff --git a/service/notify-limit.go b/service/notify-limit.go index 7bb62f62..309ea54d 100644 --- a/service/notify-limit.go +++ b/service/notify-limit.go @@ -2,6 +2,7 @@ package service import ( "fmt" + "github.com/bytedance/gopkg/util/gopool" "one-api/common" "one-api/constant" "strconv" @@ -27,7 +28,7 @@ func getDuration() time.Duration { // startCleanupTask starts a background task to clean up expired entries func startCleanupTask() { - go func() { + gopool.Go(func() { for { time.Sleep(time.Hour) now := time.Now() @@ -40,7 +41,7 @@ func startCleanupTask() { return true }) } - }() + }) } // CheckNotificationLimit checks if the user has exceeded their notification limit