github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/core/scc/throttle.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package scc 8 9 import ( 10 "context" 11 12 "github.com/hyperledger/fabric-chaincode-go/shim" 13 pb "github.com/hyperledger/fabric-protos-go/peer" 14 "github.com/hyperledger/fabric/common/semaphore" 15 ) 16 17 func Throttle(limit int, systemCC SelfDescribingSysCC) *ThrottledSysCC { 18 return &ThrottledSysCC{ 19 SelfDescribingSysCC: systemCC, 20 semaphore: semaphore.New(limit), 21 } 22 } 23 24 type ThrottledSysCC struct { 25 SelfDescribingSysCC 26 semaphore semaphore.Semaphore 27 } 28 29 func (t *ThrottledSysCC) Chaincode() shim.Chaincode { 30 return &ThrottledChaincode{ 31 chaincode: t.SelfDescribingSysCC.Chaincode(), 32 semaphore: t.semaphore, 33 } 34 } 35 36 type ThrottledChaincode struct { 37 chaincode shim.Chaincode 38 semaphore semaphore.Semaphore 39 } 40 41 func (t *ThrottledChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { 42 return t.chaincode.Init(stub) 43 } 44 45 func (t *ThrottledChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 46 if err := t.semaphore.Acquire(context.Background()); err != nil { 47 return shim.Error(err.Error()) 48 } 49 defer t.semaphore.Release() 50 51 return t.chaincode.Invoke(stub) 52 }