diff options
| author | Pablo M. Bermudo Garay <pablombg@gmail.com> | 2022-02-08 19:27:32 +0100 |
|---|---|---|
| committer | Pablo M. Bermudo Garay <pablombg@gmail.com> | 2022-02-10 13:14:19 +0100 |
| commit | 6601e2eccbe2032e10a20027393db8e84da4b12f (patch) | |
| tree | ad05baf4b8788093b1bf93b30d42d23566d7bea0 | |
| parent | 81763bb1b4bcd1cb8094696e31e0711d965e173c (diff) | |
Add mutex to the memory backend
Avoid potential problems due to concurrent access to the backend.
| -rw-r--r-- | objectstore/memory_backend.go | 15 |
1 files changed, 14 insertions, 1 deletions
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") |
