-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf.go
98 lines (83 loc) · 1.6 KB
/
sf.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sf
import (
"context"
"sync"
"sync/atomic"
"golang.org/x/sync/singleflight"
)
var disabled uint32
func SetDisable(value bool) {
if value {
atomic.StoreUint32(&disabled, 1)
} else {
atomic.StoreUint32(&disabled, 0)
}
}
func isDisabled() bool {
return atomic.LoadUint32(&disabled) == 1
}
type groupItem struct {
cancel context.CancelFunc
count uint64
}
var (
g singleflight.Group
mu sync.Mutex
shareGroup = make(map[string]*groupItem)
)
func Do[T any](ctx context.Context, key string, fn func(context.Context) (T, error)) (T, error, bool) {
if isDisabled() {
r, err := fn(ctx)
return r, err, false
}
var (
r any
err error
shared bool
done = make(chan struct{})
)
mu.Lock()
if shareGroup[key] == nil {
shareGroup[key] = &groupItem{}
}
sg := shareGroup[key]
sg.count++
go func() {
r, err, shared = g.Do(key, func() (any, error) {
var nctx context.Context
nctx, sg.cancel = context.WithCancel(context.WithoutCancel(ctx))
return fn(nctx)
})
close(done)
}()
mu.Unlock()
select {
case <-ctx.Done():
case <-done:
}
mu.Lock()
sg.count--
if sg.count == 0 {
delete(shareGroup, key)
if sg.cancel != nil {
sg.cancel()
}
}
mu.Unlock()
if err != nil {
return *new(T), err, false
}
return r.(T), nil, shared
}
func DoVoid(ctx context.Context, key string, fn func(context.Context) error) (error, bool) {
_, err, shared := Do(ctx, key, func(ctx context.Context) (struct{}, error) {
return struct{}{}, fn(ctx)
})
return err, shared
}
func Forget(key string) {
mu.Lock()
delete(shareGroup, key)
g.Forget(key)
mu.Unlock()
}