Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deep copy the configuration when instanceOptions flow to ServerOptions/ClientOptions #2596

Merged
merged 6 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions dubbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func (ins *Instance) NewClient(opts ...client.ClientOption) (*client.Client, err
}

var cliOpts []client.ClientOption
conCfg := ins.insOpts.Consumer
appCfg := ins.insOpts.Application
regsCfg := ins.insOpts.Registries
sdCfg := ins.insOpts.Shutdown
metricsCfg := ins.insOpts.Metrics
otelCfg := ins.insOpts.Otel
conCfg := ins.insOpts.CloneConsumer()
appCfg := ins.insOpts.CloneApplication()
regsCfg := ins.insOpts.CloneRegistries()
sdCfg := ins.insOpts.CloneShutdown()
metricsCfg := ins.insOpts.CloneMetrics()
otelCfg := ins.insOpts.CloneOtel()

if conCfg != nil {
if conCfg.Check {
Expand Down Expand Up @@ -125,12 +125,12 @@ func (ins *Instance) NewServer(opts ...server.ServerOption) (*server.Server, err
}

var srvOpts []server.ServerOption
appCfg := ins.insOpts.Application
regsCfg := ins.insOpts.Registries
prosCfg := ins.insOpts.Protocols
sdCfg := ins.insOpts.Shutdown
metricsCfg := ins.insOpts.Metrics
otelCfg := ins.insOpts.Otel
appCfg := ins.insOpts.CloneApplication()
regsCfg := ins.insOpts.CloneRegistries()
prosCfg := ins.insOpts.CloneProtocols()
sdCfg := ins.insOpts.CloneShutdown()
metricsCfg := ins.insOpts.CloneMetrics()
otelCfg := ins.insOpts.CloneOtel()

if appCfg != nil {
srvOpts = append(srvOpts,
Expand Down
15 changes: 15 additions & 0 deletions global/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ func DefaultApplicationConfig() *ApplicationConfig {
// return a new config without setting any field means there is not any default value for initialization
return &ApplicationConfig{}
}

// Clone a new ApplicationConfig
func (c *ApplicationConfig) Clone() *ApplicationConfig {
return &ApplicationConfig{
Organization: c.Organization,
Name: c.Name,
Module: c.Module,
Group: c.Group,
Version: c.Version,
Owner: c.Owner,
Environment: c.Environment,
MetadataType: c.MetadataType,
Tag: c.Tag,
}
}
23 changes: 23 additions & 0 deletions global/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,26 @@ func DefaultCenterConfig() *CenterConfig {
Params: make(map[string]string),
}
}

// Clone a new CenterConfig
func (c *CenterConfig) Clone() *CenterConfig {
newParams := make(map[string]string, len(c.Params))
for k, v := range c.Params {
newParams[k] = v
}

return &CenterConfig{
Protocol: c.Protocol,
Address: c.Address,
DataId: c.DataId,
Cluster: c.Cluster,
Group: c.Group,
Username: c.Username,
Password: c.Password,
Namespace: c.Namespace,
AppID: c.AppID,
Timeout: c.Timeout,
Params: newParams,
FileExtension: c.FileExtension,
}
}
150 changes: 150 additions & 0 deletions global/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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 global

import "testing"

func TestCloneDefaultConfig(t *testing.T) {
t.Run("ApplicationConfig", func(t *testing.T) {
c := DefaultApplicationConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ApplicationConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("ConfigCenterConfig", func(t *testing.T) {
c := DefaultCenterConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ConfigCenterConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("ConsumerConfig", func(t *testing.T) {
c := DefaultConsumerConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ConsumerConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("CustomConfig", func(t *testing.T) {
c := DefaultCustomConfig()
clone := c.Clone()
if clone == c {
t.Errorf("CustomConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("LoggerConfig", func(t *testing.T) {
c := DefaultLoggerConfig()
clone := c.Clone()
if clone == c {
t.Errorf("LoggerConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("MetadataReportConfig", func(t *testing.T) {
c := DefaultMetadataReportConfig()
clone := c.Clone()
if clone == c {
t.Errorf("MetadataReportConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("MethodConfig", func(t *testing.T) {
c := &MethodConfig{}
clone := c.Clone()
if clone == c {
t.Errorf("MethodConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("MetricConfig", func(t *testing.T) {
c := DefaultMetricsConfig()
clone := c.Clone()
if clone == c {
t.Errorf("MetricConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("OtelConfig", func(t *testing.T) {
c := DefaultOtelConfig()
clone := c.Clone()
if clone == c {
t.Errorf("OtelConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("ProfilesConfig", func(t *testing.T) {
c := DefaultProfilesConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ProfilesConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("ProtocolConfig", func(t *testing.T) {
c := DefaultProtocolConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ProtocolConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("ProviderConfig", func(t *testing.T) {
c := DefaultProviderConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ProviderConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("ReferenceConfig", func(t *testing.T) {
c := DefaultReferenceConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ReferenceConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("RegistryConfig", func(t *testing.T) {
c := DefaultRegistryConfig()
clone := c.Clone()
if clone == c {
t.Errorf("RegistryConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("ServiceConfig", func(t *testing.T) {
c := DefaultServiceConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ServiceConfig.Clone() = %v, want %v", clone, c)
}
})

t.Run("ShutdownConfig", func(t *testing.T) {
c := DefaultShutdownConfig()
clone := c.Clone()
if clone == c {
t.Errorf("ShutdownConfig.Clone() = %v, want %v", clone, c)
}
})
}
26 changes: 26 additions & 0 deletions global/consumer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,29 @@ func DefaultConsumerConfig() *ConsumerConfig {
References: make(map[string]*ReferenceConfig),
}
}

// Clone a new ConsumerConfig
func (c *ConsumerConfig) Clone() *ConsumerConfig {
newRegistryIDs := make([]string, len(c.RegistryIDs))
copy(newRegistryIDs, c.RegistryIDs)

newReferences := make(map[string]*ReferenceConfig, len(c.References))
for k, v := range c.References {
newReferences[k] = v.Clone()
}

return &ConsumerConfig{
Filter: c.Filter,
RegistryIDs: newRegistryIDs,
Protocol: c.Protocol,
RequestTimeout: c.RequestTimeout,
ProxyFactory: c.ProxyFactory,
Check: c.Check,
AdaptiveService: c.AdaptiveService,
References: newReferences,
TracingKey: c.TracingKey,
FilterConf: c.FilterConf,
MaxWaitTimeForServiceDiscovery: c.MaxWaitTimeForServiceDiscovery,
MeshEnabled: c.MeshEnabled,
}
}
16 changes: 15 additions & 1 deletion global/custom_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,19 @@ type CustomConfig struct {
}

func DefaultCustomConfig() *CustomConfig {
return &CustomConfig{}
return &CustomConfig{
ConfigMap: make(map[string]interface{}),
}
}

// Clone a new CustomConfig
func (c *CustomConfig) Clone() *CustomConfig {
newConfigMap := make(map[string]interface{}, len(c.ConfigMap))
for k, v := range c.ConfigMap {
newConfigMap[k] = v
}

return &CustomConfig{
ConfigMap: newConfigMap,
}
}
33 changes: 33 additions & 0 deletions global/logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,36 @@ func DefaultLoggerConfig() *LoggerConfig {

return cfg
}

// Clone a new LoggerConfig
func (c *LoggerConfig) Clone() *LoggerConfig {
var newFile *File
if c.File != nil {
newFile = c.File.Clone()
}

return &LoggerConfig{
Driver: c.Driver,
Level: c.Level,
Format: c.Format,
Appender: c.Appender,
File: newFile,
}
}

// Clone a new File
func (f *File) Clone() *File {
var newCompress *bool
if f.Compress != nil {
newCompress = new(bool)
*newCompress = *f.Compress
}

return &File{
Name: f.Name,
MaxSize: f.MaxSize,
MaxBackups: f.MaxBackups,
MaxAge: f.MaxAge,
Compress: f.Compress,
}
}
19 changes: 19 additions & 0 deletions global/metadata_report_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@ func DefaultMetadataReportConfig() *MetadataReportConfig {
// return a new config without setting any field means there is not any default value for initialization
return &MetadataReportConfig{Params: map[string]string{}}
}

// Clone a new MetadataReportConfig
func (c *MetadataReportConfig) Clone() *MetadataReportConfig {
newParams := make(map[string]string, len(c.Params))
for k, v := range c.Params {
newParams[k] = v
}

return &MetadataReportConfig{
Protocol: c.Protocol,
Address: c.Address,
Username: c.Username,
Password: c.Password,
Timeout: c.Timeout,
Group: c.Group,
Namespace: c.Namespace,
Params: newParams,
}
}
17 changes: 17 additions & 0 deletions global/method_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ type MethodConfig struct {
Sticky bool `yaml:"sticky" json:"sticky,omitempty" property:"sticky"`
RequestTimeout string `yaml:"timeout" json:"timeout,omitempty" property:"timeout"`
}

// Clone a new MethodConfig
func (c *MethodConfig) Clone() *MethodConfig {
return &MethodConfig{
Name: c.Name,
Retries: c.Retries,
LoadBalance: c.LoadBalance,
Weight: c.Weight,
TpsLimitInterval: c.TpsLimitInterval,
TpsLimitRate: c.TpsLimitRate,
TpsLimitStrategy: c.TpsLimitStrategy,
ExecuteLimit: c.ExecuteLimit,
ExecuteLimitRejectedHandler: c.ExecuteLimitRejectedHandler,
Sticky: c.Sticky,
RequestTimeout: c.RequestTimeout,
}
}
Loading
Loading