Skip to content
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
12 changes: 12 additions & 0 deletions cluster/router/affinity/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
7 changes: 7 additions & 0 deletions cluster/router/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down
16 changes: 16 additions & 0 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading