From 972436e27dccf0d373ecee001232b84afaf1245f Mon Sep 17 00:00:00 2001 From: liqingqiya Date: Thu, 21 May 2020 17:40:39 +0800 Subject: [PATCH] fix bug: delete key in defer function fix bug: delete key in defer function --- singleflight/singleflight.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/singleflight/singleflight.go b/singleflight/singleflight.go index ff2c2ee4..e6e467ea 100644 --- a/singleflight/singleflight.go +++ b/singleflight/singleflight.go @@ -53,12 +53,15 @@ func (g *Group) Do(key string, fn func() (interface{}, error)) (interface{}, err g.m[key] = c g.mu.Unlock() - c.val, c.err = fn() - c.wg.Done() + afterFn := func() { + c.wg.Done() + g.mu.Lock() + delete(g.m, key) + g.mu.Unlock() + } + defer afterFn() - g.mu.Lock() - delete(g.m, key) - g.mu.Unlock() + c.val, c.err = fn() return c.val, c.err }