tusd/lockingstore/memorylocker.go

42 lines
1016 B
Go
Raw Normal View History

package lockingstore
import (
"github.com/tus/tusd"
)
2015-12-18 22:20:52 +00:00
// MemoryLocker persists locks using memory and therefore allowing a simple and
// cheap mechansim. 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
}
2015-12-18 22:20:52 +00:00
// New creates a new lock memory persistor.
2015-12-18 22:21:13 +00:00
func NewMemoryLocker() *MemoryLocker {
return &MemoryLocker{
locks: make(map[string]bool),
}
}
2015-12-18 22:20:52 +00:00
// LockUpload tries to obtain the exclusive lock.
func (locker *MemoryLocker) LockUpload(id string) error {
// Ensure file is not locked
if _, ok := locker.locks[id]; ok {
return tusd.ErrFileLocked
}
2015-12-09 19:48:41 +00:00
locker.locks[id] = true
return nil
}
2015-12-18 22:20:52 +00:00
// UnlockUpload releases a lock. If no such lock exists, no error will be returned.
func (locker *MemoryLocker) UnlockUpload(id string) error {
// 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)
return nil
}