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 all 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
98 changes: 98 additions & 0 deletions dubbo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 dubbo

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"dubbo.apache.org/dubbo-go/v3/client"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/registry"
"dubbo.apache.org/dubbo-go/v3/server"
)

// TestIndependentConfig tests the configurations of the `instance`, `client`, and `server` are independent.
func TestIndependentConfig(t *testing.T) {
// instance configuration
ins, err := NewInstance(
WithName("dubbo_test"),
WithRegistry(
registry.WithZookeeper(),
registry.WithAddress("127.0.0.1:2181"),
),
)
if err != nil {
panic(err)
}

// client configuration, ensure that the `instance` configuration can be passed to the `client`.
_, err = ins.NewClient(
func(options *client.ClientOptions) {
assert.Equal(t, "dubbo_test", options.Application.Name)
options.Application.Name = "dubbo_test_client"
assert.Equal(t, "dubbo_test_client", options.Application.Name)

assert.Equal(t, "127.0.0.1:2181", options.Registries[constant.ZookeeperKey].Address)
options.Registries[constant.ZookeeperKey].Address = "127.0.0.1:2182"
assert.Equal(t, "127.0.0.1:2182", options.Registries[constant.ZookeeperKey].Address)
},
)
if err != nil {
panic(err)
}

// server configuration, ensure that the `instance` configuration can be passed to the `server`, and the
// `instance` configuration is not affected by the `client` configuration.
_, err = ins.NewServer(
func(options *server.ServerOptions) {
assert.Equal(t, "dubbo_test", options.Application.Name)
options.Application.Name = "dubbo_test_server"
assert.Equal(t, "dubbo_test_server", options.Application.Name)

assert.Equal(t, "127.0.0.1:2181", options.Registries[constant.ZookeeperKey].Address)
options.Registries[constant.ZookeeperKey].Address = "127.0.0.1:2183"
assert.Equal(t, "127.0.0.1:2183", options.Registries[constant.ZookeeperKey].Address)
},
)
if err != nil {
panic(err)
}

// check client configuration again, ensure that the `instance` and `client` configuration is not affected
// by the `server` configuration.
_, err = ins.NewClient(
func(options *client.ClientOptions) {
assert.Equal(t, "dubbo_test", options.Application.Name)
options.Application.Name = "dubbo_test_client"
assert.Equal(t, "dubbo_test_client", options.Application.Name)

assert.Equal(t, "127.0.0.1:2181", options.Registries[constant.ZookeeperKey].Address)
options.Registries[constant.ZookeeperKey].Address = "127.0.0.1:2184"
assert.Equal(t, "127.0.0.1:2184", options.Registries[constant.ZookeeperKey].Address)
},
)
if err != nil {
panic(err)
}
}
19 changes: 19 additions & 0 deletions global/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@ 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 {
if c == nil {
return nil
}

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,
}
}
27 changes: 27 additions & 0 deletions global/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,30 @@ func DefaultCenterConfig() *CenterConfig {
Params: make(map[string]string),
}
}

// Clone a new CenterConfig
func (c *CenterConfig) Clone() *CenterConfig {
if c == nil {
return nil
}

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,
}
}
Loading
Loading