2015-12-18 22:13:00 +00:00
|
|
|
// Package lockingstore manages concurrent access to a single upload.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// This package wrappes an existing data storage and only allows a single access
|
|
|
|
// at a time by using an exclusive locking mechanism.
|
2015-12-09 19:25:08 +00:00
|
|
|
package lockingstore
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/tus/tusd"
|
|
|
|
)
|
|
|
|
|
2015-12-18 22:13:00 +00:00
|
|
|
// Locker is the interface required for custom lock persisting mechanisms.
|
|
|
|
// Common ways to store this information is in memory, on disk or using an
|
|
|
|
// external service, such as ZooKeeper.
|
2015-12-09 19:25:08 +00:00
|
|
|
type Locker interface {
|
2015-12-18 22:13:00 +00:00
|
|
|
// LockUpload attempts to obtain an exclusive lock for the upload specified
|
|
|
|
// by its id.
|
|
|
|
// If this operation fails because the resource is already locked, the
|
|
|
|
// tusd.ErrFileLocked must be returned. If no error is returned, the attempt
|
|
|
|
// is consider to be successful and the upload to be locked until UnlockUpload
|
|
|
|
// is invoked for the same upload.
|
2015-12-09 19:25:08 +00:00
|
|
|
LockUpload(id string) error
|
2015-12-18 22:13:00 +00:00
|
|
|
// UnlockUpload releases an existing lock for the given upload.
|
2015-12-09 19:25:08 +00:00
|
|
|
UnlockUpload(id string) error
|
|
|
|
}
|
|
|
|
|
2015-12-18 22:13:00 +00:00
|
|
|
// LockingStore wraps an existing data storage and catches all operation.
|
|
|
|
// Before passing the method calls to the underlying backend, locks are required
|
|
|
|
// to be obtained.
|
2015-12-09 19:25:08 +00:00
|
|
|
type LockingStore struct {
|
2015-12-18 22:13:00 +00:00
|
|
|
// The underlying data storage to which the operation will be passed if an
|
|
|
|
// upload is not locked.
|
2015-12-09 19:25:08 +00:00
|
|
|
tusd.DataStore
|
2015-12-18 22:13:00 +00:00
|
|
|
// The custom locking persisting mechanism used for obtaining and releasing
|
|
|
|
// locks.
|
2015-12-09 19:48:41 +00:00
|
|
|
Locker Locker
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
func (store LockingStore) WriteChunk(id string, offset int64, src io.Reader) (n int64, err error) {
|
|
|
|
if err := store.Locker.LockUpload(id); err != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-12-09 19:48:41 +00:00
|
|
|
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
err = unlockErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return store.DataStore.WriteChunk(id, offset, src)
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
func (store LockingStore) GetInfo(id string) (info tusd.FileInfo, err error) {
|
|
|
|
if err := store.Locker.LockUpload(id); err != nil {
|
|
|
|
return info, err
|
2015-12-09 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-12-09 19:48:41 +00:00
|
|
|
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
err = unlockErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return store.DataStore.GetInfo(id)
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
func (store LockingStore) GetReader(id string) (src io.Reader, err error) {
|
|
|
|
if err := store.Locker.LockUpload(id); err != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-12-09 19:48:41 +00:00
|
|
|
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
err = unlockErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return store.DataStore.GetReader(id)
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:48:41 +00:00
|
|
|
func (store LockingStore) Terminate(id string) (err error) {
|
|
|
|
if err := store.Locker.LockUpload(id); err != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-12-09 19:48:41 +00:00
|
|
|
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
|
2015-12-09 19:25:08 +00:00
|
|
|
err = unlockErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return store.DataStore.Terminate(id)
|
|
|
|
}
|