pkg/fs: for locks, prefer defer and read-only ops

This commit prefers the use of 'defer' for fs.Unlock (and fs.RUnlock)
because it is more idiomatic Go and reduces repetition in the code,
lending to a cleaner code base.

It also switches a few uses of the lock to read-only locks, which should
improve performance of those functions dramatically in certain contexts.
This commit is contained in:
Brendan Ashworth
2016-03-06 21:32:49 -08:00
parent d54a8a9c07
commit 294ea814bf
3 changed files with 17 additions and 18 deletions

View File

@@ -58,12 +58,12 @@ func (fs Filesystem) DeleteBucket(bucket string) *probe.Error {
// Critical region hold write lock.
fs.rwLock.Lock()
defer fs.rwLock.Unlock()
delete(fs.buckets.Metadata, bucket)
if err := saveBucketsMetadata(*fs.buckets); err != nil {
fs.rwLock.Unlock()
return err.Trace(bucket)
}
fs.rwLock.Unlock()
return nil
}
@@ -171,12 +171,12 @@ func (fs Filesystem) MakeBucket(bucket, acl string) *probe.Error {
// Critical region hold a write lock.
fs.rwLock.Lock()
defer fs.rwLock.Unlock()
fs.buckets.Metadata[bucket] = bucketMetadata
if err := saveBucketsMetadata(*fs.buckets); err != nil {
fs.rwLock.Unlock()
return err.Trace(bucket)
}
fs.rwLock.Unlock()
return nil
}
@@ -266,11 +266,11 @@ func (fs Filesystem) SetBucketMetadata(bucket string, metadata map[string]string
// Critical region handle write lock.
fs.rwLock.Lock()
defer fs.rwLock.Unlock()
fs.buckets.Metadata[bucket] = bucketMetadata
if err := saveBucketsMetadata(*fs.buckets); err != nil {
fs.rwLock.Unlock()
return err.Trace(bucket)
}
fs.rwLock.Unlock()
return nil
}