Avoid race conditions from mismatched use of atomic & sync.Mutex

This commit is contained in:
Dane Everitt 2020-12-20 13:13:10 -08:00
parent c8d297a056
commit f8282c56cb
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -35,17 +35,12 @@ func (ult *usageLookupTime) Get() time.Time {
// Returns the maximum amount of disk space that this Filesystem instance is allowed to use. // Returns the maximum amount of disk space that this Filesystem instance is allowed to use.
func (fs *Filesystem) MaxDisk() int64 { func (fs *Filesystem) MaxDisk() int64 {
fs.mu.RLock() return atomic.LoadInt64(&fs.diskLimit)
defer fs.mu.RUnlock()
return fs.diskLimit
} }
// Sets the disk space limit for this Filesystem instance. // Sets the disk space limit for this Filesystem instance.
func (fs *Filesystem) SetDiskLimit(i int64) { func (fs *Filesystem) SetDiskLimit(i int64) {
fs.mu.Lock() atomic.SwapInt64(&fs.diskLimit, i)
fs.diskLimit = i
fs.mu.Unlock()
} }
// The same concept as HasSpaceAvailable however this will return an error if there is // The same concept as HasSpaceAvailable however this will return an error if there is
@ -87,10 +82,7 @@ func (fs *Filesystem) HasSpaceAvailable(allowStaleValue bool) bool {
// function for critical logical checks. It should only be used in areas where the actual disk usage // function for critical logical checks. It should only be used in areas where the actual disk usage
// does not need to be perfect, e.g. API responses for server resource usage. // does not need to be perfect, e.g. API responses for server resource usage.
func (fs *Filesystem) CachedUsage() int64 { func (fs *Filesystem) CachedUsage() int64 {
fs.mu.RLock() return atomic.LoadInt64(&fs.diskUsed)
defer fs.mu.RUnlock()
return fs.diskUsed
} }
// Internal helper function to allow other parts of the codebase to check the total used disk space // Internal helper function to allow other parts of the codebase to check the total used disk space