Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ebsfile): add mutex for read block #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ebsfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -85,6 +86,7 @@ type File struct {
blockTable map[int32]string

ebsclient EBSAPI
syncMap sync.Map
cache Cache[string, []byte]
ctx context.Context
}
Expand Down Expand Up @@ -160,6 +162,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)
Expand Down Expand Up @@ -207,4 +215,4 @@ func (f *File) read(index int32, token string) ([]byte, error) {
defer o.BlockData.Close()

return buf, nil
}
}