diff --git a/model/channel.go b/model/channel.go index 7aa7d097..f8e1cccc 100644 --- a/model/channel.go +++ b/model/channel.go @@ -112,6 +112,10 @@ func (channel *Channel) GetNextEnabledKey() (string, int, *types.NewAPIError) { return "", 0, types.NewError(errors.New("no keys available"), types.ErrorCodeChannelNoAvailableKey) } + lock := GetChannelPollingLock(channel.Id) + lock.Lock() + defer lock.Unlock() + statusList := channel.ChannelInfo.MultiKeyStatusList // helper to get key status, default to enabled when missing getStatus := func(idx int) int { @@ -143,9 +147,6 @@ func (channel *Channel) GetNextEnabledKey() (string, int, *types.NewAPIError) { return keys[selectedIdx], selectedIdx, nil case constant.MultiKeyModePolling: // Use channel-specific lock to ensure thread-safe polling - lock := GetChannelPollingLock(channel.Id) - lock.Lock() - defer lock.Unlock() channelInfo, err := CacheGetChannelInfo(channel.Id) if err != nil {