From 0c094cb2d778021cf4482d01ace96387a2317777 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 19 Feb 2021 18:14:34 +0800 Subject: [PATCH] fix golint issues in core/hash (#487) --- core/hash/consistenthash.go | 13 ++++++++++--- core/hash/consistenthash_test.go | 22 +++++++++++----------- core/hash/hash.go | 3 +++ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/core/hash/consistenthash.go b/core/hash/consistenthash.go index 7a652fd0..b719a672 100644 --- a/core/hash/consistenthash.go +++ b/core/hash/consistenthash.go @@ -11,6 +11,7 @@ import ( ) const ( + // TopWeight is the top weight that one entry might set. TopWeight = 100 minReplicas = 100 @@ -18,10 +19,12 @@ const ( ) type ( - HashFunc func(data []byte) uint64 + // Func defines the hash method. + Func func(data []byte) uint64 + // A ConsistentHash is a ring hash implementation. ConsistentHash struct { - hashFunc HashFunc + hashFunc Func replicas int keys []uint64 ring map[uint64][]interface{} @@ -30,11 +33,13 @@ type ( } ) +// NewConsistentHash returns a ConsistentHash. func NewConsistentHash() *ConsistentHash { return NewCustomConsistentHash(minReplicas, Hash) } -func NewCustomConsistentHash(replicas int, fn HashFunc) *ConsistentHash { +// NewCustomConsistentHash returns a ConsistentHash with given replicas and hash func. +func NewCustomConsistentHash(replicas int, fn Func) *ConsistentHash { if replicas < minReplicas { replicas = minReplicas } @@ -92,6 +97,7 @@ func (h *ConsistentHash) AddWithWeight(node interface{}, weight int) { h.AddWithReplicas(node, replicas) } +// Get returns the corresponding node from h base on the given v. func (h *ConsistentHash) Get(v interface{}) (interface{}, bool) { h.lock.RLock() defer h.lock.RUnlock() @@ -118,6 +124,7 @@ func (h *ConsistentHash) Get(v interface{}) (interface{}, bool) { } } +// Remove removes the given node from h. func (h *ConsistentHash) Remove(node interface{}) { nodeRepr := repr(node) diff --git a/core/hash/consistenthash_test.go b/core/hash/consistenthash_test.go index 162d1704..989a8d51 100644 --- a/core/hash/consistenthash_test.go +++ b/core/hash/consistenthash_test.go @@ -132,8 +132,8 @@ func TestConsistentHash_RemoveInterface(t *testing.T) { assert.Equal(t, 1, len(ch.nodes)) node, ok := ch.Get(1) assert.True(t, ok) - assert.Equal(t, key, node.(*MockNode).Addr) - assert.Equal(t, 2, node.(*MockNode).Id) + assert.Equal(t, key, node.(*mockNode).addr) + assert.Equal(t, 2, node.(*mockNode).id) } func getKeysBeforeAndAfterFailure(t *testing.T, prefix string, index int) (map[int]string, map[int]string) { @@ -164,18 +164,18 @@ func getKeysBeforeAndAfterFailure(t *testing.T, prefix string, index int) (map[i return keys, newKeys } -type MockNode struct { - Addr string - Id int +type mockNode struct { + addr string + id int } -func newMockNode(addr string, id int) *MockNode { - return &MockNode{ - Addr: addr, - Id: id, +func newMockNode(addr string, id int) *mockNode { + return &mockNode{ + addr: addr, + id: id, } } -func (n *MockNode) String() string { - return n.Addr +func (n *mockNode) String() string { + return n.addr } diff --git a/core/hash/hash.go b/core/hash/hash.go index 3cc562fd..8bd87b59 100644 --- a/core/hash/hash.go +++ b/core/hash/hash.go @@ -7,16 +7,19 @@ import ( "github.com/spaolacci/murmur3" ) +// Hash returns the hash value of data. func Hash(data []byte) uint64 { return murmur3.Sum64(data) } +// Md5 returns the md5 bytes of data. func Md5(data []byte) []byte { digest := md5.New() digest.Write(data) return digest.Sum(nil) } +// Md5Hex returns the md5 hex string of data. func Md5Hex(data []byte) string { return fmt.Sprintf("%x", Md5(data)) }