2016-02-11 14:58:36 +00:00
|
|
|
// Package memorylocker provides an in-memory locking mechanism.
|
2015-12-26 20:23:09 +00:00
|
|
|
//
|
|
|
|
// When multiple processes are attempting to access an upload, whether it be
|
|
|
|
// by reading or writing, a syncronization mechanism is required to prevent
|
|
|
|
// data corruption, especially to ensure correct offset values and the proper
|
|
|
|
// order of chunks inside a single upload.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
package memorylocker
|
2015-12-09 19:25:08 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-04 21:27:44 +00:00
|
|
|
"sync"
|
|
|
|
|
2015-12-09 19:25:08 +00:00
|
|
|
"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.
|
2015-12-09 19:25:08 +00:00
|
|
|
type MemoryLocker struct {
|
|
|
|
locks map[string]bool
|
2016-03-04 21:27:44 +00:00
|
|
|
mutex *sync.Mutex
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 21:25:15 +00:00
|
|
|
// NewMemoryLocker creates a new in-memory locker. The DataStore parameter
|
|
|
|
// is only presented for back-wards compatibility and is ignored. Please
|
|
|
|
// use the New() function instead.
|
2016-02-21 22:25:35 +00:00
|
|
|
func NewMemoryLocker(_ tusd.DataStore) *MemoryLocker {
|
|
|
|
return New()
|
|
|
|
}
|
|
|
|
|
2016-03-04 21:25:15 +00:00
|
|
|
// New creates a new in-memory locker.
|
2016-02-21 22:25:35 +00:00
|
|
|
func New() *MemoryLocker {
|
2015-12-09 19:25:08 +00:00
|
|
|
return &MemoryLocker{
|
2016-02-21 22:25:35 +00:00
|
|
|
locks: make(map[string]bool),
|
2016-03-04 21:27:44 +00:00
|
|
|
mutex: new(sync.Mutex),
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-21 22:25:35 +00:00
|
|
|
func (locker *MemoryLocker) UseIn(composer *tusd.StoreComposer) {
|
|
|
|
composer.UseLocker(locker)
|
|
|
|
}
|
|
|
|
|
2015-12-18 22:20:52 +00:00
|
|
|
// LockUpload tries to obtain the exclusive lock.
|
2015-12-09 19:25:08 +00:00
|
|
|
func (locker *MemoryLocker) LockUpload(id string) error {
|
2016-03-04 21:27:44 +00:00
|
|
|
locker.mutex.Lock()
|
|
|
|
defer locker.mutex.Unlock()
|
|
|
|
|
2015-12-09 19:25:08 +00:00
|
|
|
// 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
|
2015-12-09 19:25:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-18 22:20:52 +00:00
|
|
|
// UnlockUpload releases a lock. If no such lock exists, no error will be returned.
|
2015-12-09 19:25:08 +00:00
|
|
|
func (locker *MemoryLocker) UnlockUpload(id string) error {
|
2016-03-04 21:27:44 +00:00
|
|
|
locker.mutex.Lock()
|
|
|
|
defer locker.mutex.Unlock()
|
|
|
|
|
2015-12-09 19:25:08 +00:00
|
|
|
// 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
|
|
|
|
}
|