refactor: Replace manual goroutine creation with gopool.Go

This commit is contained in:
1808837298@qq.com
2025-02-19 18:38:29 +08:00
parent 2b7435500c
commit 5937d850d9
5 changed files with 11 additions and 21 deletions

View File

@@ -1,22 +1,9 @@
package common package common
import ( import (
"fmt"
"runtime/debug"
"time" "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) { func SafeSendBool(ch chan bool, value bool) (closed bool) {
defer func() { defer func() {
// Recover from panic if one occured. A panic would mean the channel was closed. // Recover from panic if one occured. A panic would mean the channel was closed.

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/bytedance/gopkg/util/gopool"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io" "io"
"log" "log"
@@ -80,9 +81,9 @@ func logHelper(ctx context.Context, level string, msg string) {
if logCount > maxLogCount && !setupLogWorking { if logCount > maxLogCount && !setupLogWorking {
logCount = 0 logCount = 0
setupLogWorking = true setupLogWorking = true
go func() { gopool.Go(func() {
SetupLogger() SetupLogger()
}() })
} }
} }

View File

@@ -119,9 +119,9 @@ func main() {
} }
if os.Getenv("ENABLE_PPROF") == "true" { if os.Getenv("ENABLE_PPROF") == "true" {
go func() { gopool.Go(func() {
log.Println(http.ListenAndServe("0.0.0.0:8005", nil)) log.Println(http.ListenAndServe("0.0.0.0:8005", nil))
}() })
go common.Monitor() go common.Monitor()
common.SysLog("pprof enabled") common.SysLog("pprof enabled")
} }

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/bytedance/gopkg/util/gopool"
"io" "io"
"math" "math"
"net/http" "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) { func returnPreConsumedQuota(c *gin.Context, relayInfo *relaycommon.RelayInfo, userQuota int, preConsumedQuota int) {
if preConsumedQuota != 0 { if preConsumedQuota != 0 {
go func() { gopool.Go(func() {
relayInfoCopy := *relayInfo relayInfoCopy := *relayInfo
err := service.PostConsumeQuota(&relayInfoCopy, -preConsumedQuota, 0, false) err := service.PostConsumeQuota(&relayInfoCopy, -preConsumedQuota, 0, false)
if err != nil { if err != nil {
common.SysError("error return pre-consumed quota: " + err.Error()) common.SysError("error return pre-consumed quota: " + err.Error())
} }
}() })
} }
} }

View File

@@ -2,6 +2,7 @@ package service
import ( import (
"fmt" "fmt"
"github.com/bytedance/gopkg/util/gopool"
"one-api/common" "one-api/common"
"one-api/constant" "one-api/constant"
"strconv" "strconv"
@@ -27,7 +28,7 @@ func getDuration() time.Duration {
// startCleanupTask starts a background task to clean up expired entries // startCleanupTask starts a background task to clean up expired entries
func startCleanupTask() { func startCleanupTask() {
go func() { gopool.Go(func() {
for { for {
time.Sleep(time.Hour) time.Sleep(time.Hour)
now := time.Now() now := time.Now()
@@ -40,7 +41,7 @@ func startCleanupTask() {
return true return true
}) })
} }
}() })
} }
// CheckNotificationLimit checks if the user has exceeded their notification limit // CheckNotificationLimit checks if the user has exceeded their notification limit