github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/core/scc/inprocstream.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  	"errors"
    11  	"fmt"
    12  
    13  	pb "github.com/hyperledger/fabric-protos-go/peer"
    14  )
    15  
    16  //SendPanicFailure
    17  type SendPanicFailure string
    18  
    19  func (e SendPanicFailure) Error() string {
    20  	return fmt.Sprintf("send failure %s", string(e))
    21  }
    22  
    23  // PeerChaincodeStream interface for stream between Peer and chaincode instance.
    24  type inProcStream struct {
    25  	recv <-chan *pb.ChaincodeMessage
    26  	send chan<- *pb.ChaincodeMessage
    27  }
    28  
    29  func newInProcStream(recv <-chan *pb.ChaincodeMessage, send chan<- *pb.ChaincodeMessage) *inProcStream {
    30  	return &inProcStream{recv, send}
    31  }
    32  
    33  func (s *inProcStream) Send(msg *pb.ChaincodeMessage) (err error) {
    34  	//send may happen on a closed channel when the system is
    35  	//shutting down. Just catch the exception and return error
    36  	defer func() {
    37  		if r := recover(); r != nil {
    38  			err = SendPanicFailure(fmt.Sprintf("%s", r))
    39  			return
    40  		}
    41  	}()
    42  	s.send <- msg
    43  	return
    44  }
    45  
    46  func (s *inProcStream) Recv() (*pb.ChaincodeMessage, error) {
    47  	msg, ok := <-s.recv
    48  	if !ok {
    49  		return nil, errors.New("channel is closed")
    50  	}
    51  	return msg, nil
    52  }
    53  
    54  func (s *inProcStream) CloseSend() error {
    55  	return nil
    56  }