Переглянути джерело

refactor: Replace manual goroutine creation with gopool.Go

[email protected] 10 місяців тому
батько
коміт
5937d850d9
5 змінених файлів з 11 додано та 21 видалено
  1. 0 13
      common/go-channel.go
  2. 3 2
      common/logger.go
  3. 2 2
      main.go
  4. 3 2
      relay/relay-text.go
  5. 3 2
      service/notify-limit.go

+ 0 - 13
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.

+ 3 - 2
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()
-		}()
+		})
 	}
 }
 

+ 2 - 2
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")
 	}

+ 3 - 2
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())
 			}
-		}()
+		})
 	}
 }
 

+ 3 - 2
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