forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamconnection.go
executable file
·106 lines (84 loc) · 2.63 KB
/
streamconnection.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"sync"
"github.com/pkg/errors"
"github.com/akorosmezey/fabric-sdk-go/pkg/client/common/verifier"
"github.com/akorosmezey/fabric-sdk-go/pkg/common/options"
fabcontext "github.com/akorosmezey/fabric-sdk-go/pkg/common/providers/context"
"github.com/akorosmezey/fabric-sdk-go/pkg/common/providers/fab"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
)
// StreamProvider creates a GRPC stream
type StreamProvider func(conn *grpc.ClientConn) (grpc.ClientStream, func(), error)
// StreamConnection manages the GRPC connection and client stream
type StreamConnection struct {
*GRPCConnection
chConfig fab.ChannelCfg
stream grpc.ClientStream
cancel func()
lock sync.Mutex
}
// NewStreamConnection creates a new connection with stream
func NewStreamConnection(ctx fabcontext.Client, chConfig fab.ChannelCfg, streamProvider StreamProvider, url string, opts ...options.Opt) (*StreamConnection, error) {
conn, err := NewConnection(ctx, url, opts...)
if err != nil {
return nil, err
}
stream, cancel, err := streamProvider(conn.conn)
if err != nil {
conn.commManager.ReleaseConn(conn.conn)
return nil, errors.Wrapf(err, "could not create stream to %s", url)
}
if stream == nil {
return nil, errors.New("unexpected nil stream received from provider")
}
peer, ok := peer.FromContext(stream.Context())
if !ok || peer == nil {
//return error - certificate is not available
return nil, errors.Wrap(err, "No peer cert in GRPC stream")
}
if peer.AuthInfo != nil {
tlsInfo := peer.AuthInfo.(credentials.TLSInfo)
for _, peercert := range tlsInfo.State.PeerCertificates {
err := verifier.ValidateCertificateDates(peercert)
if err != nil {
logger.Error(err)
return nil, errors.Wrapf(err, "error validating certificate dates for [%v]", peercert.Subject)
}
}
}
return &StreamConnection{
GRPCConnection: conn,
chConfig: chConfig,
stream: stream,
cancel: cancel,
}, nil
}
// ChannelConfig returns the channel configuration
func (c *StreamConnection) ChannelConfig() fab.ChannelCfg {
return c.chConfig
}
// Close closes the connection
func (c *StreamConnection) Close() {
c.lock.Lock()
defer c.lock.Unlock()
if c.Closed() {
return
}
logger.Debug("Closing stream....")
c.cancel()
if err := c.stream.CloseSend(); err != nil {
logger.Warnf("error closing GRPC stream: %s", err)
}
c.GRPCConnection.Close()
}
// Stream returns the GRPC stream
func (c *StreamConnection) Stream() grpc.ClientStream {
return c.stream
}