This is an automated email from the ASF dual-hosted git repository.
wuxinfan pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/develop by this push:
new 6f906d082 Multi registry verification and Use LoadOrStore instead
(#2862)
6f906d082 is described below
commit 6f906d0822b5d5d9f72e33790165d969f1efdb75
Author: 1kasa <[email protected]>
AuthorDate: Fri May 9 21:17:55 2025 +0800
Multi registry verification and Use LoadOrStore instead (#2862)
* feat:multi registry center address verification and Use loaderstore
instead
* fix: optimization logger Error print
* handle comment
* add TODO message
---
config/root_config.go | 32 ++++++++++++++++++++++++++++++++
registry/protocol/protocol.go | 15 ++++++---------
2 files changed, 38 insertions(+), 9 deletions(-)
diff --git a/config/root_config.go b/config/root_config.go
index ca4a6bb4c..94cf48ccd 100644
--- a/config/root_config.go
+++ b/config/root_config.go
@@ -18,6 +18,7 @@
package config
import (
+ "fmt"
"sync"
)
@@ -166,6 +167,11 @@ func (rc *RootConfig) Init() error {
}
}
+ // TODO:When config is migrated later, the impact of this will be
migrated to the global module
+ if err := validateRegistryAddresses(rc.Registries); err != nil {
+ return err
+ }
+
if err := rc.MetadataReport.Init(rc); err != nil {
return err
}
@@ -367,3 +373,29 @@ func (rc *RootConfig) Process(event
*config_center.ConfigChangeEvent) {
// dynamically update metric
rc.Metrics.DynamicUpdateProperties(updateRootConfig.Metrics)
}
+
+// TODO:When config is migrated later, the impact of this will be migrated to
the global module
+// validateRegistryAddresses Checks whether there are duplicate registry
addresses
+func validateRegistryAddresses(registries map[string]*RegistryConfig) error {
+ cacheKeyMap := make(map[string]string, len(registries))
+
+ for id, reg := range registries {
+ address := reg.Address
+ namespace := reg.Namespace
+
+ cacheKey := address
+ if namespace != "" {
+ cacheKey = cacheKey + "?" + constant.NacosNamespaceID +
"=" + namespace
+ }
+
+ if existingID, exists := cacheKeyMap[cacheKey]; exists {
+ err := fmt.Errorf("duplicate registry address: [%s]
used by both [%s] and [%s]", cacheKey, existingID, id)
+ logger.Errorf("duplicate registry address: [%s] used by
both [%s] and [%s]", cacheKey, existingID, id)
+ return err
+ }
+
+ cacheKeyMap[cacheKey] = id
+ }
+
+ return nil
+}
diff --git a/registry/protocol/protocol.go b/registry/protocol/protocol.go
index 7cdc547cf..9109ae371 100644
--- a/registry/protocol/protocol.go
+++ b/registry/protocol/protocol.go
@@ -79,23 +79,20 @@ func newRegistryProtocol() *registryProtocol {
}
func (proto *registryProtocol) getRegistry(registryUrl *common.URL)
registry.Registry {
- var err error
-
namespace := registryUrl.GetParam(constant.RegistryNamespaceKey, "")
cacheKey := registryUrl.PrimitiveURL
if namespace != "" {
cacheKey = cacheKey + "?" + constant.NacosNamespaceID + "=" +
namespace
}
- reg, loaded := proto.registries.Load(cacheKey)
- if !loaded {
- reg, err = extension.GetRegistry(registryUrl.Protocol,
registryUrl)
+ actualReg, _ := proto.registries.LoadOrStore(cacheKey, func() any {
+ reg, err := extension.GetRegistry(registryUrl.Protocol,
registryUrl)
if err != nil {
- logger.Errorf("Registry can not connect success,
program is going to panic.Error message is %s", err.Error())
+ logger.Errorf("Registry cannot connect successfully.
Error: %s", err.Error())
panic(err)
}
- proto.registries.Store(cacheKey, reg)
- }
- return reg.(registry.Registry)
+ return reg
+ }())
+ return actualReg.(registry.Registry)
}
func getCacheKey(invoker protocol.Invoker) string {