Skip to content

Commit

Permalink
Make minor improvements to memorylocker internals
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Feb 6, 2017
1 parent 5491b8f commit e9255c6
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions memorylocker/memorylocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
// cheap mechanism. Locks will only exist as long as this object is kept in
// reference and will be erased if the program exits.
type MemoryLocker struct {
locks map[string]bool
mutex *sync.Mutex
locks map[string]struct{}
mutex sync.Mutex
}

// NewMemoryLocker creates a new in-memory locker. The DataStore parameter
Expand All @@ -34,8 +34,7 @@ func NewMemoryLocker(_ tusd.DataStore) *MemoryLocker {
// New creates a new in-memory locker.
func New() *MemoryLocker {
return &MemoryLocker{
locks: make(map[string]bool),
mutex: new(sync.Mutex),
locks: make(map[string]struct{}),
}
}

Expand All @@ -54,19 +53,19 @@ func (locker *MemoryLocker) LockUpload(id string) error {
return tusd.ErrFileLocked
}

locker.locks[id] = true
locker.locks[id] = struct{}{}

return nil
}

// UnlockUpload releases a lock. If no such lock exists, no error will be returned.
func (locker *MemoryLocker) UnlockUpload(id string) error {
locker.mutex.Lock()
defer locker.mutex.Unlock()

// Deleting a non-existing key does not end in unexpected errors or panic
// since this operation results in a no-op
delete(locker.locks, id)

locker.mutex.Unlock()
return nil
}

0 comments on commit e9255c6

Please sign in to comment.