This is an automated email from the ASF dual-hosted git repository.

alexstocks 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 b3d9983d1 fix(router/affinity): prevent panic caused by nil return in 
initialization phase (#3216)
b3d9983d1 is described below

commit b3d9983d1b93d59ca52cd0d15fb2865ed678f335
Author: Aether <[email protected]>
AuthorDate: Wed Mar 4 02:40:22 2026 +0800

    fix(router/affinity): prevent panic caused by nil return in initialization 
phase (#3216)
    
    Signed-off-by: Aetherance <[email protected]>
    Co-authored-by: Aetherance <[email protected]>
---
 cluster/router/affinity/router_test.go | 12 ++++++++++++
 cluster/router/chain/chain.go          |  7 +++++++
 common/url.go                          | 16 ++++++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/cluster/router/affinity/router_test.go 
b/cluster/router/affinity/router_test.go
index 41b294b06..3b62fa2ef 100644
--- a/cluster/router/affinity/router_test.go
+++ b/cluster/router/affinity/router_test.go
@@ -28,6 +28,7 @@ import (
 import (
        "dubbo.apache.org/dubbo-go/v3/cluster/router/condition"
        "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
        "dubbo.apache.org/dubbo-go/v3/config_center"
        "dubbo.apache.org/dubbo-go/v3/protocol/base"
        "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
@@ -235,3 +236,14 @@ affinityAware:
        }
 
 }
+
+func Test_newApplicationAffinityRouter(t *testing.T) {
+       u, _ := common.NewURL("condition://0.0.0.0/com.foo.BarService")
+       router := newApplicationAffinityRouter(u)
+       assert.Nil(t, router)
+
+       u.SetParam(constant.ApplicationKey, "test-app")
+       router = newApplicationAffinityRouter(u)
+       assert.NotNil(t, router)
+       assert.Equal(t, "test-app", router.currentApplication)
+}
diff --git a/cluster/router/chain/chain.go b/cluster/router/chain/chain.go
index c4a116882..3159f9c39 100644
--- a/cluster/router/chain/chain.go
+++ b/cluster/router/chain/chain.go
@@ -33,6 +33,7 @@ import (
 import (
        "dubbo.apache.org/dubbo-go/v3/cluster/router"
        "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
        "dubbo.apache.org/dubbo-go/v3/common/extension"
        "dubbo.apache.org/dubbo-go/v3/protocol/base"
 )
@@ -108,6 +109,12 @@ func (c *RouterChain) copyRouters() 
[]router.PriorityRouter {
 // NewRouterChain init router chain
 // Loop routerFactories and call NewRouter method
 func NewRouterChain(url *common.URL) (*RouterChain, error) {
+       if url.SubURL != nil {
+               if appName := url.SubURL.GetParam(constant.ApplicationKey, ""); 
appName != "" {
+                       url.CompareAndSwapParam(constant.ApplicationKey, "", 
appName)
+               }
+       }
+
        routerFactories := extension.GetRouterFactories()
        if len(routerFactories) == 0 {
                return nil, perrors.Errorf("No routerFactory exits , create one 
please")
diff --git a/common/url.go b/common/url.go
index 8f5cd7895..f8df908dd 100644
--- a/common/url.go
+++ b/common/url.go
@@ -564,6 +564,22 @@ func (c *URL) SetParam(key string, value string) {
        c.params.Set(key, value)
 }
 
+// CompareAndSwapParam will set the key-value pair into URL when the current 
value equals the expected value.
+// It returns true if the value was set successfully, false otherwise.
+// This is a thread-safe compare-and-swap operation.
+func (c *URL) CompareAndSwapParam(key string, expected string, newValue 
string) bool {
+       c.paramsLock.Lock()
+       defer c.paramsLock.Unlock()
+       if c.params == nil {
+               c.params = url.Values{}
+       }
+       if c.params.Get(key) == expected {
+               c.params.Set(key, newValue)
+               return true
+       }
+       return false
+}
+
 func (c *URL) SetAttribute(key string, value any) {
        c.attributesLock.Lock()
        defer c.attributesLock.Unlock()

Reply via email to