Skip to content

Commit

Permalink
fix race condition (#17)
Browse files Browse the repository at this point in the history
make MemoryStore thread-safe
  • Loading branch information
shizhMSFT committed Dec 28, 2018
1 parent 5f50a43 commit cf59c2b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pkg/oras/store.go
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"io"
"io/ioutil"
"sync"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
Expand All @@ -21,13 +22,13 @@ var (

// MemoryStore stores contents in the memory
type MemoryStore struct {
content map[string][]byte
content *sync.Map
}

// NewMemoryStore creates a new memory store
func NewMemoryStore() *MemoryStore {
return &MemoryStore{
content: make(map[string][]byte),
content: &sync.Map{},
}
}

Expand Down Expand Up @@ -60,18 +61,22 @@ func (s *MemoryStore) FetchHandler(fetcher remotes.Fetcher) images.HandlerFunc {

// Set adds the content to the store
func (s *MemoryStore) Set(desc ocispec.Descriptor, content []byte) {
s.content[desc.Digest.String()] = content
s.content.Store(desc.Digest, content)
}

// Get finds the content from the store
func (s *MemoryStore) Get(desc ocispec.Descriptor) ([]byte, bool) {
content, ok := s.content[desc.Digest.String()]
value, ok := s.content.Load(desc.Digest)
if !ok {
return nil, false
}
content, ok := value.([]byte)
return content, ok
}

// ReaderAt provides contents
func (s *MemoryStore) ReaderAt(ctx context.Context, desc ocispec.Descriptor) (content.ReaderAt, error) {
if content, ok := s.content[desc.Digest.String()]; ok {
if content, ok := s.Get(desc); ok {
return newReaderAt(content), nil

}
Expand Down

0 comments on commit cf59c2b

Please sign in to comment.