This is an automated email from the ASF dual-hosted git repository.
Alanxtl 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 fcbbd0920 fix: toKey now uses configured hash.arguments indexes
instead of slice positions (#3432)
fcbbd0920 is described below
commit fcbbd092031d3b9e7f165fc2973c277a0d6791fa
Author: Stefano Maffeis <[email protected]>
AuthorDate: Wed Jun 17 12:17:46 2026 +0200
fix: toKey now uses configured hash.arguments indexes instead of slice
positions (#3432)
* fix: toKey now uses configured hash.arguments indexes instead of slice
positions
Previously toKey iterated with `for i := range c.argumentIndex` which
used the slice index (0,1,2,...) as the argument position to read from
args. With a configuration like hash.arguments=1, this still read
args[0] instead of args[1], making consistent hashing route by the
wrong argument.
Also replaced the unsafe args[i].(string) type assertion with
fmt.Fprint to avoid panicking on non-string arguments.
Fixes: #3431
Signed-off-by: RepoScout <[email protected]>
Co-Authored-By: Paperclip <[email protected]>
* fix: toKey now uses configured hash.arguments indexes and avoids
fmt.Fprint for strings
Previously toKey iterated with `for i := range c.argumentIndex` which
used the slice index (0,1,2,...) as the argument position to read from
args. With a configuration like hash.arguments=1, this still read
args[0] instead of args[1].
Added a type switch so the common string case uses the cheaper
sb.WriteString instead of fmt.Fprint.
Fixes: #3431
Signed-off-by: RepoScout <[email protected]>
Co-Authored-By: Paperclip <[email protected]>
---------
Signed-off-by: RepoScout <[email protected]>
Co-authored-by: RepoScout <[email protected]>
Co-authored-by: Paperclip <[email protected]>
---
.../loadbalance/consistenthashing/loadbalance_test.go | 18 ++++++++++++++++++
cluster/loadbalance/consistenthashing/selector.go | 9 +++++++--
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/cluster/loadbalance/consistenthashing/loadbalance_test.go
b/cluster/loadbalance/consistenthashing/loadbalance_test.go
index 95b4a71a5..e8a0e6523 100644
--- a/cluster/loadbalance/consistenthashing/loadbalance_test.go
+++ b/cluster/loadbalance/consistenthashing/loadbalance_test.go
@@ -68,6 +68,24 @@ func (s *consistentHashSelectorSuite) TestToKey() {
s.Equal("usernameage", result)
}
+// TestToKeySingleNonZeroIndex verifies that a single non-zero hash.arguments
+// index produces a key from the correct argument position.
+func TestToKeyWithArgumentIndex(t *testing.T) {
+ var invokers []base.Invoker
+ // hash.arguments=1 means only the second argument (index 1) is used
+ url, err :=
common.NewURL("dubbo://192.168.1.0:20000/org.apache.demo.HelloService?methods.echo.hash.arguments=1")
+ if err != nil {
+ t.Fatal(err)
+ }
+ invokers = append(invokers, base.NewBaseInvoker(url))
+ sel := newSelector(invokers, "echo", 999944)
+
+ key := sel.toKey([]any{"ignored", "used"})
+ if key != "used" {
+ t.Errorf("expected key %q, got %q", "used", key)
+ }
+}
+
func (s *consistentHashSelectorSuite) TestSelectForKey() {
url1, _ := common.NewURL(url8080Short)
url2, _ := common.NewURL(url8081Short)
diff --git a/cluster/loadbalance/consistenthashing/selector.go
b/cluster/loadbalance/consistenthashing/selector.go
index 86808a85e..51c2763ec 100644
--- a/cluster/loadbalance/consistenthashing/selector.go
+++ b/cluster/loadbalance/consistenthashing/selector.go
@@ -83,9 +83,14 @@ func (c *selector) Select(invocation base.Invocation)
base.Invoker {
func (c *selector) toKey(args []any) string {
var sb strings.Builder
- for i := range c.argumentIndex {
+ for _, i := range c.argumentIndex {
if i >= 0 && i < len(args) {
- _, _ = fmt.Fprint(&sb, args[i])
+ switch v := args[i].(type) {
+ case string:
+ sb.WriteString(v)
+ default:
+ _, _ = fmt.Fprint(&sb, v)
+ }
}
}
return sb.String()