XnLemon commented on code in PR #32: URL: https://github.com/apache/dubbo-go-extensions/pull/32#discussion_r4578476901
########## 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: Fixed in `33308f8`. Both READMEs now explain that `default` also fills omitted or zero-valued fields in method-specific entries, and that zero means “unset” and cannot be configured explicitly. <details> <summary>中文</summary> 已在 `33308f8` 修复。中英文 README 已说明 `default` 也会填充具体 method 配置中省略或为零的字段;零表示“未设置”,不能显式配置为零。 </details> -- 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]
