From e8a1da7d7165ca82d73382a49445adc13b426225 Mon Sep 17 00:00:00 2001 From: masahiro331 Date: Sun, 26 Feb 2023 16:04:22 +0900 Subject: [PATCH 1/2] feat(ebsfile): add mutex for read block --- ebsfile.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ebsfile.go b/ebsfile.go index f6d9f0d..4ac75e3 100644 --- a/ebsfile.go +++ b/ebsfile.go @@ -4,13 +4,13 @@ import ( "bytes" "context" "fmt" - "io" - "os" - "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/ebs" "github.com/aws/aws-sdk-go-v2/service/ebs/types" + "io" + "os" + "sync" ) type MockEBS struct { @@ -85,6 +85,7 @@ type File struct { blockTable map[int32]string ebsclient EBSAPI + syncMap sync.Map cache Cache[string, []byte] ctx context.Context } @@ -160,6 +161,12 @@ func (f *File) ReadAt(p []byte, off int64) (n int, err error) { } key := cacheKey(int64(index)) + mu := &sync.Mutex{} + v, ok := f.syncMap.LoadOrStore(key, mu) + mu = v.(*sync.Mutex) + mu.Lock() + defer mu.Unlock() + buf, ok := f.cache.Get(key) if !ok { buf, err = f.read(index, token) From 3d494ff42130669dd31f2ed68b52ef7e131a72e3 Mon Sep 17 00:00:00 2001 From: masahiro331 Date: Sun, 26 Feb 2023 16:05:40 +0900 Subject: [PATCH 2/2] refactor(ebsfile): sort imports --- ebsfile.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ebsfile.go b/ebsfile.go index 4ac75e3..20436c7 100644 --- a/ebsfile.go +++ b/ebsfile.go @@ -4,13 +4,14 @@ import ( "bytes" "context" "fmt" + "io" + "os" + "sync" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/ebs" "github.com/aws/aws-sdk-go-v2/service/ebs/types" - "io" - "os" - "sync" ) type MockEBS struct { @@ -214,4 +215,4 @@ func (f *File) read(index int32, token string) ([]byte, error) { defer o.BlockData.Close() return buf, nil -} +} \ No newline at end of file