Skip to content

Commit d53c331

Browse files
committed
refactor: use maphash instead of xxhash3
1 parent 205393e commit d53c331

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

pkg/loadbalance/consist.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ package loadbalance
1818

1919
import (
2020
"context"
21+
"hash/maphash"
2122
"sort"
2223
"sync"
2324
"time"
2425

25-
"github.com/bytedance/gopkg/util/xxhash3"
2626
"golang.org/x/sync/singleflight"
2727

2828
"github.com/cloudwego/kitex/pkg/discovery"
2929
"github.com/cloudwego/kitex/pkg/utils"
3030
)
3131

32+
var hashSeed = maphash.MakeSeed()
33+
3234
/*
3335
type hints for sync.Map:
3436
consistBalancer -> sync.Map[entry.CacheKey]*consistInfo
@@ -156,7 +158,7 @@ func (cp *consistPicker) Next(ctx context.Context, request interface{}) discover
156158
if key == "" {
157159
return nil
158160
}
159-
res := buildConsistResult(cp.info, xxhash3.HashString(key))
161+
res := buildConsistResult(cp.info, maphash.String(hashSeed, key))
160162
return res.Primary
161163
// Todo(DMwangnima): Optimise Replica-related logic
162164
// This comment part is previous implementation considering connecting to Replica
@@ -326,7 +328,7 @@ func (cb *consistBalancer) buildVirtualNodes(rNodes []realNode) []virtualNode {
326328
}
327329
// At this point, the index inside ret should be cur + j.
328330
index := cur + j
329-
ret[index].hash = xxhash3.Hash(b)
331+
ret[index].hash = maphash.Bytes(hashSeed, b)
330332
ret[index].RealNode = &rNodes[i]
331333
}
332334
cur += vLen

pkg/remote/codec/validate_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ package codec
1919
import (
2020
"context"
2121
"errors"
22+
"hash/maphash"
2223
"strconv"
2324
"testing"
2425

25-
"github.com/bytedance/gopkg/util/xxhash3"
26-
2726
"github.com/cloudwego/kitex/internal/test"
2827
"github.com/cloudwego/kitex/pkg/kerrors"
2928
"github.com/cloudwego/kitex/pkg/remote"
@@ -44,6 +43,8 @@ func (m *mockPayloadValidator) Key(ctx context.Context) string {
4443
return "mockValidator"
4544
}
4645

46+
var hashSeed = maphash.MakeSeed()
47+
4748
func (m *mockPayloadValidator) Generate(ctx context.Context, outPayload []byte) (need bool, value string, err error) {
4849
if l := ctx.Value(mockGenerateSkipKey); l != nil {
4950
return false, "", nil
@@ -55,7 +56,7 @@ func (m *mockPayloadValidator) Generate(ctx context.Context, outPayload []byte)
5556
if l := ctx.Value(mockGenerateErrorKey); l != nil {
5657
return false, "", errors.New("mockGenerateError")
5758
}
58-
hash := xxhash3.Hash(outPayload)
59+
hash := maphash.Bytes(hashSeed, outPayload)
5960
return true, strconv.FormatInt(int64(hash), 10), nil
6061
}
6162

0 commit comments

Comments
 (0)