oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/internal/status/tracker.go (about) 1 /* 2 Copyright The ORAS Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package status 17 18 import ( 19 "sync" 20 21 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 22 "oras.land/oras-go/v2/internal/descriptor" 23 ) 24 25 // Tracker tracks content status described by a descriptor. 26 type Tracker struct { 27 status sync.Map // map[descriptor.Descriptor]chan struct{} 28 } 29 30 // NewTracker creates a new content status tracker. 31 func NewTracker() *Tracker { 32 return &Tracker{} 33 } 34 35 // TryCommit tries to commit the work for the target descriptor. 36 // Returns true if committed. A channel is also returned for sending 37 // notifications. Once the work is done, the channel should be closed. 38 // Returns false if the work is done or still in progress. 39 func (t *Tracker) TryCommit(target ocispec.Descriptor) (chan struct{}, bool) { 40 key := descriptor.FromOCI(target) 41 status, exists := t.status.LoadOrStore(key, make(chan struct{})) 42 return status.(chan struct{}), !exists 43 }