From 6601e2eccbe2032e10a20027393db8e84da4b12f Mon Sep 17 00:00:00 2001 From: "Pablo M. Bermudo Garay" Date: Tue, 8 Feb 2022 19:27:32 +0100 Subject: Add mutex to the memory backend Avoid potential problems due to concurrent access to the backend. --- objectstore/memory_backend.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'objectstore/memory_backend.go') diff --git a/objectstore/memory_backend.go b/objectstore/memory_backend.go index 735a649..09f7d8a 100644 --- a/objectstore/memory_backend.go +++ b/objectstore/memory_backend.go @@ -4,13 +4,17 @@ // https://creativecommons.org/publicdomain/zero/1.0/legalcode package objectstore -import "fmt" +import ( + "fmt" + "sync" +) type memBucket struct { objects map[string]string } type MemObjectStore struct { + sync.Mutex buckets map[string]memBucket } @@ -21,6 +25,9 @@ func NewMemBackend() *MemObjectStore { } func (os *MemObjectStore) CreateObject(bucketId string, objectId string, object string) { + os.Lock() + defer os.Unlock() + bucket, ok := os.buckets[bucketId] if !ok { bucket = memBucket{} @@ -32,6 +39,9 @@ func (os *MemObjectStore) CreateObject(bucketId string, objectId string, object } func (os *MemObjectStore) GetObject(bucketId string, objectId string) (string, error) { + os.Lock() + defer os.Unlock() + bucket, ok := os.buckets[bucketId] if !ok { return "", fmt.Errorf("Bucket not found") @@ -46,6 +56,9 @@ func (os *MemObjectStore) GetObject(bucketId string, objectId string) (string, e } func (os *MemObjectStore) DeleteObject(bucketId string, objectId string) error { + os.Lock() + defer os.Unlock() + bucket, ok := os.buckets[bucketId] if !ok { return fmt.Errorf("Bucket not found") -- cgit v1.2.3-70-g09d2