Make minor improvements to memorylocker internals

This commit is contained in:
Marius 2017-02-06 15:10:43 +01:00
parent 5491b8ff12
commit e9255c63b9
1 changed files with 5 additions and 6 deletions

View File

@ -20,8 +20,8 @@ import (
// cheap mechanism. Locks will only exist as long as this object is kept in // cheap mechanism. Locks will only exist as long as this object is kept in
// reference and will be erased if the program exits. // reference and will be erased if the program exits.
type MemoryLocker struct { type MemoryLocker struct {
locks map[string]bool locks map[string]struct{}
mutex *sync.Mutex mutex sync.Mutex
} }
// NewMemoryLocker creates a new in-memory locker. The DataStore parameter // NewMemoryLocker creates a new in-memory locker. The DataStore parameter
@ -34,8 +34,7 @@ func NewMemoryLocker(_ tusd.DataStore) *MemoryLocker {
// New creates a new in-memory locker. // New creates a new in-memory locker.
func New() *MemoryLocker { func New() *MemoryLocker {
return &MemoryLocker{ return &MemoryLocker{
locks: make(map[string]bool), locks: make(map[string]struct{}),
mutex: new(sync.Mutex),
} }
} }
@ -54,7 +53,7 @@ func (locker *MemoryLocker) LockUpload(id string) error {
return tusd.ErrFileLocked return tusd.ErrFileLocked
} }
locker.locks[id] = true locker.locks[id] = struct{}{}
return nil return nil
} }
@ -62,11 +61,11 @@ func (locker *MemoryLocker) LockUpload(id string) error {
// UnlockUpload releases a lock. If no such lock exists, no error will be returned. // UnlockUpload releases a lock. If no such lock exists, no error will be returned.
func (locker *MemoryLocker) UnlockUpload(id string) error { func (locker *MemoryLocker) UnlockUpload(id string) error {
locker.mutex.Lock() locker.mutex.Lock()
defer locker.mutex.Unlock()
// Deleting a non-existing key does not end in unexpected errors or panic // Deleting a non-existing key does not end in unexpected errors or panic
// since this operation results in a no-op // since this operation results in a no-op
delete(locker.locks, id) delete(locker.locks, id)
locker.mutex.Unlock()
return nil return nil
} }