github.com/ahlemtn/fabric@v2.1.1+incompatible/core/container/build_registry.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package container 8 9 import ( 10 "sync" 11 ) 12 13 type BuildRegistry struct { 14 mutex sync.Mutex 15 builds map[string]*BuildStatus 16 } 17 18 // BuildStatus returns a BuildStatus for the ccid, and whether the caller 19 // is waiting in line (true), or this build status is new and their responsibility. 20 // If the build status is new, then the caller must call Notify with the error 21 // (or nil) upon completion. 22 func (br *BuildRegistry) BuildStatus(ccid string) (*BuildStatus, bool) { 23 br.mutex.Lock() 24 defer br.mutex.Unlock() 25 if br.builds == nil { 26 br.builds = map[string]*BuildStatus{} 27 } 28 29 bs, ok := br.builds[ccid] 30 if !ok { 31 bs = NewBuildStatus() 32 br.builds[ccid] = bs 33 } 34 35 return bs, ok 36 } 37 38 // ResetBuildStatus returns a new BuildStatus for the ccid. This build status 39 // is new and the caller's responsibility. The caller must use external 40 // locking to ensure the build status is not reset by another install request 41 // and must call Notify with the error (or nil) upon completion. 42 func (br *BuildRegistry) ResetBuildStatus(ccid string) *BuildStatus { 43 br.mutex.Lock() 44 defer br.mutex.Unlock() 45 46 bs := NewBuildStatus() 47 br.builds[ccid] = bs 48 49 return bs 50 } 51 52 type BuildStatus struct { 53 mutex sync.Mutex 54 doneC chan struct{} 55 err error 56 } 57 58 func NewBuildStatus() *BuildStatus { 59 return &BuildStatus{ 60 doneC: make(chan struct{}), 61 } 62 } 63 64 func (bs *BuildStatus) Err() error { 65 bs.mutex.Lock() 66 defer bs.mutex.Unlock() 67 return bs.err 68 } 69 70 func (bs *BuildStatus) Notify(err error) { 71 bs.mutex.Lock() 72 defer bs.mutex.Unlock() 73 bs.err = err 74 close(bs.doneC) 75 } 76 77 func (bs *BuildStatus) Done() <-chan struct{} { 78 return bs.doneC 79 }