github.com/lzy4123/fabric@v2.1.1+incompatible/core/chaincode/active_transactions.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package chaincode
     8  
     9  import "sync"
    10  
    11  func NewTxKey(channelID, txID string) string { return channelID + txID }
    12  
    13  type ActiveTransactions struct {
    14  	mutex sync.Mutex
    15  	ids   map[string]struct{}
    16  }
    17  
    18  func NewActiveTransactions() *ActiveTransactions {
    19  	return &ActiveTransactions{
    20  		ids: map[string]struct{}{},
    21  	}
    22  }
    23  
    24  func (a *ActiveTransactions) Add(channelID, txID string) bool {
    25  	key := NewTxKey(channelID, txID)
    26  	a.mutex.Lock()
    27  	defer a.mutex.Unlock()
    28  	if _, ok := a.ids[key]; ok {
    29  		return false
    30  	}
    31  
    32  	a.ids[key] = struct{}{}
    33  	return true
    34  }
    35  
    36  func (a *ActiveTransactions) Remove(channelID, txID string) {
    37  	key := NewTxKey(channelID, txID)
    38  	a.mutex.Lock()
    39  	delete(a.ids, key)
    40  	a.mutex.Unlock()
    41  }