2023-04-07 21:02:35 +00:00
|
|
|
package sema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2023-06-17 17:36:56 +00:00
|
|
|
"sync"
|
2023-04-07 21:02:35 +00:00
|
|
|
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2023-04-07 21:02:35 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
)
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
// make sure that connectionLimitedBackend implements backend.Backend
|
|
|
|
var _ backend.Backend = &connectionLimitedBackend{}
|
2023-04-07 21:02:35 +00:00
|
|
|
|
2023-04-21 20:44:45 +00:00
|
|
|
// connectionLimitedBackend limits the number of concurrent operations.
|
|
|
|
type connectionLimitedBackend struct {
|
2023-10-01 09:40:12 +00:00
|
|
|
backend.Backend
|
2023-06-17 17:36:56 +00:00
|
|
|
sem semaphore
|
|
|
|
freezeLock sync.Mutex
|
2023-04-07 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-22 10:32:57 +00:00
|
|
|
// NewBackend creates a backend that limits the concurrent operations on the underlying backend
|
2023-10-01 09:40:12 +00:00
|
|
|
func NewBackend(be backend.Backend) backend.Backend {
|
2023-04-07 21:02:35 +00:00
|
|
|
sem, err := newSemaphore(be.Connections())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-04-21 20:44:45 +00:00
|
|
|
return &connectionLimitedBackend{
|
2023-04-07 21:02:35 +00:00
|
|
|
Backend: be,
|
|
|
|
sem: sem,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:16:54 +00:00
|
|
|
// typeDependentLimit acquire a token unless the FileType is a lock file. The returned function
|
|
|
|
// must be called to release the token.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (be *connectionLimitedBackend) typeDependentLimit(t backend.FileType) func() {
|
2023-04-23 10:16:54 +00:00
|
|
|
// allow concurrent lock file operations to ensure that the lock refresh is always possible
|
2023-10-01 09:40:12 +00:00
|
|
|
if t == backend.LockFile {
|
2023-04-23 10:16:54 +00:00
|
|
|
return func() {}
|
|
|
|
}
|
|
|
|
be.sem.GetToken()
|
2023-06-17 17:36:56 +00:00
|
|
|
// prevent token usage while the backend is frozen
|
|
|
|
be.freezeLock.Lock()
|
|
|
|
defer be.freezeLock.Unlock()
|
|
|
|
|
2023-04-23 10:16:54 +00:00
|
|
|
return be.sem.ReleaseToken
|
|
|
|
}
|
|
|
|
|
2023-06-17 17:36:56 +00:00
|
|
|
// Freeze blocks all backend operations except those on lock files
|
|
|
|
func (be *connectionLimitedBackend) Freeze() {
|
|
|
|
be.freezeLock.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfreeze allows all backend operations to continue
|
|
|
|
func (be *connectionLimitedBackend) Unfreeze() {
|
|
|
|
be.freezeLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:02:35 +00:00
|
|
|
// Save adds new Data to the backend.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (be *connectionLimitedBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
|
2023-04-07 21:02:35 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return backoff.Permanent(err)
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:16:54 +00:00
|
|
|
defer be.typeDependentLimit(h.Type)()
|
2023-04-07 21:02:35 +00:00
|
|
|
|
2023-06-17 17:36:56 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:02:35 +00:00
|
|
|
return be.Backend.Save(ctx, h, rd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load runs fn with a reader that yields the contents of the file at h at the
|
|
|
|
// given offset.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (be *connectionLimitedBackend) Load(ctx context.Context, h backend.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
2023-04-07 21:02:35 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return backoff.Permanent(err)
|
|
|
|
}
|
|
|
|
if offset < 0 {
|
|
|
|
return backoff.Permanent(errors.New("offset is negative"))
|
|
|
|
}
|
|
|
|
if length < 0 {
|
|
|
|
return backoff.Permanent(errors.Errorf("invalid length %d", length))
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:16:54 +00:00
|
|
|
defer be.typeDependentLimit(h.Type)()
|
2023-04-07 21:02:35 +00:00
|
|
|
|
2023-06-17 17:36:56 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:02:35 +00:00
|
|
|
return be.Backend.Load(ctx, h, length, offset, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns information about a file in the backend.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (be *connectionLimitedBackend) Stat(ctx context.Context, h backend.Handle) (backend.FileInfo, error) {
|
2023-04-07 21:02:35 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2023-10-01 09:40:12 +00:00
|
|
|
return backend.FileInfo{}, backoff.Permanent(err)
|
2023-04-07 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 10:16:54 +00:00
|
|
|
defer be.typeDependentLimit(h.Type)()
|
2023-04-07 21:02:35 +00:00
|
|
|
|
2023-06-17 17:36:56 +00:00
|
|
|
if ctx.Err() != nil {
|
2023-10-01 09:40:12 +00:00
|
|
|
return backend.FileInfo{}, ctx.Err()
|
2023-06-17 17:36:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 21:02:35 +00:00
|
|
|
return be.Backend.Stat(ctx, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes a file from the backend.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (be *connectionLimitedBackend) Remove(ctx context.Context, h backend.Handle) error {
|
2023-04-07 21:02:35 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return backoff.Permanent(err)
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:16:54 +00:00
|
|
|
defer be.typeDependentLimit(h.Type)()
|
2023-04-07 21:02:35 +00:00
|
|
|
|
2023-06-17 17:36:56 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:02:35 +00:00
|
|
|
return be.Backend.Remove(ctx, h)
|
|
|
|
}
|
2023-04-08 10:53:43 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (be *connectionLimitedBackend) Unwrap() backend.Backend {
|
2023-04-08 10:53:43 +00:00
|
|
|
return be.Backend
|
|
|
|
}
|