2016-02-11 14:58:36 +00:00
|
|
|
// Package memorylocker provides an in-memory locking mechanism.
|
2015-12-26 20:23:09 +00:00
|
|
|
//
|
2022-03-19 22:21:17 +00:00
|
|
|
// TODO: Update comment
|
2015-12-26 20:23:09 +00:00
|
|
|
// When multiple processes are attempting to access an upload, whether it be
|
2016-09-27 20:10:16 +00:00
|
|
|
// by reading or writing, a synchronization mechanism is required to prevent
|
2015-12-26 20:23:09 +00:00
|
|
|
// 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
|
2016-09-27 20:10:16 +00:00
|
|
|
// cheap mechanism. Locks will only exist as long as this object is kept in
|
2015-12-26 20:23:09 +00:00
|
|
|
// reference and will be erased if the program exits.
|
|
|
|
package memorylocker
|
2015-12-09 19:25:08 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-19 22:21:17 +00:00
|
|
|
"context"
|
2016-03-04 21:27:44 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-06-11 16:23:20 +00:00
|
|
|
"github.com/tus/tusd/pkg/handler"
|
2015-12-09 19:25:08 +00:00
|
|
|
)
|
|
|
|
|
2015-12-18 22:20:52 +00:00
|
|
|
// MemoryLocker persists locks using memory and therefore allowing a simple and
|
2016-09-27 20:10:16 +00:00
|
|
|
// cheap mechanism. Locks will only exist as long as this object is kept in
|
2015-12-18 22:20:52 +00:00
|
|
|
// reference and will be erased if the program exits.
|
2015-12-09 19:25:08 +00:00
|
|
|
type MemoryLocker struct {
|
2022-03-19 22:21:17 +00:00
|
|
|
locks map[string]lockEntry
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type lockEntry struct {
|
|
|
|
lockReleased chan struct{}
|
|
|
|
requestRelease func()
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
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{
|
2022-03-19 22:21:17 +00:00
|
|
|
locks: make(map[string]lockEntry),
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 19:17:43 +00:00
|
|
|
// UseIn adds this locker to the passed composer.
|
2019-06-11 16:23:20 +00:00
|
|
|
func (locker *MemoryLocker) UseIn(composer *handler.StoreComposer) {
|
2019-09-12 10:37:43 +00:00
|
|
|
composer.UseLocker(locker)
|
2019-09-11 08:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (locker *MemoryLocker) NewLock(id string) (handler.Lock, error) {
|
|
|
|
return memoryLock{locker, id}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type memoryLock struct {
|
|
|
|
locker *MemoryLocker
|
|
|
|
id string
|
2016-02-21 22:25:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 13:02:46 +00:00
|
|
|
// Lock tries to obtain the exclusive lock.
|
2022-03-19 22:21:17 +00:00
|
|
|
func (lock memoryLock) Lock(ctx context.Context, requestRelease func()) error {
|
|
|
|
lock.locker.mutex.RLock()
|
|
|
|
entry, ok := lock.locker.locks[lock.id]
|
|
|
|
lock.locker.mutex.RUnlock()
|
|
|
|
|
|
|
|
requestRelease:
|
|
|
|
if ok {
|
|
|
|
// TODO: Make this channel?
|
|
|
|
// TODO: Should we ensure this is only called once?
|
|
|
|
entry.requestRelease()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return handler.ErrLockTimeout
|
|
|
|
case <-entry.lockReleased:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 08:28:38 +00:00
|
|
|
lock.locker.mutex.Lock()
|
2022-03-19 22:21:17 +00:00
|
|
|
// Check that the lock has not already been created in the meantime
|
|
|
|
entry, ok = lock.locker.locks[lock.id]
|
|
|
|
if ok {
|
|
|
|
// Lock has been created in the meantime, so we must wait again until it is free
|
|
|
|
lock.locker.mutex.Unlock()
|
|
|
|
goto requestRelease
|
|
|
|
}
|
2016-03-04 21:27:44 +00:00
|
|
|
|
2022-03-19 22:21:17 +00:00
|
|
|
// No lock exists, so we can create it
|
|
|
|
entry = lockEntry{
|
|
|
|
lockReleased: make(chan struct{}),
|
|
|
|
requestRelease: requestRelease,
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 22:21:17 +00:00
|
|
|
lock.locker.locks[lock.id] = entry
|
|
|
|
lock.locker.mutex.Unlock()
|
2015-12-09 19:25:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-01 13:02:46 +00:00
|
|
|
// Unlock releases a lock. If no such lock exists, no error will be returned.
|
2019-09-11 08:28:38 +00:00
|
|
|
func (lock memoryLock) Unlock() error {
|
|
|
|
lock.locker.mutex.Lock()
|
2016-03-04 21:27:44 +00:00
|
|
|
|
2022-03-19 22:21:17 +00:00
|
|
|
lockReleased := lock.locker.locks[lock.id].lockReleased
|
|
|
|
|
|
|
|
// Delete the lock entry entirely
|
2019-09-11 08:28:38 +00:00
|
|
|
delete(lock.locker.locks, lock.id)
|
2015-12-09 19:25:08 +00:00
|
|
|
|
2019-09-11 08:28:38 +00:00
|
|
|
lock.locker.mutex.Unlock()
|
2022-03-19 22:21:17 +00:00
|
|
|
|
|
|
|
close(lockReleased)
|
|
|
|
|
2015-12-09 19:25:08 +00:00
|
|
|
return nil
|
|
|
|
}
|