From 65072acb79c27aa6b9425a7d49187353f3eb983b Mon Sep 17 00:00:00 2001 From: Marius Date: Wed, 11 Sep 2019 10:28:38 +0200 Subject: [PATCH] memorylocker: Implement new locker interface --- pkg/filelocker/filelocker.go | 2 ++ pkg/memorylocker/memorylocker.go | 30 ++++++++++++++++++--------- pkg/memorylocker/memorylocker_test.go | 21 +++++++++++++------ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/pkg/filelocker/filelocker.go b/pkg/filelocker/filelocker.go index 78ae8cb..a1322af 100644 --- a/pkg/filelocker/filelocker.go +++ b/pkg/filelocker/filelocker.go @@ -35,6 +35,8 @@ func New(path string) FileLocker { return FileLocker{path} } +// TODO: Add UseIn method + func (locker FileLocker) NewLock(id string) (handler.Lock, error) { path, err := filepath.Abs(filepath.Join(locker.Path, id+".lock")) if err != nil { diff --git a/pkg/memorylocker/memorylocker.go b/pkg/memorylocker/memorylocker.go index 21add11..f2ac94e 100644 --- a/pkg/memorylocker/memorylocker.go +++ b/pkg/memorylocker/memorylocker.go @@ -33,32 +33,42 @@ func New() *MemoryLocker { // UseIn adds this locker to the passed composer. func (locker *MemoryLocker) UseIn(composer *handler.StoreComposer) { - composer.UseLocker(locker) + // TOOD: Add back + //composer.UseLocker(locker) +} + +func (locker *MemoryLocker) NewLock(id string) (handler.Lock, error) { + return memoryLock{locker, id}, nil +} + +type memoryLock struct { + locker *MemoryLocker + id string } // LockUpload tries to obtain the exclusive lock. -func (locker *MemoryLocker) LockUpload(id string) error { - locker.mutex.Lock() - defer locker.mutex.Unlock() +func (lock memoryLock) Lock() error { + lock.locker.mutex.Lock() + defer lock.locker.mutex.Unlock() // Ensure file is not locked - if _, ok := locker.locks[id]; ok { + if _, ok := lock.locker.locks[lock.id]; ok { return handler.ErrFileLocked } - locker.locks[id] = struct{}{} + lock.locker.locks[lock.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() +func (lock memoryLock) Unlock() error { + lock.locker.mutex.Lock() // 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) + delete(lock.locker.locks, lock.id) - locker.mutex.Unlock() + lock.locker.mutex.Unlock() return nil } diff --git a/pkg/memorylocker/memorylocker_test.go b/pkg/memorylocker/memorylocker_test.go index ddf2625..9fe9224 100644 --- a/pkg/memorylocker/memorylocker_test.go +++ b/pkg/memorylocker/memorylocker_test.go @@ -8,14 +8,23 @@ import ( "github.com/tus/tusd/pkg/handler" ) +var _ handler.Locker = &MemoryLocker{} + func TestMemoryLocker(t *testing.T) { a := assert.New(t) - var locker handler.LockerDataStore - locker = New() + locker := New() - a.NoError(locker.LockUpload("one")) - a.Equal(handler.ErrFileLocked, locker.LockUpload("one")) - a.NoError(locker.UnlockUpload("one")) - a.NoError(locker.UnlockUpload("one")) + lock1, err := locker.NewLock("one") + a.NoError(err) + + a.NoError(lock1.Lock()) + a.Equal(handler.ErrFileLocked, lock1.Lock()) + + lock2, err := locker.NewLock("one") + a.NoError(err) + a.Equal(handler.ErrFileLocked, lock2.Lock()) + + a.NoError(lock1.Unlock()) + a.NoError(lock1.Unlock()) }