From 433830b4e201c5eb091f06179fd8de3839db25dc Mon Sep 17 00:00:00 2001 From: CaIon Date: Sat, 7 Nov 2026 15:51:55 +0800 Subject: [PATCH] fix(channel): restore a multi-key channel when a key is enabled after all keys failed When request failures disabled every key of a multi-key channel, enabling one key again left the channel auto-disabled. Enabling a key now restores the channel in this case too. A later manual or tag disable is kept. --- controller/channel.go | 5 ++++- controller/channel_multi_key_test.go | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index 8399fc3cba..efbe67d660 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -122,7 +122,10 @@ func disableMultiKeyChannelIfUnavailable(channel *model.Channel) bool { } func restoreMultiKeyChannelIfAvailable(channel *model.Channel) { - if channel.Status != common.ChannelStatusManuallyDisabled || !hasEnabledMultiKey(channel) { + // Manual key disables and request-driven key failures both record the + // all-keys reason; any later manual channel disable replaces it. + disabled := channel.Status == common.ChannelStatusManuallyDisabled || channel.Status == common.ChannelStatusAutoDisabled + if !disabled || !hasEnabledMultiKey(channel) { return } info := channel.GetOtherInfo() diff --git a/controller/channel_multi_key_test.go b/controller/channel_multi_key_test.go index 2b0ae26a02..5e7dbb0f9d 100644 --- a/controller/channel_multi_key_test.go +++ b/controller/channel_multi_key_test.go @@ -53,6 +53,7 @@ func TestMultiKeyEnableRestoresOnlyExhaustedChannels(t *testing.T) { for _, tc := range []struct { name string initialStatus int + autoExhaust bool manualOverride string wantStatus int }{ @@ -60,6 +61,10 @@ func TestMultiKeyEnableRestoresOnlyExhaustedChannels(t *testing.T) { {name: "manual disable is preserved", initialStatus: common.ChannelStatusManuallyDisabled, wantStatus: common.ChannelStatusManuallyDisabled}, {name: "manual disable after exhaustion is preserved", initialStatus: common.ChannelStatusEnabled, manualOverride: "status", wantStatus: common.ChannelStatusManuallyDisabled}, {name: "tag disable after exhaustion is preserved", initialStatus: common.ChannelStatusEnabled, manualOverride: "tag", wantStatus: common.ChannelStatusManuallyDisabled}, + {name: "auto key exhaustion restores", initialStatus: common.ChannelStatusEnabled, autoExhaust: true, wantStatus: common.ChannelStatusEnabled}, + {name: "auto disable for another reason is preserved", initialStatus: common.ChannelStatusAutoDisabled, wantStatus: common.ChannelStatusAutoDisabled}, + {name: "manual disable after auto exhaustion is preserved", initialStatus: common.ChannelStatusEnabled, autoExhaust: true, manualOverride: "status", wantStatus: common.ChannelStatusManuallyDisabled}, + {name: "tag disable after auto exhaustion is preserved", initialStatus: common.ChannelStatusEnabled, autoExhaust: true, manualOverride: "tag", wantStatus: common.ChannelStatusManuallyDisabled}, } { t.Run(fmt.Sprintf("cache=%t/%s/%s", cacheEnabled, action, tc.name), func(t *testing.T) { common.MemoryCacheEnabled = cacheEnabled @@ -72,7 +77,18 @@ func TestMultiKeyEnableRestoresOnlyExhaustedChannels(t *testing.T) { require.NoError(t, channel.Delete()) model.InitChannelCache() }) - for _, operation := range []string{"disable_key", action} { + operations := []string{"disable_key", action} + if tc.autoExhaust { + // Key two is already disabled, so a request failure on key + // one disables the whole channel automatically. + model.InitChannelCache() + require.True(t, model.UpdateChannelStatus(channel.Id, "key-one", common.ChannelStatusAutoDisabled, "provider rejected key")) + exhausted, err := model.GetChannelById(channel.Id, true) + require.NoError(t, err) + require.Equal(t, common.ChannelStatusAutoDisabled, exhausted.Status) + operations = []string{action} + } + for _, operation := range operations { if operation == action { if tc.manualOverride == "status" { model.UpdateChannelStatus(channel.Id, "", common.ChannelStatusManuallyDisabled, "manual operation") @@ -98,6 +114,9 @@ func TestMultiKeyEnableRestoresOnlyExhaustedChannels(t *testing.T) { loaded, err := model.GetChannelById(channel.Id, true) require.NoError(t, err) assert.Equal(t, tc.wantStatus, loaded.Status) + if tc.wantStatus == common.ChannelStatusEnabled { + assert.Equal(t, "", loaded.GetOtherInfo()["status_reason"]) + } assert.NotContains(t, loaded.ChannelInfo.MultiKeyStatusList, 0) assert.NotContains(t, loaded.ChannelInfo.MultiKeyDisabledReason, 0) assert.NotContains(t, loaded.ChannelInfo.MultiKeyDisabledTime, 0)