-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshardedsf.go
52 lines (43 loc) · 1.43 KB
/
shardedsf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package shardedsingleflight
import (
"golang.org/x/sync/singleflight"
)
//ShardedGroup is a sharded singleflight Group. See singleflight.Group
type ShardedGroup struct {
newHash NewHash
shardc uint64
shards []singleflight.Group
}
//NewShardedGroup is the sharded singleflight Group contstructor. Any provided
//options, which may be absent, will be applied.
func NewShardedGroup(opts ...GroupOptions) *ShardedGroup {
sg := &ShardedGroup{
newHash: DefHashFunc,
shardc: DefShardCount,
}
for _, opt := range opts {
opt.apply(sg)
}
sg.shards = make([]singleflight.Group, sg.shardc)
return sg
}
//Do maps the key to a shard and runs the singleflight shard's Do.
// See singleflight.Do
func (sg *ShardedGroup) Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool) {
return sg.shards[sg.shardIdx(key)].Do(key, fn)
}
//DoChan maps the key to a shard and runs the singleflight shard's DoChan.
// See singleflight.DoChan
func (sg *ShardedGroup) DoChan(key string, fn func() (interface{}, error)) <-chan singleflight.Result {
return sg.shards[sg.shardIdx(key)].DoChan(key, fn)
}
//Forget maps the key to a shard and runs the singleflight shard's Forget.
// See singleflight.Forget
func (sg *ShardedGroup) Forget(key string) {
sg.shards[sg.shardIdx(key)].Forget(key)
}
func (sg *ShardedGroup) shardIdx(key string) uint64 {
hasher := sg.newHash()
hasher.Write([]byte(key))
return hasher.Sum64() % sg.shardc
}