Skip to content

Commit

Permalink
added lock to memstore make threadsafe (#175)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Rammer <[email protected]>
  • Loading branch information
hamersaw authored Apr 8, 2024
1 parent 6c807ff commit 80edae2
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions flytestdlib/storage/mem_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"io"
"io/ioutil"
"os"
"sync"
)

type rawFile = []byte

type InMemoryStore struct {
copyImpl
cache map[DataReference]rawFile
rwMutex sync.RWMutex
}

type MemoryMetadata struct {
Expand All @@ -37,6 +39,9 @@ func (m MemoryMetadata) Etag() string {
}

func (s *InMemoryStore) Head(ctx context.Context, reference DataReference) (Metadata, error) {
s.rwMutex.RLock()
defer s.rwMutex.RUnlock()

data, found := s.cache[reference]
var hash [md5.Size]byte
if found {
Expand All @@ -50,6 +55,9 @@ func (s *InMemoryStore) Head(ctx context.Context, reference DataReference) (Meta
}

func (s *InMemoryStore) ReadRaw(ctx context.Context, reference DataReference) (io.ReadCloser, error) {
s.rwMutex.RLock()
defer s.rwMutex.RUnlock()

if raw, found := s.cache[reference]; found {
return ioutil.NopCloser(bytes.NewReader(raw)), nil
}
Expand All @@ -59,6 +67,9 @@ func (s *InMemoryStore) ReadRaw(ctx context.Context, reference DataReference) (i

// Delete removes the referenced data from the cache map.
func (s *InMemoryStore) Delete(ctx context.Context, reference DataReference) error {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()

if _, found := s.cache[reference]; !found {
return os.ErrNotExist
}
Expand All @@ -70,6 +81,8 @@ func (s *InMemoryStore) Delete(ctx context.Context, reference DataReference) err

func (s *InMemoryStore) WriteRaw(ctx context.Context, reference DataReference, size int64, opts Options, raw io.Reader) (
err error) {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()

rawBytes, err := ioutil.ReadAll(raw)
if err != nil {
Expand All @@ -81,6 +94,9 @@ func (s *InMemoryStore) WriteRaw(ctx context.Context, reference DataReference, s
}

func (s *InMemoryStore) Clear(ctx context.Context) error {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()

s.cache = map[DataReference]rawFile{}
return nil
}
Expand Down

0 comments on commit 80edae2

Please sign in to comment.