2024-01-19 20:50:09 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
|
|
|
tusd "github.com/tus/tusd/v2/pkg/handler"
|
|
|
|
"go.uber.org/zap"
|
2024-01-28 07:20:59 +00:00
|
|
|
"gorm.io/gorm"
|
2024-01-19 20:50:09 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ tusd.Locker = (*MySQLLocker)(nil)
|
|
|
|
_ tusd.Lock = (*Lock)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type MySQLLocker struct {
|
2024-01-28 07:20:59 +00:00
|
|
|
storage *StorageServiceImpl
|
2024-01-19 20:50:09 +00:00
|
|
|
AcquirerPollInterval time.Duration
|
|
|
|
HolderPollInterval time.Duration
|
2024-01-28 07:20:59 +00:00
|
|
|
db *gorm.DB
|
|
|
|
logger *zap.Logger
|
2024-01-19 20:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Lock struct {
|
|
|
|
locker *MySQLLocker
|
|
|
|
id string
|
|
|
|
holderPollInterval time.Duration
|
|
|
|
acquirerPollInterval time.Duration
|
|
|
|
stopHolderPoll chan struct{}
|
|
|
|
lockRecord models.TusLock
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func NewMySQLLocker(db *gorm.DB, logger *zap.Logger) *MySQLLocker {
|
|
|
|
return &MySQLLocker{HolderPollInterval: 5 * time.Second, AcquirerPollInterval: 2 * time.Second, db: db, logger: logger}
|
2024-01-19 20:50:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 16:04:43 +00:00
|
|
|
func (l *Lock) released() error {
|
2024-01-28 07:20:59 +00:00
|
|
|
err := l.lockRecord.Released(l.locker.db)
|
2024-01-20 16:04:43 +00:00
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
l.locker.logger.Error("Failed to release lock", zap.Error(err))
|
2024-01-20 16:04:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-19 20:50:09 +00:00
|
|
|
func (l *Lock) Lock(ctx context.Context, requestUnlock func()) error {
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
db := l.locker.db
|
2024-01-19 20:50:09 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
err := l.lockRecord.TryLock(db, ctx)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != models.ErrTusLockBusy {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-20 15:48:42 +00:00
|
|
|
err = l.lockRecord.RequestRelease(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-19 20:50:09 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-01-20 16:06:50 +00:00
|
|
|
err := l.released()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-19 20:50:09 +00:00
|
|
|
// Context expired, so we return a timeout
|
|
|
|
return tusd.ErrLockTimeout
|
|
|
|
case <-time.After(l.acquirerPollInterval):
|
|
|
|
// Continue with the next attempt after a short delay
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2024-01-20 16:16:54 +00:00
|
|
|
ticker := time.NewTicker(l.holderPollInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2024-01-19 20:50:09 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-l.stopHolderPoll:
|
|
|
|
return
|
2024-01-20 16:16:54 +00:00
|
|
|
case <-ticker.C:
|
2024-01-19 20:50:09 +00:00
|
|
|
requested, err := l.lockRecord.IsReleaseRequested(db)
|
2024-01-20 16:16:54 +00:00
|
|
|
if err != nil {
|
|
|
|
// Handle error
|
2024-01-19 20:50:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if requested {
|
|
|
|
requestUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Lock) Unlock() error {
|
|
|
|
l.once.Do(func() {
|
|
|
|
close(l.stopHolderPoll)
|
|
|
|
})
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
return l.lockRecord.Delete(l.locker.db)
|
2024-01-19 20:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MySQLLocker) NewLock(id string) (tusd.Lock, error) {
|
|
|
|
return &Lock{
|
|
|
|
locker: m,
|
|
|
|
id: id,
|
|
|
|
holderPollInterval: m.HolderPollInterval,
|
|
|
|
acquirerPollInterval: m.AcquirerPollInterval,
|
|
|
|
stopHolderPoll: make(chan struct{}),
|
|
|
|
lockRecord: models.TusLock{
|
|
|
|
LockId: id,
|
|
|
|
HolderPID: os.Getpid(),
|
|
|
|
AcquiredAt: time.Now(),
|
|
|
|
ExpiresAt: time.Now().Add(30 * time.Minute),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|