forked from tw-bc-group/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inprocstream.go
59 lines (48 loc) · 1.25 KB
/
inprocstream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package scc
import (
"errors"
"fmt"
"sync"
pb "github.com/hyperledger/fabric-protos-go/peer"
)
//SendPanicFailure
type SendPanicFailure string
func (e SendPanicFailure) Error() string {
return fmt.Sprintf("send failure %s", string(e))
}
// PeerChaincodeStream interface for stream between Peer and chaincode instance.
type inProcStream struct {
recv <-chan *pb.ChaincodeMessage
send chan<- *pb.ChaincodeMessage
closeOnce sync.Once
}
func newInProcStream(recv <-chan *pb.ChaincodeMessage, send chan<- *pb.ChaincodeMessage) *inProcStream {
return &inProcStream{recv: recv, send: send}
}
func (s *inProcStream) Send(msg *pb.ChaincodeMessage) (err error) {
//send may happen on a closed channel when the system is
//shutting down. Just catch the exception and return error
defer func() {
if r := recover(); r != nil {
err = SendPanicFailure(fmt.Sprintf("%s", r))
return
}
}()
s.send <- msg
return
}
func (s *inProcStream) Recv() (*pb.ChaincodeMessage, error) {
msg, ok := <-s.recv
if !ok {
return nil, errors.New("channel is closed")
}
return msg, nil
}
func (s *inProcStream) CloseSend() error {
s.closeOnce.Do(func() { close(s.send) })
return nil
}