21
21
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
22
// THE SOFTWARE.
23
23
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
27
27
//
28
28
// Example Use:
29
29
//
@@ -65,15 +65,16 @@ import (
65
65
"sync"
66
66
)
67
67
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
74
72
)
75
73
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.
77
78
// Hasher should minimize collisions (generating same hash for different byte slice)
78
79
// and while performance is also important fast functions are preferable (i.e.
79
80
// you can use FarmHash family).
@@ -88,7 +89,7 @@ type Member interface {
88
89
89
90
// Config represents a structure to control consistent package.
90
91
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.
92
93
Hasher Hasher
93
94
94
95
// Keys are distributed among partitions. Prime numbers are good to
@@ -120,16 +121,26 @@ type Consistent struct {
120
121
121
122
// New creates and returns a new Consistent object.
122
123
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
+
123
137
c := & Consistent {
124
138
config : config ,
125
139
members : make (map [string ]* Member ),
126
140
partitionCount : uint64 (config .PartitionCount ),
127
141
ring : make (map [uint64 ]* Member ),
128
142
}
129
- if config .Hasher == nil {
130
- panic ("Hasher cannot be nil" )
131
- }
132
- // TODO: Check configuration here
143
+
133
144
c .hasher = config .Hasher
134
145
for _ , member := range members {
135
146
c .add (member )
0 commit comments