AlexStocks commented on code in PR #32:
URL: 
https://github.com/apache/dubbo-go-extensions/pull/32#discussion_r4577109758


##########
filter/hystrix/options.go:
##########
@@ -0,0 +1,560 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package hystrix
+
+import (
+       "fmt"
+       "sort"
+       "strings"
+       "sync"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+
+       hystrixgo "github.com/afex/hystrix-go/hystrix"
+)
+
+const (
+       hystrixExtensionName = "hystrix"
+       defaultCommandName   = "default"
+)
+
+var (
+       _ extension.Config     = (*Config)(nil)
+       _ extension.Rollbacker = (*Config)(nil)
+       _ extension.Option     = configOption{}
+)
+
+var (
+       hystrixConfigMu         sync.RWMutex
+       hystrixRuntimeCommands  = make(map[string]commandRuntimeState)
+       hystrixRuntimeDefaults  = make(map[extension.Scope]runtimeDefaultState)
+       hystrixLegacyWarnings   sync.Map
+       hystrixConfigGeneration uint64
+)
+
+// Config stores a Hystrix configuration for each Dubbo service method.
+// Command names are resource names in the form interface:group:version:method.
+type Config struct {
+       Commands map[string]CommandConfig `yaml:",remain"`
+
+       defaults       CommandConfig
+       rollbackStates map[extension.Scope]configRollbackState
+}
+
+type commandRollbackState struct {
+       previousSettings    hystrixgo.CommandConfig
+       hadPreviousSettings bool
+       previousRuntime     commandRuntimeState
+       hadPreviousRuntime  bool
+       configuredWith      hystrixgo.CommandConfig
+}
+
+type configRollbackState struct {
+       generation         uint64
+       commands           map[string]commandRollbackState
+       previousDefault    runtimeDefaultState
+       hadPreviousDefault bool
+}
+
+type commandRuntimeState struct {
+       generation  uint64
+       fromDefault bool
+       scope       extension.Scope
+}
+
+type runtimeDefaultState struct {
+       command    hystrixgo.CommandConfig
+       enabled    bool
+       generation uint64
+}
+
+// CommandConfig configures one Hystrix command. Zero values use hystrix-go's
+// defaults.
+type CommandConfig struct {
+       Timeout                int            `yaml:"timeout"`
+       MaxConcurrentRequests  int            `yaml:"max-concurrent-requests"`
+       RequestVolumeThreshold int            `yaml:"request-volume-threshold"`
+       SleepWindow            int            `yaml:"sleep-window"`
+       ErrorPercentThreshold  int            `yaml:"error-percent-threshold"`
+       UnknownFields          map[string]any `yaml:",remain"`
+}
+
+func (c *Config) Prefix() string {
+       return hystrixExtensionName
+}
+
+func (c *Config) New() extension.Config {
+       return &Config{
+               Commands:       make(map[string]CommandConfig),
+               defaults:       defaultCommandConfig(),
+               rollbackStates: make(map[extension.Scope]configRollbackState),
+       }
+}
+
+func (c *Config) Init(scope extension.Scope) error {
+       if _, err := filterNameForScope(scope); err != nil {
+               return err
+       }
+
+       commandNames := make([]string, 0, len(c.Commands))
+       for commandName := range c.Commands {
+               commandNames = append(commandNames, commandName)
+       }
+       sort.Strings(commandNames)
+
+       for _, commandName := range commandNames {
+               if err := validateCommandName(commandName); err != nil {
+                       return err
+               }
+               commandConfig := c.Commands[commandName]
+               if len(commandConfig.UnknownFields) > 0 {
+                       unknownFields := make([]string, 0, 
len(commandConfig.UnknownFields))
+                       for fieldName := range commandConfig.UnknownFields {
+                               unknownFields = append(unknownFields, fieldName)
+                       }
+                       sort.Strings(unknownFields)
+                       return fmt.Errorf("hystrix command %q: unknown 
field(s): %s",
+                               commandName, strings.Join(unknownFields, ", "))
+               }
+       }
+
+       c.defaults = defaultCommandConfig()
+       hasDefault := false
+       if defaultCommand, exists := c.Commands[defaultCommandName]; exists {
+               var err error
+               c.defaults, err = c.defaultedCommand(defaultCommand)
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
defaultCommandName, err)
+               }
+               hasDefault = true
+       }
+
+       configured := make(map[string]CommandConfig, len(commandNames))
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               command, err := c.defaultedCommand(c.Commands[commandName])
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
commandName, err)
+               }
+               configured[internalCommandName(scope, commandName)] = command
+       }
+
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+       hystrixConfigGeneration++
+       generation := hystrixConfigGeneration
+       rollbackState := configRollbackState{
+               generation: generation,
+               commands:   make(map[string]commandRollbackState, 
len(configured)),
+       }
+       if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists {
+               rollbackState.previousDefault = previousDefault
+               rollbackState.hadPreviousDefault = true
+       }
+       hystrixRuntimeDefaults[scope] = runtimeDefaultState{
+               command:    toHystrixCommandConfig(c.defaults),
+               enabled:    hasDefault,
+               generation: generation,
+       }
+       existingSettings := hystrixgo.GetCircuitSettings()
+       warnUnconfiguredLegacyCommands(scope, existingSettings, configured)
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               internalName := internalCommandName(scope, commandName)
+               command := configured[internalName]
+               hystrixCommand := toHystrixCommandConfig(command)
+               state := commandRollbackState{configuredWith: hystrixCommand}
+               if previous, exists := existingSettings[internalName]; exists {
+                       state.previousSettings = 
commandConfigFromSettings(previous)
+                       state.hadPreviousSettings = true
+               }
+               if previous, exists := hystrixRuntimeCommands[internalName]; 
exists {
+                       state.previousRuntime = previous
+                       state.hadPreviousRuntime = true
+               }
+               rollbackState.commands[internalName] = state
+               hystrixRuntimeCommands[internalName] = 
commandRuntimeState{generation: generation, scope: scope}
+               hystrixgo.ConfigureCommand(internalName, hystrixCommand)
+       }
+       if c.rollbackStates == nil {
+               c.rollbackStates = make(map[extension.Scope]configRollbackState)
+       }
+       c.rollbackStates[scope] = rollbackState
+       return nil
+}
+
+// Rollback restores settings written by Init when a later extension fails
+// during the same initialization attempt. Hystrix keeps settings process-wide,
+// so clients or servers using the same command name also share its settings.
+func (c *Config) Rollback(scope extension.Scope) error {
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+
+       rollbackState, exists := c.rollbackStates[scope]
+       if !exists {
+               return nil
+       }
+       for commandName, state := range rollbackState.commands {
+               currentRuntime, exists := hystrixRuntimeCommands[commandName]
+               if !exists || currentRuntime.generation != 
rollbackState.generation {
+                       continue
+               }
+               current, exists := hystrixgo.GetCircuitSettings()[commandName]
+               if !exists || 
!sameCommandConfig(commandConfigFromSettings(current), state.configuredWith) {
+                       continue
+               }
+               if state.hadPreviousSettings {
+                       hystrixgo.ConfigureCommand(commandName, 
state.previousSettings)
+               } else {
+                       // hystrix-go has no per-command delete API. 
ConfigureCommand fills
+                       // every zero field from its Default* constants 
(settings.go), so an
+                       // empty config resets this command to the library 
defaults.
+                       hystrixgo.ConfigureCommand(commandName, 
hystrixgo.CommandConfig{})
+               }
+               if state.hadPreviousRuntime {
+                       hystrixRuntimeCommands[commandName] = 
state.previousRuntime
+               } else {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+       }
+       if currentDefault, exists := hystrixRuntimeDefaults[scope]; exists && 
currentDefault.generation == rollbackState.generation {
+               if rollbackState.hadPreviousDefault {
+                       hystrixRuntimeDefaults[scope] = 
rollbackState.previousDefault
+               } else {
+                       delete(hystrixRuntimeDefaults, scope)
+               }
+       }
+       for commandName, commandState := range hystrixRuntimeCommands {
+               if commandState.scope == scope && commandState.fromDefault && 
commandState.generation == rollbackState.generation {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+       }
+       delete(c.rollbackStates, scope)
+       return nil
+}
+
+func commandConfigFromSettings(settings *hystrixgo.Settings) 
hystrixgo.CommandConfig {
+       return hystrixgo.CommandConfig{
+               Timeout:                int(settings.Timeout.Milliseconds()),
+               MaxConcurrentRequests:  settings.MaxConcurrentRequests,
+               RequestVolumeThreshold: int(settings.RequestVolumeThreshold),
+               SleepWindow:            
int(settings.SleepWindow.Milliseconds()),
+               ErrorPercentThreshold:  settings.ErrorPercentThreshold,
+       }
+}
+
+func sameCommandConfig(left, right hystrixgo.CommandConfig) bool {
+       return left.Timeout == right.Timeout &&
+               left.MaxConcurrentRequests == right.MaxConcurrentRequests &&
+               left.RequestVolumeThreshold == right.RequestVolumeThreshold &&
+               left.SleepWindow == right.SleepWindow &&
+               left.ErrorPercentThreshold == right.ErrorPercentThreshold
+}
+
+func toHystrixCommandConfig(command CommandConfig) hystrixgo.CommandConfig {
+       return hystrixgo.CommandConfig{
+               Timeout:                command.Timeout,
+               MaxConcurrentRequests:  command.MaxConcurrentRequests,
+               RequestVolumeThreshold: command.RequestVolumeThreshold,
+               SleepWindow:            command.SleepWindow,
+               ErrorPercentThreshold:  command.ErrorPercentThreshold,
+       }
+}
+
+func ensureRuntimeCommand(scope extension.Scope, commandName string) bool {
+       hystrixConfigMu.RLock()
+       registration, exists := hystrixRuntimeCommands[commandName]
+       if exists && !registration.fromDefault {
+               hystrixConfigMu.RUnlock()
+               return true
+       }
+       defaults, hasDefault := hystrixRuntimeDefaults[scope]
+       if !hasDefault || !defaults.enabled {
+               hystrixConfigMu.RUnlock()
+               warnLegacyCommandOnce(commandName)

Review Comment:
   🔴 **热路径上的无界 map**(对应 review 正文第 1 条)
   
   这行位于「未配置方法直通」分支,每次 RPC 都会走 `warnLegacyCommandOnce` → 
`hystrixLegacyWarnings.LoadOrStore(commandName, &sync.Once{})`,而全代码没有任何 
Delete。实测:1000 个不同方法名 → 1000 条永久条目。
   
   **Why**: `sync.Once` 只防重复日志,不防条目累积;`Init` 里的 
`warnUnconfiguredLegacyCommands` 已经覆盖了「存在 legacy 
设置」的场景,直通分支再调一次既多余又有副作用。方法名若带泛化调用参数或动态 path,长跑服务内存只增不减。
   
   **Suggestion**: 直通分支去掉这次调用;若必须保留,改成单个全局 `atomic.Bool`(整进程只提示一次)或有界集合,并在 
`Init` / `Rollback` 时清理。



##########
filter/hystrix/filter.go:
##########
@@ -96,32 +100,98 @@ type Filter struct {
 
 // Invoke is an implementation of filter, provides Hystrix pattern latency and 
fault tolerance
 func (f *Filter) Invoke(ctx context.Context, invoker base.Invoker, invocation 
base.Invocation) result.Result {
-       cmdName := getResourceName(invoker, invocation, f.isConsumer)
+       resourceName := getResourceName(invoker, invocation)
+       scope := extension.ServerScope
+       if f.isConsumer {
+               scope = extension.ClientScope
+       }
+       cmdName := internalCommandName(scope, resourceName)
+       if !ensureRuntimeCommand(scope, cmdName) {
+               return invoker.Invoke(ctx, invocation)
+       }
 
-       var res result.Result
-       err := hystrix.Do(cmdName, func() error {
-               res = invoker.Invoke(ctx, invocation)
-               return res.Error()
-       }, func(err error) error {
+       var (
+               res          result.Result
+               resMu        sync.Mutex
+               fallbackUsed bool
+       )
+       err := hystrix.DoC(ctx, cmdName, func(ctx context.Context) error {
+               invokeResult := invoker.Invoke(ctx, invocation)
+               if invokeResult == nil {
+                       return errors.New("hystrix invoker returned a nil 
result")
+               }
+               resMu.Lock()
+               if !fallbackUsed {
+                       res = invokeResult
+               }
+               resMu.Unlock()
+               return invokeResult.Error()
+       }, func(_ context.Context, err error) error {
                // Return fallback error
-               _, isCircuitOpen := err.(hystrix.CircuitError)
+               isCircuitOpen := errors.Is(err, hystrix.ErrCircuitOpen)
                if isCircuitOpen {
                        logger.Debugf("[Hystrix Filter] Circuit opened for %s", 
cmdName)
                } else {
                        logger.Debugf("[Hystrix Filter] Hystrix fallback for 
%s: %v", cmdName, err)
                }
+               resMu.Lock()
+               fallbackUsed = true
                res = &result.RPCResult{}
                res.SetResult(nil)
                res.SetError(NewHystrixFilterError(err, isCircuitOpen))
+               resMu.Unlock()
                return err
        })
 
-       if err != nil {
-               return res
+       resMu.Lock()
+       if res == nil {
+               if err == nil {
+                       err = errors.New("hystrix returned no result")
+               }
+               res = &result.RPCResult{}
+               res.SetError(NewHystrixFilterError(err, errors.Is(err, 
hystrix.ErrCircuitOpen)))
        }
+       resMu.Unlock()
        return res
 }
 
+// warnUnconfiguredLegacyCommands reports old hystrix-go settings that the
+// extension config did not declare. The caller holds hystrixConfigMu.
+func warnUnconfiguredLegacyCommands(
+       scope extension.Scope,
+       settings map[string]*hystrix.Settings,
+       configured map[string]CommandConfig,
+) {
+       prefix := internalConsumerCommandPrefix
+       if scope == extension.ServerScope {
+               prefix = internalProviderCommandPrefix
+       }
+       for commandName := range settings {
+               if !strings.HasPrefix(commandName, prefix) {
+                       continue
+               }
+               if _, configuredByExtension := configured[commandName]; 
configuredByExtension {
+                       continue
+               }
+               if _, registered := hystrixRuntimeCommands[commandName]; 
registered {
+                       continue
+               }
+               warnLegacyCommandOnce(commandName)
+       }
+}
+
+func warnLegacyCommandOnce(commandName string) {

Review Comment:
   🔴 同一条问题的另一半(review 正文第 1 条)
   
   `LoadOrStore` 是每次直通调用的必经路径;`Once` 里还会调 `hystrix.GetCircuitSettings()`,它在 
`settings.go:109-119` 会**复制整份**全局 settings map。虽然每个命令名只做一次,但每出现一个新方法名就要付一次全量拷贝 
+ 一个永久 `*sync.Once`。
   
   建议和 `options.go:296` 一起收敛:把「是否已提示过」做成进程级一次性的判断,或干脆只在 `Init` 阶段提示。



##########
filter/hystrix/options.go:
##########
@@ -0,0 +1,560 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package hystrix
+
+import (
+       "fmt"
+       "sort"
+       "strings"
+       "sync"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+
+       hystrixgo "github.com/afex/hystrix-go/hystrix"
+)
+
+const (
+       hystrixExtensionName = "hystrix"
+       defaultCommandName   = "default"
+)
+
+var (
+       _ extension.Config     = (*Config)(nil)
+       _ extension.Rollbacker = (*Config)(nil)
+       _ extension.Option     = configOption{}
+)
+
+var (
+       hystrixConfigMu         sync.RWMutex
+       hystrixRuntimeCommands  = make(map[string]commandRuntimeState)
+       hystrixRuntimeDefaults  = make(map[extension.Scope]runtimeDefaultState)
+       hystrixLegacyWarnings   sync.Map
+       hystrixConfigGeneration uint64
+)
+
+// Config stores a Hystrix configuration for each Dubbo service method.
+// Command names are resource names in the form interface:group:version:method.
+type Config struct {
+       Commands map[string]CommandConfig `yaml:",remain"`
+
+       defaults       CommandConfig
+       rollbackStates map[extension.Scope]configRollbackState
+}
+
+type commandRollbackState struct {
+       previousSettings    hystrixgo.CommandConfig
+       hadPreviousSettings bool
+       previousRuntime     commandRuntimeState
+       hadPreviousRuntime  bool
+       configuredWith      hystrixgo.CommandConfig
+}
+
+type configRollbackState struct {
+       generation         uint64
+       commands           map[string]commandRollbackState
+       previousDefault    runtimeDefaultState
+       hadPreviousDefault bool
+}
+
+type commandRuntimeState struct {
+       generation  uint64
+       fromDefault bool
+       scope       extension.Scope
+}
+
+type runtimeDefaultState struct {
+       command    hystrixgo.CommandConfig
+       enabled    bool
+       generation uint64
+}
+
+// CommandConfig configures one Hystrix command. Zero values use hystrix-go's
+// defaults.
+type CommandConfig struct {
+       Timeout                int            `yaml:"timeout"`
+       MaxConcurrentRequests  int            `yaml:"max-concurrent-requests"`
+       RequestVolumeThreshold int            `yaml:"request-volume-threshold"`
+       SleepWindow            int            `yaml:"sleep-window"`
+       ErrorPercentThreshold  int            `yaml:"error-percent-threshold"`
+       UnknownFields          map[string]any `yaml:",remain"`
+}
+
+func (c *Config) Prefix() string {
+       return hystrixExtensionName
+}
+
+func (c *Config) New() extension.Config {
+       return &Config{
+               Commands:       make(map[string]CommandConfig),
+               defaults:       defaultCommandConfig(),
+               rollbackStates: make(map[extension.Scope]configRollbackState),
+       }
+}
+
+func (c *Config) Init(scope extension.Scope) error {
+       if _, err := filterNameForScope(scope); err != nil {
+               return err
+       }
+
+       commandNames := make([]string, 0, len(c.Commands))
+       for commandName := range c.Commands {
+               commandNames = append(commandNames, commandName)
+       }
+       sort.Strings(commandNames)
+
+       for _, commandName := range commandNames {
+               if err := validateCommandName(commandName); err != nil {
+                       return err
+               }
+               commandConfig := c.Commands[commandName]
+               if len(commandConfig.UnknownFields) > 0 {
+                       unknownFields := make([]string, 0, 
len(commandConfig.UnknownFields))
+                       for fieldName := range commandConfig.UnknownFields {
+                               unknownFields = append(unknownFields, fieldName)
+                       }
+                       sort.Strings(unknownFields)
+                       return fmt.Errorf("hystrix command %q: unknown 
field(s): %s",
+                               commandName, strings.Join(unknownFields, ", "))
+               }
+       }
+
+       c.defaults = defaultCommandConfig()
+       hasDefault := false
+       if defaultCommand, exists := c.Commands[defaultCommandName]; exists {
+               var err error
+               c.defaults, err = c.defaultedCommand(defaultCommand)
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
defaultCommandName, err)
+               }
+               hasDefault = true
+       }
+
+       configured := make(map[string]CommandConfig, len(commandNames))
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               command, err := c.defaultedCommand(c.Commands[commandName])
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
commandName, err)
+               }
+               configured[internalCommandName(scope, commandName)] = command
+       }
+
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+       hystrixConfigGeneration++
+       generation := hystrixConfigGeneration
+       rollbackState := configRollbackState{
+               generation: generation,
+               commands:   make(map[string]commandRollbackState, 
len(configured)),
+       }
+       if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists {
+               rollbackState.previousDefault = previousDefault
+               rollbackState.hadPreviousDefault = true
+       }
+       hystrixRuntimeDefaults[scope] = runtimeDefaultState{

Review Comment:
   🟡 **`default` 的双重语义没有写进 README**(review 正文第 6 条)
   
   `c.defaults` 在这里既作为「未配置方法的兜底」,也在 `configured` 
循环里作为**每个具体命令零值字段的基线**(`options_test.go` 中只设 `timeout=600` 的条目继承到了 
`max-concurrent=17`)。README / README_CN 只描述了前者。
   
   另外 `defaultedCommand` 把 `0` 当作「未设置」,`WithTimeout(0)` 会静默变成 1000,用户无法显式配 
0。建议两版 README 各补两句,避免「我明明配了单方法的 timeout,为什么并发数变了」这类排查。



##########
filter/hystrix/options.go:
##########
@@ -0,0 +1,560 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package hystrix
+
+import (
+       "fmt"
+       "sort"
+       "strings"
+       "sync"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+
+       hystrixgo "github.com/afex/hystrix-go/hystrix"
+)
+
+const (
+       hystrixExtensionName = "hystrix"
+       defaultCommandName   = "default"
+)
+
+var (
+       _ extension.Config     = (*Config)(nil)
+       _ extension.Rollbacker = (*Config)(nil)
+       _ extension.Option     = configOption{}
+)
+
+var (
+       hystrixConfigMu         sync.RWMutex
+       hystrixRuntimeCommands  = make(map[string]commandRuntimeState)
+       hystrixRuntimeDefaults  = make(map[extension.Scope]runtimeDefaultState)
+       hystrixLegacyWarnings   sync.Map
+       hystrixConfigGeneration uint64
+)
+
+// Config stores a Hystrix configuration for each Dubbo service method.
+// Command names are resource names in the form interface:group:version:method.
+type Config struct {
+       Commands map[string]CommandConfig `yaml:",remain"`
+
+       defaults       CommandConfig
+       rollbackStates map[extension.Scope]configRollbackState
+}
+
+type commandRollbackState struct {
+       previousSettings    hystrixgo.CommandConfig
+       hadPreviousSettings bool
+       previousRuntime     commandRuntimeState
+       hadPreviousRuntime  bool
+       configuredWith      hystrixgo.CommandConfig
+}
+
+type configRollbackState struct {
+       generation         uint64
+       commands           map[string]commandRollbackState
+       previousDefault    runtimeDefaultState
+       hadPreviousDefault bool
+}
+
+type commandRuntimeState struct {
+       generation  uint64
+       fromDefault bool
+       scope       extension.Scope
+}
+
+type runtimeDefaultState struct {
+       command    hystrixgo.CommandConfig
+       enabled    bool
+       generation uint64
+}
+
+// CommandConfig configures one Hystrix command. Zero values use hystrix-go's
+// defaults.
+type CommandConfig struct {
+       Timeout                int            `yaml:"timeout"`
+       MaxConcurrentRequests  int            `yaml:"max-concurrent-requests"`
+       RequestVolumeThreshold int            `yaml:"request-volume-threshold"`
+       SleepWindow            int            `yaml:"sleep-window"`
+       ErrorPercentThreshold  int            `yaml:"error-percent-threshold"`
+       UnknownFields          map[string]any `yaml:",remain"`
+}
+
+func (c *Config) Prefix() string {
+       return hystrixExtensionName
+}
+
+func (c *Config) New() extension.Config {
+       return &Config{
+               Commands:       make(map[string]CommandConfig),
+               defaults:       defaultCommandConfig(),
+               rollbackStates: make(map[extension.Scope]configRollbackState),
+       }
+}
+
+func (c *Config) Init(scope extension.Scope) error {
+       if _, err := filterNameForScope(scope); err != nil {
+               return err
+       }
+
+       commandNames := make([]string, 0, len(c.Commands))
+       for commandName := range c.Commands {
+               commandNames = append(commandNames, commandName)
+       }
+       sort.Strings(commandNames)
+
+       for _, commandName := range commandNames {
+               if err := validateCommandName(commandName); err != nil {
+                       return err
+               }
+               commandConfig := c.Commands[commandName]
+               if len(commandConfig.UnknownFields) > 0 {
+                       unknownFields := make([]string, 0, 
len(commandConfig.UnknownFields))
+                       for fieldName := range commandConfig.UnknownFields {
+                               unknownFields = append(unknownFields, fieldName)
+                       }
+                       sort.Strings(unknownFields)
+                       return fmt.Errorf("hystrix command %q: unknown 
field(s): %s",
+                               commandName, strings.Join(unknownFields, ", "))
+               }
+       }
+
+       c.defaults = defaultCommandConfig()
+       hasDefault := false
+       if defaultCommand, exists := c.Commands[defaultCommandName]; exists {
+               var err error
+               c.defaults, err = c.defaultedCommand(defaultCommand)
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
defaultCommandName, err)
+               }
+               hasDefault = true
+       }
+
+       configured := make(map[string]CommandConfig, len(commandNames))
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               command, err := c.defaultedCommand(c.Commands[commandName])
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
commandName, err)
+               }
+               configured[internalCommandName(scope, commandName)] = command
+       }
+
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+       hystrixConfigGeneration++
+       generation := hystrixConfigGeneration
+       rollbackState := configRollbackState{
+               generation: generation,
+               commands:   make(map[string]commandRollbackState, 
len(configured)),
+       }
+       if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists {
+               rollbackState.previousDefault = previousDefault
+               rollbackState.hadPreviousDefault = true
+       }
+       hystrixRuntimeDefaults[scope] = runtimeDefaultState{
+               command:    toHystrixCommandConfig(c.defaults),
+               enabled:    hasDefault,
+               generation: generation,
+       }
+       existingSettings := hystrixgo.GetCircuitSettings()
+       warnUnconfiguredLegacyCommands(scope, existingSettings, configured)
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               internalName := internalCommandName(scope, commandName)
+               command := configured[internalName]
+               hystrixCommand := toHystrixCommandConfig(command)
+               state := commandRollbackState{configuredWith: hystrixCommand}
+               if previous, exists := existingSettings[internalName]; exists {
+                       state.previousSettings = 
commandConfigFromSettings(previous)
+                       state.hadPreviousSettings = true
+               }
+               if previous, exists := hystrixRuntimeCommands[internalName]; 
exists {
+                       state.previousRuntime = previous
+                       state.hadPreviousRuntime = true
+               }
+               rollbackState.commands[internalName] = state
+               hystrixRuntimeCommands[internalName] = 
commandRuntimeState{generation: generation, scope: scope}
+               hystrixgo.ConfigureCommand(internalName, hystrixCommand)
+       }
+       if c.rollbackStates == nil {
+               c.rollbackStates = make(map[extension.Scope]configRollbackState)
+       }
+       c.rollbackStates[scope] = rollbackState
+       return nil
+}
+
+// Rollback restores settings written by Init when a later extension fails
+// during the same initialization attempt. Hystrix keeps settings process-wide,
+// so clients or servers using the same command name also share its settings.
+func (c *Config) Rollback(scope extension.Scope) error {
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+
+       rollbackState, exists := c.rollbackStates[scope]
+       if !exists {
+               return nil
+       }
+       for commandName, state := range rollbackState.commands {
+               currentRuntime, exists := hystrixRuntimeCommands[commandName]
+               if !exists || currentRuntime.generation != 
rollbackState.generation {
+                       continue
+               }
+               current, exists := hystrixgo.GetCircuitSettings()[commandName]
+               if !exists || 
!sameCommandConfig(commandConfigFromSettings(current), state.configuredWith) {
+                       continue
+               }
+               if state.hadPreviousSettings {
+                       hystrixgo.ConfigureCommand(commandName, 
state.previousSettings)
+               } else {
+                       // hystrix-go has no per-command delete API. 
ConfigureCommand fills
+                       // every zero field from its Default* constants 
(settings.go), so an
+                       // empty config resets this command to the library 
defaults.
+                       hystrixgo.ConfigureCommand(commandName, 
hystrixgo.CommandConfig{})
+               }
+               if state.hadPreviousRuntime {
+                       hystrixRuntimeCommands[commandName] = 
state.previousRuntime
+               } else {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+       }
+       if currentDefault, exists := hystrixRuntimeDefaults[scope]; exists && 
currentDefault.generation == rollbackState.generation {
+               if rollbackState.hadPreviousDefault {
+                       hystrixRuntimeDefaults[scope] = 
rollbackState.previousDefault
+               } else {
+                       delete(hystrixRuntimeDefaults, scope)
+               }
+       }
+       for commandName, commandState := range hystrixRuntimeCommands {
+               if commandState.scope == scope && commandState.fromDefault && 
commandState.generation == rollbackState.generation {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+       }
+       delete(c.rollbackStates, scope)
+       return nil
+}
+
+func commandConfigFromSettings(settings *hystrixgo.Settings) 
hystrixgo.CommandConfig {
+       return hystrixgo.CommandConfig{
+               Timeout:                int(settings.Timeout.Milliseconds()),
+               MaxConcurrentRequests:  settings.MaxConcurrentRequests,
+               RequestVolumeThreshold: int(settings.RequestVolumeThreshold),
+               SleepWindow:            
int(settings.SleepWindow.Milliseconds()),
+               ErrorPercentThreshold:  settings.ErrorPercentThreshold,
+       }
+}
+
+func sameCommandConfig(left, right hystrixgo.CommandConfig) bool {
+       return left.Timeout == right.Timeout &&
+               left.MaxConcurrentRequests == right.MaxConcurrentRequests &&
+               left.RequestVolumeThreshold == right.RequestVolumeThreshold &&
+               left.SleepWindow == right.SleepWindow &&
+               left.ErrorPercentThreshold == right.ErrorPercentThreshold
+}
+
+func toHystrixCommandConfig(command CommandConfig) hystrixgo.CommandConfig {
+       return hystrixgo.CommandConfig{
+               Timeout:                command.Timeout,
+               MaxConcurrentRequests:  command.MaxConcurrentRequests,
+               RequestVolumeThreshold: command.RequestVolumeThreshold,
+               SleepWindow:            command.SleepWindow,
+               ErrorPercentThreshold:  command.ErrorPercentThreshold,
+       }
+}
+
+func ensureRuntimeCommand(scope extension.Scope, commandName string) bool {
+       hystrixConfigMu.RLock()
+       registration, exists := hystrixRuntimeCommands[commandName]
+       if exists && !registration.fromDefault {
+               hystrixConfigMu.RUnlock()
+               return true
+       }
+       defaults, hasDefault := hystrixRuntimeDefaults[scope]
+       if !hasDefault || !defaults.enabled {
+               hystrixConfigMu.RUnlock()
+               warnLegacyCommandOnce(commandName)
+               return false
+       }
+       if exists && registration.fromDefault && registration.generation == 
defaults.generation {
+               hystrixConfigMu.RUnlock()
+               return true
+       }
+       hystrixConfigMu.RUnlock()
+
+       // Only the first invocation of an unconfigured method takes the write 
lock
+       // to install the scope's default command settings.
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+       registration, exists = hystrixRuntimeCommands[commandName]
+       if exists && !registration.fromDefault {
+               return true
+       }
+       defaults, hasDefault = hystrixRuntimeDefaults[scope]
+       if !hasDefault || !defaults.enabled {
+               if exists {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+               return false
+       }
+       if exists && registration.fromDefault && registration.generation == 
defaults.generation {
+               return true
+       }
+       hystrixgo.ConfigureCommand(commandName, defaults.command)
+       hystrixRuntimeCommands[commandName] = commandRuntimeState{
+               generation:  defaults.generation,
+               fromDefault: true,
+               scope:       scope,
+       }
+       return true
+}
+
+func (c *Config) FilterNames(scope extension.Scope) []string {
+       filterName, err := filterNameForScope(scope)
+       if err != nil {
+               // The core asks for filter names before Init; Init reports 
unsupported scopes.
+               return nil
+       }
+       return []string{filterName}
+}
+
+func filterNameForScope(scope extension.Scope) (string, error) {
+       switch scope {
+       case extension.ClientScope:
+               return constant.HystrixConsumerFilterKey, nil
+       case extension.ServerScope:
+               return constant.HystrixProviderFilterKey, nil
+       default:
+               return "", fmt.Errorf("hystrix does not support scope %d", 
scope)
+       }
+}
+
+func defaultCommandConfig() CommandConfig {
+       return CommandConfig{
+               Timeout:                hystrixgo.DefaultTimeout,
+               MaxConcurrentRequests:  hystrixgo.DefaultMaxConcurrent,
+               RequestVolumeThreshold: hystrixgo.DefaultVolumeThreshold,
+               SleepWindow:            hystrixgo.DefaultSleepWindow,
+               ErrorPercentThreshold:  hystrixgo.DefaultErrorPercentThreshold,
+       }
+}
+
+func validateCommandName(commandName string) error {
+       if strings.TrimSpace(commandName) == "" {
+               return fmt.Errorf("hystrix command name is required")
+       }
+       if strings.TrimSpace(commandName) != commandName {
+               return fmt.Errorf("hystrix command name %q must not contain 
surrounding whitespace", commandName)
+       }
+       if commandName == defaultCommandName {
+               return nil
+       }
+       if strings.Count(commandName, ":") != 3 {

Review Comment:
   🟡 **命令名校验只数冒号**(review 正文第 5 条)
   
   `strings.Count(commandName, ":") != 3` 挡不住空段,实测以下都通过:
   
   | 配置名 | 生成的内部命令名 |
   | --- | --- |
   | `:::Run` | `dubbo:consumer::::Run` |
   | `a:b:c:` | `dubbo:consumer:a:b:c:` |
   | `:group:v1:Run` | `dubbo:consumer::group:v1:Run` |
   
   **Suggestion**: `strings.Split` 后校验 interface 与 method 非空,顺便拒绝带 
`dubbo:consumer:` / `dubbo:provider:` 前缀的输入。



##########
filter/hystrix/options.go:
##########
@@ -0,0 +1,560 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package hystrix
+
+import (
+       "fmt"
+       "sort"
+       "strings"
+       "sync"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+
+       hystrixgo "github.com/afex/hystrix-go/hystrix"
+)
+
+const (
+       hystrixExtensionName = "hystrix"
+       defaultCommandName   = "default"
+)
+
+var (
+       _ extension.Config     = (*Config)(nil)
+       _ extension.Rollbacker = (*Config)(nil)
+       _ extension.Option     = configOption{}
+)
+
+var (
+       hystrixConfigMu         sync.RWMutex
+       hystrixRuntimeCommands  = make(map[string]commandRuntimeState)
+       hystrixRuntimeDefaults  = make(map[extension.Scope]runtimeDefaultState)
+       hystrixLegacyWarnings   sync.Map
+       hystrixConfigGeneration uint64
+)
+
+// Config stores a Hystrix configuration for each Dubbo service method.
+// Command names are resource names in the form interface:group:version:method.
+type Config struct {
+       Commands map[string]CommandConfig `yaml:",remain"`
+
+       defaults       CommandConfig
+       rollbackStates map[extension.Scope]configRollbackState
+}
+
+type commandRollbackState struct {
+       previousSettings    hystrixgo.CommandConfig
+       hadPreviousSettings bool
+       previousRuntime     commandRuntimeState
+       hadPreviousRuntime  bool
+       configuredWith      hystrixgo.CommandConfig
+}
+
+type configRollbackState struct {
+       generation         uint64
+       commands           map[string]commandRollbackState
+       previousDefault    runtimeDefaultState
+       hadPreviousDefault bool
+}
+
+type commandRuntimeState struct {
+       generation  uint64
+       fromDefault bool
+       scope       extension.Scope
+}
+
+type runtimeDefaultState struct {
+       command    hystrixgo.CommandConfig
+       enabled    bool
+       generation uint64
+}
+
+// CommandConfig configures one Hystrix command. Zero values use hystrix-go's
+// defaults.
+type CommandConfig struct {
+       Timeout                int            `yaml:"timeout"`
+       MaxConcurrentRequests  int            `yaml:"max-concurrent-requests"`
+       RequestVolumeThreshold int            `yaml:"request-volume-threshold"`
+       SleepWindow            int            `yaml:"sleep-window"`
+       ErrorPercentThreshold  int            `yaml:"error-percent-threshold"`
+       UnknownFields          map[string]any `yaml:",remain"`
+}
+
+func (c *Config) Prefix() string {
+       return hystrixExtensionName
+}
+
+func (c *Config) New() extension.Config {
+       return &Config{
+               Commands:       make(map[string]CommandConfig),
+               defaults:       defaultCommandConfig(),
+               rollbackStates: make(map[extension.Scope]configRollbackState),
+       }
+}
+
+func (c *Config) Init(scope extension.Scope) error {
+       if _, err := filterNameForScope(scope); err != nil {
+               return err
+       }
+
+       commandNames := make([]string, 0, len(c.Commands))
+       for commandName := range c.Commands {
+               commandNames = append(commandNames, commandName)
+       }
+       sort.Strings(commandNames)
+
+       for _, commandName := range commandNames {
+               if err := validateCommandName(commandName); err != nil {
+                       return err
+               }
+               commandConfig := c.Commands[commandName]
+               if len(commandConfig.UnknownFields) > 0 {
+                       unknownFields := make([]string, 0, 
len(commandConfig.UnknownFields))
+                       for fieldName := range commandConfig.UnknownFields {
+                               unknownFields = append(unknownFields, fieldName)
+                       }
+                       sort.Strings(unknownFields)
+                       return fmt.Errorf("hystrix command %q: unknown 
field(s): %s",
+                               commandName, strings.Join(unknownFields, ", "))
+               }
+       }
+
+       c.defaults = defaultCommandConfig()
+       hasDefault := false
+       if defaultCommand, exists := c.Commands[defaultCommandName]; exists {
+               var err error
+               c.defaults, err = c.defaultedCommand(defaultCommand)
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
defaultCommandName, err)
+               }
+               hasDefault = true
+       }
+
+       configured := make(map[string]CommandConfig, len(commandNames))
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               command, err := c.defaultedCommand(c.Commands[commandName])
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
commandName, err)
+               }
+               configured[internalCommandName(scope, commandName)] = command
+       }
+
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+       hystrixConfigGeneration++
+       generation := hystrixConfigGeneration
+       rollbackState := configRollbackState{
+               generation: generation,
+               commands:   make(map[string]commandRollbackState, 
len(configured)),
+       }
+       if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists {
+               rollbackState.previousDefault = previousDefault
+               rollbackState.hadPreviousDefault = true
+       }
+       hystrixRuntimeDefaults[scope] = runtimeDefaultState{
+               command:    toHystrixCommandConfig(c.defaults),
+               enabled:    hasDefault,
+               generation: generation,
+       }
+       existingSettings := hystrixgo.GetCircuitSettings()
+       warnUnconfiguredLegacyCommands(scope, existingSettings, configured)
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               internalName := internalCommandName(scope, commandName)
+               command := configured[internalName]
+               hystrixCommand := toHystrixCommandConfig(command)
+               state := commandRollbackState{configuredWith: hystrixCommand}
+               if previous, exists := existingSettings[internalName]; exists {
+                       state.previousSettings = 
commandConfigFromSettings(previous)
+                       state.hadPreviousSettings = true
+               }
+               if previous, exists := hystrixRuntimeCommands[internalName]; 
exists {
+                       state.previousRuntime = previous
+                       state.hadPreviousRuntime = true
+               }
+               rollbackState.commands[internalName] = state
+               hystrixRuntimeCommands[internalName] = 
commandRuntimeState{generation: generation, scope: scope}
+               hystrixgo.ConfigureCommand(internalName, hystrixCommand)
+       }
+       if c.rollbackStates == nil {
+               c.rollbackStates = make(map[extension.Scope]configRollbackState)
+       }
+       c.rollbackStates[scope] = rollbackState
+       return nil
+}
+
+// Rollback restores settings written by Init when a later extension fails
+// during the same initialization attempt. Hystrix keeps settings process-wide,
+// so clients or servers using the same command name also share its settings.
+func (c *Config) Rollback(scope extension.Scope) error {
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+
+       rollbackState, exists := c.rollbackStates[scope]
+       if !exists {
+               return nil
+       }
+       for commandName, state := range rollbackState.commands {
+               currentRuntime, exists := hystrixRuntimeCommands[commandName]
+               if !exists || currentRuntime.generation != 
rollbackState.generation {
+                       continue
+               }
+               current, exists := hystrixgo.GetCircuitSettings()[commandName]
+               if !exists || 
!sameCommandConfig(commandConfigFromSettings(current), state.configuredWith) {
+                       continue
+               }
+               if state.hadPreviousSettings {
+                       hystrixgo.ConfigureCommand(commandName, 
state.previousSettings)
+               } else {
+                       // hystrix-go has no per-command delete API. 
ConfigureCommand fills
+                       // every zero field from its Default* constants 
(settings.go), so an
+                       // empty config resets this command to the library 
defaults.
+                       hystrixgo.ConfigureCommand(commandName, 
hystrixgo.CommandConfig{})
+               }
+               if state.hadPreviousRuntime {
+                       hystrixRuntimeCommands[commandName] = 
state.previousRuntime
+               } else {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+       }
+       if currentDefault, exists := hystrixRuntimeDefaults[scope]; exists && 
currentDefault.generation == rollbackState.generation {
+               if rollbackState.hadPreviousDefault {
+                       hystrixRuntimeDefaults[scope] = 
rollbackState.previousDefault
+               } else {
+                       delete(hystrixRuntimeDefaults, scope)
+               }
+       }
+       for commandName, commandState := range hystrixRuntimeCommands {
+               if commandState.scope == scope && commandState.fromDefault && 
commandState.generation == rollbackState.generation {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+       }
+       delete(c.rollbackStates, scope)
+       return nil
+}
+
+func commandConfigFromSettings(settings *hystrixgo.Settings) 
hystrixgo.CommandConfig {
+       return hystrixgo.CommandConfig{
+               Timeout:                int(settings.Timeout.Milliseconds()),
+               MaxConcurrentRequests:  settings.MaxConcurrentRequests,
+               RequestVolumeThreshold: int(settings.RequestVolumeThreshold),
+               SleepWindow:            
int(settings.SleepWindow.Milliseconds()),
+               ErrorPercentThreshold:  settings.ErrorPercentThreshold,
+       }
+}
+
+func sameCommandConfig(left, right hystrixgo.CommandConfig) bool {
+       return left.Timeout == right.Timeout &&
+               left.MaxConcurrentRequests == right.MaxConcurrentRequests &&
+               left.RequestVolumeThreshold == right.RequestVolumeThreshold &&
+               left.SleepWindow == right.SleepWindow &&
+               left.ErrorPercentThreshold == right.ErrorPercentThreshold
+}
+
+func toHystrixCommandConfig(command CommandConfig) hystrixgo.CommandConfig {
+       return hystrixgo.CommandConfig{
+               Timeout:                command.Timeout,
+               MaxConcurrentRequests:  command.MaxConcurrentRequests,
+               RequestVolumeThreshold: command.RequestVolumeThreshold,
+               SleepWindow:            command.SleepWindow,
+               ErrorPercentThreshold:  command.ErrorPercentThreshold,
+       }
+}
+
+func ensureRuntimeCommand(scope extension.Scope, commandName string) bool {
+       hystrixConfigMu.RLock()
+       registration, exists := hystrixRuntimeCommands[commandName]
+       if exists && !registration.fromDefault {
+               hystrixConfigMu.RUnlock()
+               return true
+       }
+       defaults, hasDefault := hystrixRuntimeDefaults[scope]
+       if !hasDefault || !defaults.enabled {
+               hystrixConfigMu.RUnlock()
+               warnLegacyCommandOnce(commandName)
+               return false
+       }
+       if exists && registration.fromDefault && registration.generation == 
defaults.generation {
+               hystrixConfigMu.RUnlock()
+               return true
+       }
+       hystrixConfigMu.RUnlock()
+
+       // Only the first invocation of an unconfigured method takes the write 
lock
+       // to install the scope's default command settings.
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+       registration, exists = hystrixRuntimeCommands[commandName]
+       if exists && !registration.fromDefault {
+               return true
+       }
+       defaults, hasDefault = hystrixRuntimeDefaults[scope]
+       if !hasDefault || !defaults.enabled {
+               if exists {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+               return false
+       }
+       if exists && registration.fromDefault && registration.generation == 
defaults.generation {
+               return true
+       }
+       hystrixgo.ConfigureCommand(commandName, defaults.command)

Review Comment:
   🔴 **配置 `default` 会静默覆盖旧 API 的手工配置,而 warn 说「会被绕过」**(review 正文第 2 条)
   
   实测:
   
   ```
   手工 ConfigureCommand("dubbo:consumer:legacy.Service:::Run", Timeout: 777)
   配置 default: timeout=425 → 发一次 RPC
   → GetCircuitSettings()[legacy].Timeout = 425ms
   → ensureRuntimeCommand(legacy) = true   // 确实被 hystrix 包住,不是 bypass
   ```
   
   **Why**: 有 `default` 时这里会对任意未显式配置的方法无条件 `ConfigureCommand(commandName, 
defaults.command)`,把旧值冲掉;`filter.go:186-190` 的 warn 文案只覆盖「无 default → 
直通」这一种情况。按旧文档配的命令不但没被绕过,反而被默认值改写,且没有任何提示。
   
   **Suggestion**: 二选一 —— (a) 把 `Init` 时发现的 legacy 命令登记进 
`hystrixRuntimeCommands` 作为迁移期兼容,让旧配置真的生效;(b) 按 `hasDefault` 分两种 warn 文案,并在 
README 写清「配置 `default` 会覆盖手工设置的同名命令」。



##########
filter/hystrix/options.go:
##########
@@ -0,0 +1,560 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package hystrix
+
+import (
+       "fmt"
+       "sort"
+       "strings"
+       "sync"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+
+       hystrixgo "github.com/afex/hystrix-go/hystrix"
+)
+
+const (
+       hystrixExtensionName = "hystrix"
+       defaultCommandName   = "default"
+)
+
+var (
+       _ extension.Config     = (*Config)(nil)
+       _ extension.Rollbacker = (*Config)(nil)
+       _ extension.Option     = configOption{}
+)
+
+var (
+       hystrixConfigMu         sync.RWMutex
+       hystrixRuntimeCommands  = make(map[string]commandRuntimeState)
+       hystrixRuntimeDefaults  = make(map[extension.Scope]runtimeDefaultState)
+       hystrixLegacyWarnings   sync.Map
+       hystrixConfigGeneration uint64
+)
+
+// Config stores a Hystrix configuration for each Dubbo service method.
+// Command names are resource names in the form interface:group:version:method.
+type Config struct {
+       Commands map[string]CommandConfig `yaml:",remain"`
+
+       defaults       CommandConfig
+       rollbackStates map[extension.Scope]configRollbackState
+}
+
+type commandRollbackState struct {
+       previousSettings    hystrixgo.CommandConfig
+       hadPreviousSettings bool
+       previousRuntime     commandRuntimeState
+       hadPreviousRuntime  bool
+       configuredWith      hystrixgo.CommandConfig
+}
+
+type configRollbackState struct {
+       generation         uint64
+       commands           map[string]commandRollbackState
+       previousDefault    runtimeDefaultState
+       hadPreviousDefault bool
+}
+
+type commandRuntimeState struct {
+       generation  uint64
+       fromDefault bool
+       scope       extension.Scope
+}
+
+type runtimeDefaultState struct {
+       command    hystrixgo.CommandConfig
+       enabled    bool
+       generation uint64
+}
+
+// CommandConfig configures one Hystrix command. Zero values use hystrix-go's
+// defaults.
+type CommandConfig struct {
+       Timeout                int            `yaml:"timeout"`
+       MaxConcurrentRequests  int            `yaml:"max-concurrent-requests"`
+       RequestVolumeThreshold int            `yaml:"request-volume-threshold"`
+       SleepWindow            int            `yaml:"sleep-window"`
+       ErrorPercentThreshold  int            `yaml:"error-percent-threshold"`
+       UnknownFields          map[string]any `yaml:",remain"`
+}
+
+func (c *Config) Prefix() string {
+       return hystrixExtensionName
+}
+
+func (c *Config) New() extension.Config {
+       return &Config{
+               Commands:       make(map[string]CommandConfig),
+               defaults:       defaultCommandConfig(),
+               rollbackStates: make(map[extension.Scope]configRollbackState),
+       }
+}
+
+func (c *Config) Init(scope extension.Scope) error {
+       if _, err := filterNameForScope(scope); err != nil {
+               return err
+       }
+
+       commandNames := make([]string, 0, len(c.Commands))
+       for commandName := range c.Commands {
+               commandNames = append(commandNames, commandName)
+       }
+       sort.Strings(commandNames)
+
+       for _, commandName := range commandNames {
+               if err := validateCommandName(commandName); err != nil {
+                       return err
+               }
+               commandConfig := c.Commands[commandName]
+               if len(commandConfig.UnknownFields) > 0 {
+                       unknownFields := make([]string, 0, 
len(commandConfig.UnknownFields))
+                       for fieldName := range commandConfig.UnknownFields {
+                               unknownFields = append(unknownFields, fieldName)
+                       }
+                       sort.Strings(unknownFields)
+                       return fmt.Errorf("hystrix command %q: unknown 
field(s): %s",
+                               commandName, strings.Join(unknownFields, ", "))
+               }
+       }
+
+       c.defaults = defaultCommandConfig()
+       hasDefault := false
+       if defaultCommand, exists := c.Commands[defaultCommandName]; exists {
+               var err error
+               c.defaults, err = c.defaultedCommand(defaultCommand)
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
defaultCommandName, err)
+               }
+               hasDefault = true
+       }
+
+       configured := make(map[string]CommandConfig, len(commandNames))
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               command, err := c.defaultedCommand(c.Commands[commandName])
+               if err != nil {
+                       return fmt.Errorf("hystrix command %q: %w", 
commandName, err)
+               }
+               configured[internalCommandName(scope, commandName)] = command
+       }
+
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+       hystrixConfigGeneration++
+       generation := hystrixConfigGeneration
+       rollbackState := configRollbackState{
+               generation: generation,
+               commands:   make(map[string]commandRollbackState, 
len(configured)),
+       }
+       if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists {
+               rollbackState.previousDefault = previousDefault
+               rollbackState.hadPreviousDefault = true
+       }
+       hystrixRuntimeDefaults[scope] = runtimeDefaultState{
+               command:    toHystrixCommandConfig(c.defaults),
+               enabled:    hasDefault,
+               generation: generation,
+       }
+       existingSettings := hystrixgo.GetCircuitSettings()
+       warnUnconfiguredLegacyCommands(scope, existingSettings, configured)
+       for _, commandName := range commandNames {
+               if commandName == defaultCommandName {
+                       continue
+               }
+               internalName := internalCommandName(scope, commandName)
+               command := configured[internalName]
+               hystrixCommand := toHystrixCommandConfig(command)
+               state := commandRollbackState{configuredWith: hystrixCommand}
+               if previous, exists := existingSettings[internalName]; exists {
+                       state.previousSettings = 
commandConfigFromSettings(previous)
+                       state.hadPreviousSettings = true
+               }
+               if previous, exists := hystrixRuntimeCommands[internalName]; 
exists {
+                       state.previousRuntime = previous
+                       state.hadPreviousRuntime = true
+               }
+               rollbackState.commands[internalName] = state
+               hystrixRuntimeCommands[internalName] = 
commandRuntimeState{generation: generation, scope: scope}
+               hystrixgo.ConfigureCommand(internalName, hystrixCommand)
+       }
+       if c.rollbackStates == nil {
+               c.rollbackStates = make(map[extension.Scope]configRollbackState)
+       }
+       c.rollbackStates[scope] = rollbackState
+       return nil
+}
+
+// Rollback restores settings written by Init when a later extension fails
+// during the same initialization attempt. Hystrix keeps settings process-wide,
+// so clients or servers using the same command name also share its settings.
+func (c *Config) Rollback(scope extension.Scope) error {
+       hystrixConfigMu.Lock()
+       defer hystrixConfigMu.Unlock()
+
+       rollbackState, exists := c.rollbackStates[scope]
+       if !exists {
+               return nil
+       }
+       for commandName, state := range rollbackState.commands {
+               currentRuntime, exists := hystrixRuntimeCommands[commandName]
+               if !exists || currentRuntime.generation != 
rollbackState.generation {
+                       continue
+               }
+               current, exists := hystrixgo.GetCircuitSettings()[commandName]
+               if !exists || 
!sameCommandConfig(commandConfigFromSettings(current), state.configuredWith) {
+                       continue
+               }
+               if state.hadPreviousSettings {
+                       hystrixgo.ConfigureCommand(commandName, 
state.previousSettings)
+               } else {
+                       // hystrix-go has no per-command delete API. 
ConfigureCommand fills
+                       // every zero field from its Default* constants 
(settings.go), so an
+                       // empty config resets this command to the library 
defaults.
+                       hystrixgo.ConfigureCommand(commandName, 
hystrixgo.CommandConfig{})
+               }
+               if state.hadPreviousRuntime {
+                       hystrixRuntimeCommands[commandName] = 
state.previousRuntime
+               } else {
+                       delete(hystrixRuntimeCommands, commandName)
+               }
+       }
+       if currentDefault, exists := hystrixRuntimeDefaults[scope]; exists && 
currentDefault.generation == rollbackState.generation {
+               if rollbackState.hadPreviousDefault {
+                       hystrixRuntimeDefaults[scope] = 
rollbackState.previousDefault
+               } else {
+                       delete(hystrixRuntimeDefaults, scope)
+               }
+       }
+       for commandName, commandState := range hystrixRuntimeCommands {

Review Comment:
   🟡 **Rollback 没有撤销 lazy default 写入 hystrix-go 的设置**(review 正文第 4 条)
   
   `Rollback` 只遍历 `rollbackState.commands`(显式配置),lazy 注册的命令既没记 rollback 
state,末尾这段清理也只删 `hystrixRuntimeCommands` 的登记、不动 hystrix-go 的 settings。
   
   实测:配 `default`(425ms / 17) → 调一个未配置方法 → `Rollback` 后 runtime 登记删了,但 
`GetCircuitSettings()` 里 `dubbo:consumer:lazy.Service:::Run` 仍是 425ms / 17 
的孤儿配置。
   
   **Suggestion**: 把 lazy 注册名记进本次 generation 的状态里一并 `ConfigureCommand(name, 
CommandConfig{})`;或者在注释里明确「Rollback 只保证显式配置」并补一个用例锁住这个边界。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to