-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
75 lines (58 loc) · 1.71 KB
/
sync.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"sync"
"golang.org/x/sync/singleflight"
)
type syncMap[K comparable, V any] struct{ m sync.Map }
func (m *syncMap[K, V]) Store(k K, v V) { m.m.Store(k, v) }
func (m *syncMap[K, V]) Load(k K) (V, bool) {
got, ok := m.m.Load(k)
return got.(V), ok
}
func (m *syncMap[K, V]) LoadOrStore(k K, v V) (V, bool) {
got, ok := m.m.LoadOrStore(k, v)
return got.(V), ok
}
func (m *syncMap[K, V]) LoadAndDelete(k K) (V, bool) {
got, ok := m.m.LoadAndDelete(k)
return got.(V), ok
}
func (m *syncMap[K, V]) Delete(k K) { m.m.Delete(k) }
func (m *syncMap[K, V]) Range(f func(k K, v V) bool) {
m.m.Range(func(k, v interface{}) bool {
return f(k.(K), v.(V))
})
}
type syncPool[V any] struct{ p *sync.Pool }
func newSyncPool[V any](newFunc func() V) *syncPool[V] {
return &syncPool[V]{
p: &sync.Pool{New: func() interface{} { return newFunc() }},
}
}
func (p syncPool[V]) Get() V { return p.p.Get().(V) }
func (p syncPool[V]) Put(v V) { p.p.Put(v) }
type singleflightGroup[V any] struct {
g singleflight.Group
}
type singleflightResult[V any] struct {
Val V
Err error
Shared bool
}
func (g *singleflightGroup[V]) Do(k string, f func() (V, error)) (V, error, bool) {
v, err, shared := g.g.Do(k, func() (interface{}, error) { return f() })
return v.(V), err, shared
}
func (g *singleflightGroup[V]) DoChan(k string, f func() (V, error)) <-chan singleflightResult[V] {
rawCh := g.g.DoChan(k, func() (interface{}, error) { return f() })
ch := make(chan singleflightResult[V], 1)
go func() {
result := <-rawCh
ch <- singleflightResult[V]{Val: result.Val.(V), Err: result.Err, Shared: result.Shared}
close(ch)
}()
return ch
}
func (g *singleflightGroup[V]) Forget(k string) {
g.g.Forget(k)
}