mirror of
				https://github.com/linux-do/new-api.git
				synced 2025-11-04 13:23:42 +08:00 
			
		
		
		
	chore: add SafeGoroutine
This commit is contained in:
		@@ -1,5 +1,21 @@
 | 
			
		||||
package common
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"runtime/debug"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
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 SafeSend(ch chan bool, value bool) (closed bool) {
 | 
			
		||||
	defer func() {
 | 
			
		||||
		// Recover from panic if one occured. A panic would mean the channel was closed.
 | 
			
		||||
 
 | 
			
		||||
@@ -80,7 +80,9 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
 | 
			
		||||
		common.LogError(ctx, "failed to record log: "+err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	if common.DataExportEnabled {
 | 
			
		||||
		go LogQuotaData(userId, username, modelName, quota, common.GetTimestamp())
 | 
			
		||||
		common.SafeGoroutine(func() {
 | 
			
		||||
			LogQuotaData(userId, username, modelName, quota, common.GetTimestamp())
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user