Skip to content

Commit 4516339

Browse files
committed
refactor: add default values to the configuration
1 parent 7eb5636 commit 4516339

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

Diff for: consistent.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
// THE SOFTWARE.
2323

24-
// Package consistent provides a consistent hashing function with bounded loads.
25-
// For more information about the underlying algorithm, please take a look at
26-
// https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
24+
// Package consistent provides a consistent hashing function with bounded loads. This implementation also adds
25+
// partitioning logic on top of the original algorithm. For more information about the underlying algorithm,
26+
// please take a look at https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
2727
//
2828
// Example Use:
2929
//
@@ -65,15 +65,16 @@ import (
6565
"sync"
6666
)
6767

68-
var (
69-
// ErrInsufficientMemberCount represents an error which means there are not enough members to complete the task.
70-
ErrInsufficientMemberCount = errors.New("insufficient member count")
71-
72-
// ErrMemberNotFound represents an error which means requested member could not be found in consistent hash ring.
73-
ErrMemberNotFound = errors.New("member could not be found in ring")
68+
const (
69+
DefaultPartitionCount int = 271
70+
DefaultReplicationFactor int = 20
71+
DefaultLoad float64 = 1.25
7472
)
7573

76-
// Hasher is responsible for generating unsigned, 64 bit hash of provided byte slice.
74+
// ErrInsufficientMemberCount represents an error which means there are not enough members to complete the task.
75+
var ErrInsufficientMemberCount = errors.New("insufficient member count")
76+
77+
// Hasher is responsible for generating unsigned, 64-bit hash of provided byte slice.
7778
// Hasher should minimize collisions (generating same hash for different byte slice)
7879
// and while performance is also important fast functions are preferable (i.e.
7980
// you can use FarmHash family).
@@ -88,7 +89,7 @@ type Member interface {
8889

8990
// Config represents a structure to control consistent package.
9091
type Config struct {
91-
// Hasher is responsible for generating unsigned, 64 bit hash of provided byte slice.
92+
// Hasher is responsible for generating unsigned, 64-bit hash of provided byte slice.
9293
Hasher Hasher
9394

9495
// Keys are distributed among partitions. Prime numbers are good to
@@ -120,16 +121,26 @@ type Consistent struct {
120121

121122
// New creates and returns a new Consistent object.
122123
func New(members []Member, config Config) *Consistent {
124+
if config.Hasher == nil {
125+
panic("Hasher cannot be nil")
126+
}
127+
if config.PartitionCount == 0 {
128+
config.PartitionCount = DefaultPartitionCount
129+
}
130+
if config.ReplicationFactor == 0 {
131+
config.ReplicationFactor = DefaultReplicationFactor
132+
}
133+
if config.Load == 0 {
134+
config.Load = DefaultLoad
135+
}
136+
123137
c := &Consistent{
124138
config: config,
125139
members: make(map[string]*Member),
126140
partitionCount: uint64(config.PartitionCount),
127141
ring: make(map[uint64]*Member),
128142
}
129-
if config.Hasher == nil {
130-
panic("Hasher cannot be nil")
131-
}
132-
// TODO: Check configuration here
143+
133144
c.hasher = config.Hasher
134145
for _, member := range members {
135146
c.add(member)

Diff for: consistent_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2021 Burak Sezer
1+
// Copyright (c) 2018-2022 Burak Sezer
22
// All rights reserved.
33
//
44
// This code is licensed under the MIT License.
@@ -76,7 +76,7 @@ func TestConsistentAdd(t *testing.T) {
7676
}
7777

7878
func TestConsistentRemove(t *testing.T) {
79-
members := []Member{}
79+
var members []Member
8080
for i := 0; i < 8; i++ {
8181
member := testMember(fmt.Sprintf("node%d.olric", i))
8282
members = append(members, member)
@@ -144,7 +144,7 @@ func TestConsistentLocateKey(t *testing.T) {
144144
}
145145

146146
func TestConsistentInsufficientMemberCount(t *testing.T) {
147-
members := []Member{}
147+
var members []Member
148148
for i := 0; i < 8; i++ {
149149
member := testMember(fmt.Sprintf("node%d.olric", i))
150150
members = append(members, member)
@@ -159,7 +159,7 @@ func TestConsistentInsufficientMemberCount(t *testing.T) {
159159
}
160160

161161
func TestConsistentClosestMembers(t *testing.T) {
162-
members := []Member{}
162+
var members []Member
163163
for i := 0; i < 8; i++ {
164164
member := testMember(fmt.Sprintf("node%d.olric", i))
165165
members = append(members, member)
@@ -215,6 +215,6 @@ func BenchmarkGetClosestN(b *testing.B) {
215215
b.ResetTimer()
216216
for i := 0; i < b.N; i++ {
217217
key := []byte("key" + strconv.Itoa(i))
218-
c.GetClosestN(key, 3)
218+
_, _ = c.GetClosestN(key, 3)
219219
}
220220
}

0 commit comments

Comments
 (0)