From e339c9ff8f0694fd341f84814ca390aa71a97c64 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 22 Aug 2019 20:12:13 +0100 Subject: [PATCH] lib/bucket: shorten locking window where possible --- lib/bucket/bucket.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/bucket/bucket.go b/lib/bucket/bucket.go index 45065c474..2ebc14d33 100644 --- a/lib/bucket/bucket.go +++ b/lib/bucket/bucket.go @@ -78,16 +78,16 @@ type ( // // If f returns an error we assume the bucket was not created func (c *Cache) Create(bucket string, create CreateFn, exists ExistsFn) (err error) { - c.createMu.Lock() - defer c.createMu.Unlock() - c.mu.Lock() - defer c.mu.Unlock() - // if we are at the root, then it is OK if bucket == "" { return nil } + c.createMu.Lock() + defer c.createMu.Unlock() + c.mu.Lock() + defer c.mu.Unlock() + // if have exists fuction and bucket has been deleted, check // it still exists if created, ok := c.status[bucket]; ok && !created && exists != nil { @@ -124,16 +124,16 @@ func (c *Cache) Create(bucket string, create CreateFn, exists ExistsFn) (err err // // If the bucket has already been deleted it returns ErrAlreadyDeleted func (c *Cache) Remove(bucket string, f func() error) error { - c.removeMu.Lock() - defer c.removeMu.Unlock() - c.mu.Lock() - defer c.mu.Unlock() - // if we are at the root, then it is OK if bucket == "" { return nil } + c.removeMu.Lock() + defer c.removeMu.Unlock() + c.mu.Lock() + defer c.mu.Unlock() + // If bucket already deleted then it is OK if created, ok := c.status[bucket]; ok && !created { return ErrAlreadyDeleted