github.com/kaituanwang/hyperledger@v2.0.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  type BuildStatus struct {
    39  	mutex sync.Mutex
    40  	doneC chan struct{}
    41  	err   error
    42  }
    43  
    44  func NewBuildStatus() *BuildStatus {
    45  	return &BuildStatus{
    46  		doneC: make(chan struct{}),
    47  	}
    48  }
    49  
    50  func (bs *BuildStatus) Err() error {
    51  	bs.mutex.Lock()
    52  	defer bs.mutex.Unlock()
    53  	return bs.err
    54  }
    55  
    56  func (bs *BuildStatus) Notify(err error) {
    57  	bs.mutex.Lock()
    58  	defer bs.mutex.Unlock()
    59  	bs.err = err
    60  	close(bs.doneC)
    61  }
    62  
    63  func (bs *BuildStatus) Done() <-chan struct{} {
    64  	return bs.doneC
    65  }