Skip to content

Commit da3d7c0

Browse files
author
Yusuke Kato
authored
[patch] bugfix of race condition (#75)
Signed-off-by: kpango <[email protected]>
1 parent 4af29cf commit da3d7c0

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

gache.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type (
6464
expFunc func(context.Context, string)
6565
expFuncEnabled bool
6666
expGroup singleflight.Group
67-
cancel context.CancelFunc
67+
cancel atomic.Value
6868
expire int64
6969
l uint64
7070
shards [slen]*Map
@@ -170,7 +170,8 @@ func SetExpiredHook(f func(context.Context, string)) Gache {
170170
func (g *gache) StartExpired(ctx context.Context, dur time.Duration) Gache {
171171
go func() {
172172
tick := time.NewTicker(dur)
173-
ctx, g.cancel = context.WithCancel(ctx)
173+
ctx, cancel := context.WithCancel(ctx)
174+
g.cancel.Store(cancel)
174175
for {
175176
select {
176177
case <-ctx.Done():
@@ -428,8 +429,8 @@ func Read(r io.Reader) error {
428429

429430
// Stop kills expire daemon
430431
func (g *gache) Stop() {
431-
if g.cancel != nil {
432-
g.cancel()
432+
if cancel := g.cancel.Load().(context.CancelFunc); cancel != nil {
433+
cancel()
433434
}
434435
}
435436

0 commit comments

Comments
 (0)