Compare commits

..

4 Commits

Author SHA1 Message Date
JustSong
45940dcb12 chore: add more info for panic fix 2024-03-10 23:59:35 +08:00
JustSong
969042b001 chore: only use one log file (close #1116) 2024-03-10 23:44:48 +08:00
JustSong
7e7369dbc4 fix: only disable channel when allowed 2024-03-10 23:41:16 +08:00
JustSong
e54e647170 chore: remove useless code 2024-03-10 23:36:29 +08:00
5 changed files with 22 additions and 26 deletions

View File

@@ -19,9 +19,6 @@ const (
loggerError = "ERR" loggerError = "ERR"
) )
const maxLogCount = 1000000
var logCount int
var setupLogLock sync.Mutex var setupLogLock sync.Mutex
var setupLogWorking bool var setupLogWorking bool
@@ -96,9 +93,7 @@ func logHelper(ctx context.Context, level string, msg string) {
id := ctx.Value(RequestIdKey) id := ctx.Value(RequestIdKey)
now := time.Now() now := time.Now()
_, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg) _, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg)
logCount++ // we don't need accurate count, so no lock here if !setupLogWorking {
if logCount > maxLogCount && !setupLogWorking {
logCount = 0
setupLogWorking = true setupLogWorking = true
go func() { go func() {
SetupLogger() SetupLogger()

View File

@@ -323,15 +323,14 @@ func updateAllChannelsBalance() error {
} }
func UpdateAllChannelsBalance(c *gin.Context) { func UpdateAllChannelsBalance(c *gin.Context) {
// TODO: make it async //err := updateAllChannelsBalance()
err := updateAllChannelsBalance() //if err != nil {
if err != nil { // c.JSON(http.StatusOK, gin.H{
c.JSON(http.StatusOK, gin.H{ // "success": false,
"success": false, // "message": err.Error(),
"message": err.Error(), // })
}) // return
return //}
}
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": true, "success": true,
"message": "", "message": "",

View File

@@ -178,7 +178,11 @@ func testChannels(notify bool, scope string) error {
milliseconds := tok.Sub(tik).Milliseconds() milliseconds := tok.Sub(tik).Milliseconds()
if isChannelEnabled && milliseconds > disableThreshold { if isChannelEnabled && milliseconds > disableThreshold {
err = errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0)) err = errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0))
monitor.DisableChannel(channel.Id, channel.Name, err.Error()) if config.AutomaticDisableChannelEnabled {
monitor.DisableChannel(channel.Id, channel.Name, err.Error())
} else {
_ = message.Notify(message.ByAll, fmt.Sprintf("渠道 %s %d测试超时", channel.Name, channel.Id), "", err.Error())
}
} }
if isChannelEnabled && util.ShouldDisableChannel(openaiErr, -1) { if isChannelEnabled && util.ShouldDisableChannel(openaiErr, -1) {
monitor.DisableChannel(channel.Id, channel.Name, err.Error()) monitor.DisableChannel(channel.Id, channel.Name, err.Error())

View File

@@ -64,13 +64,6 @@ func main() {
go model.SyncOptions(config.SyncFrequency) go model.SyncOptions(config.SyncFrequency)
go model.SyncChannelCache(config.SyncFrequency) go model.SyncChannelCache(config.SyncFrequency)
} }
if os.Getenv("CHANNEL_UPDATE_FREQUENCY") != "" {
frequency, err := strconv.Atoi(os.Getenv("CHANNEL_UPDATE_FREQUENCY"))
if err != nil {
logger.FatalLog("failed to parse CHANNEL_UPDATE_FREQUENCY: " + err.Error())
}
go controller.AutomaticallyUpdateChannels(frequency)
}
if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" { if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" {
frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY")) frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY"))
if err != nil { if err != nil {

View File

@@ -3,6 +3,7 @@ package middleware
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
@@ -12,11 +13,15 @@ func RelayPanicRecover() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
logger.SysError(fmt.Sprintf("panic detected: %v", err)) ctx := c.Request.Context()
logger.SysError(fmt.Sprintf("stacktrace from panic: %s", string(debug.Stack()))) logger.Errorf(ctx, fmt.Sprintf("panic detected: %v", err))
logger.Errorf(ctx, fmt.Sprintf("stacktrace from panic: %s", string(debug.Stack())))
logger.Errorf(ctx, fmt.Sprintf("request: %s %s", c.Request.Method, c.Request.URL.Path))
body, _ := common.GetRequestBody(c)
logger.Errorf(ctx, fmt.Sprintf("request body: %s", string(body)))
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"error": gin.H{ "error": gin.H{
"message": fmt.Sprintf("Panic detected, error: %v. Please submit a issue here: https://github.com/songquanpeng/one-api", err), "message": fmt.Sprintf("Panic detected, error: %v. Please submit an issue with the related log here: https://github.com/songquanpeng/one-api", err),
"type": "one_api_panic", "type": "one_api_panic",
}, },
}) })