backend/mem: Actually enforce connection limit

This will allow tests to detect deadlocks related to the connections
limit.
This commit is contained in:
Michael Eischer 2022-04-23 11:22:00 +02:00
parent 4f97492d28
commit ee627cd832

View file

@ -25,17 +25,26 @@ var _ restic.Backend = &MemoryBackend{}
var errNotFound = errors.New("not found") var errNotFound = errors.New("not found")
const connectionCount = 2
// MemoryBackend is a mock backend that uses a map for storing all data in // MemoryBackend is a mock backend that uses a map for storing all data in
// memory. This should only be used for tests. // memory. This should only be used for tests.
type MemoryBackend struct { type MemoryBackend struct {
data memMap data memMap
m sync.Mutex m sync.Mutex
sem *backend.Semaphore
} }
// New returns a new backend that saves all data in a map in memory. // New returns a new backend that saves all data in a map in memory.
func New() *MemoryBackend { func New() *MemoryBackend {
sem, err := backend.NewSemaphore(connectionCount)
if err != nil {
panic(err)
}
be := &MemoryBackend{ be := &MemoryBackend{
data: make(memMap), data: make(memMap),
sem: sem,
} }
debug.Log("created new memory backend") debug.Log("created new memory backend")
@ -45,6 +54,9 @@ func New() *MemoryBackend {
// Test returns whether a file exists. // Test returns whether a file exists.
func (be *MemoryBackend) Test(ctx context.Context, h restic.Handle) (bool, error) { func (be *MemoryBackend) Test(ctx context.Context, h restic.Handle) (bool, error) {
be.sem.GetToken()
defer be.sem.ReleaseToken()
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()
@ -68,6 +80,9 @@ func (be *MemoryBackend) Save(ctx context.Context, h restic.Handle, rd restic.Re
return backoff.Permanent(err) return backoff.Permanent(err)
} }
be.sem.GetToken()
defer be.sem.ReleaseToken()
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()
@ -120,6 +135,7 @@ func (be *MemoryBackend) openReader(ctx context.Context, h restic.Handle, length
return nil, backoff.Permanent(err) return nil, backoff.Permanent(err)
} }
be.sem.GetToken()
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()
@ -131,15 +147,18 @@ func (be *MemoryBackend) openReader(ctx context.Context, h restic.Handle, length
debug.Log("Load %v offset %v len %v", h, offset, length) debug.Log("Load %v offset %v len %v", h, offset, length)
if offset < 0 { if offset < 0 {
be.sem.ReleaseToken()
return nil, errors.New("offset is negative") return nil, errors.New("offset is negative")
} }
if _, ok := be.data[h]; !ok { if _, ok := be.data[h]; !ok {
be.sem.ReleaseToken()
return nil, errNotFound return nil, errNotFound
} }
buf := be.data[h] buf := be.data[h]
if offset > int64(len(buf)) { if offset > int64(len(buf)) {
be.sem.ReleaseToken()
return nil, errors.New("offset beyond end of file") return nil, errors.New("offset beyond end of file")
} }
@ -148,18 +167,21 @@ func (be *MemoryBackend) openReader(ctx context.Context, h restic.Handle, length
buf = buf[:length] buf = buf[:length]
} }
return ioutil.NopCloser(bytes.NewReader(buf)), ctx.Err() return be.sem.ReleaseTokenOnClose(ioutil.NopCloser(bytes.NewReader(buf)), nil), ctx.Err()
} }
// Stat returns information about a file in the backend. // Stat returns information about a file in the backend.
func (be *MemoryBackend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) { func (be *MemoryBackend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
be.m.Lock()
defer be.m.Unlock()
if err := h.Valid(); err != nil { if err := h.Valid(); err != nil {
return restic.FileInfo{}, backoff.Permanent(err) return restic.FileInfo{}, backoff.Permanent(err)
} }
be.sem.GetToken()
defer be.sem.ReleaseToken()
be.m.Lock()
defer be.m.Unlock()
h.ContainedBlobType = restic.InvalidBlob h.ContainedBlobType = restic.InvalidBlob
if h.Type == restic.ConfigFile { if h.Type == restic.ConfigFile {
h.Name = "" h.Name = ""
@ -177,6 +199,9 @@ func (be *MemoryBackend) Stat(ctx context.Context, h restic.Handle) (restic.File
// Remove deletes a file from the backend. // Remove deletes a file from the backend.
func (be *MemoryBackend) Remove(ctx context.Context, h restic.Handle) error { func (be *MemoryBackend) Remove(ctx context.Context, h restic.Handle) error {
be.sem.GetToken()
defer be.sem.ReleaseToken()
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()
@ -230,7 +255,7 @@ func (be *MemoryBackend) List(ctx context.Context, t restic.FileType, fn func(re
} }
func (be *MemoryBackend) Connections() uint { func (be *MemoryBackend) Connections() uint {
return 2 return connectionCount
} }
// Location returns the location of the backend (RAM). // Location returns the location of the backend (RAM).