Skip to content

Commit 0c27fd1

Browse files
resolved comments
Signed-off-by: Xiaoxuan Wang <[email protected]>
1 parent bed7a6c commit 0c27fd1

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

content/oci/oci.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ type Store struct {
5353
root string
5454
indexPath string
5555
index *ocispec.Index
56-
indexLock sync.Mutex
57-
sync sync.RWMutex
58-
59-
storage *Storage
60-
tagResolver *resolver.Memory
61-
graph *graph.Memory
56+
storage *Storage
57+
tagResolver *resolver.Memory
58+
graph *graph.Memory
59+
60+
// sync ensures that most operations can be done concurrently, while Delete
61+
// has the exclusive access to Store if a delete operation is underway. Operations
62+
// such as Fetch, Push use sync.RLock(), while Delete uses sync.Lock().
63+
sync sync.RWMutex
64+
// indexLock ensures that only one process is writing to the index.
65+
indexLock sync.Mutex
6266
}
6367

6468
// New creates a new OCI store with context.Background().
@@ -100,8 +104,8 @@ func NewWithContext(ctx context.Context, root string) (*Store, error) {
100104
}
101105

102106
// Fetch fetches the content identified by the descriptor. It returns an io.ReadCloser.
103-
// It's recommended to close the io.ReadCloser before a DeletableStore.Delete
104-
// operation, otherwise Delete may fail (for example on NTFS file systems).
107+
// It's recommended to close the io.ReadCloser before a Delete operation, otherwise
108+
// Delete may fail (for example on NTFS file systems).
105109
func (s *Store) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) {
106110
s.sync.RLock()
107111
defer s.sync.RUnlock()
@@ -154,7 +158,7 @@ func (s *Store) Delete(ctx context.Context, target ocispec.Descriptor) error {
154158
return err
155159
}
156160
if untagged && s.AutoSaveIndex {
157-
err := s.SaveIndex()
161+
err := s.saveIndex()
158162
if err != nil {
159163
return err
160164
}
@@ -197,7 +201,7 @@ func (s *Store) tag(ctx context.Context, desc ocispec.Descriptor, reference stri
197201
return err
198202
}
199203
if s.AutoSaveIndex {
200-
return s.SaveIndex()
204+
return s.saveIndex()
201205
}
202206
return nil
203207
}
@@ -318,6 +322,13 @@ func (s *Store) loadIndexFile(ctx context.Context) error {
318322
// - If AutoSaveIndex is set to false, it's the caller's responsibility
319323
// to manually call this method when needed.
320324
func (s *Store) SaveIndex() error {
325+
s.sync.RLock()
326+
defer s.sync.RUnlock()
327+
328+
return s.saveIndex()
329+
}
330+
331+
func (s *Store) saveIndex() error {
321332
s.indexLock.Lock()
322333
defer s.indexLock.Unlock()
323334

content/oci/oci_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ func TestStore_DisableAutoSaveIndex(t *testing.T) {
644644
if got, want := len(s.index.Manifests), 0; got != want {
645645
t.Errorf("len(index.Manifests) = %v, want %v", got, want)
646646
}
647-
if err := s.SaveIndex(); err != nil {
647+
if err := s.saveIndex(); err != nil {
648648
t.Fatal("Store.SaveIndex() error =", err)
649649
}
650650
// test index file again

0 commit comments

Comments
 (0)