Merge pull request #1507 from QuantumNous/multi-key-manage

feat: implement channel-specific locking for thread-safe polling
This commit is contained in:
Calcium-Ion
2025-08-05 20:40:26 +08:00
committed by GitHub
2 changed files with 7 additions and 3 deletions

View File

@@ -1107,6 +1107,10 @@ func ManageMultiKeys(c *gin.Context) {
return return
} }
lock := model.GetChannelPollingLock(channel.Id)
lock.Lock()
defer lock.Unlock()
switch request.Action { switch request.Action {
case "get_key_status": case "get_key_status":
keys := channel.GetKeys() keys := channel.GetKeys()

View File

@@ -141,7 +141,7 @@ func (channel *Channel) GetNextEnabledKey() (string, int, *types.NewAPIError) {
return keys[selectedIdx], selectedIdx, nil return keys[selectedIdx], selectedIdx, nil
case constant.MultiKeyModePolling: case constant.MultiKeyModePolling:
// Use channel-specific lock to ensure thread-safe polling // Use channel-specific lock to ensure thread-safe polling
lock := getChannelPollingLock(channel.Id) lock := GetChannelPollingLock(channel.Id)
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
@@ -500,8 +500,8 @@ var channelStatusLock sync.Mutex
// channelPollingLocks stores locks for each channel.id to ensure thread-safe polling // channelPollingLocks stores locks for each channel.id to ensure thread-safe polling
var channelPollingLocks sync.Map var channelPollingLocks sync.Map
// getChannelPollingLock returns or creates a mutex for the given channel ID // GetChannelPollingLock returns or creates a mutex for the given channel ID
func getChannelPollingLock(channelId int) *sync.Mutex { func GetChannelPollingLock(channelId int) *sync.Mutex {
if lock, exists := channelPollingLocks.Load(channelId); exists { if lock, exists := channelPollingLocks.Load(channelId); exists {
return lock.(*sync.Mutex) return lock.(*sync.Mutex)
} }