-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.go
191 lines (162 loc) · 4.15 KB
/
stream.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package brpc
import (
"context"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
leagcyproto "github.com/golang/protobuf/proto"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
policypb "github.com/tomwei7/brpc-go/genproto/brpc/policy"
)
var ErrUnavailable = fmt.Errorf("stream unavailable")
var ErrReachMaxConcurrency = fmt.Errorf("stream reach max concurrency")
type resp struct {
err error
frame *frame
}
type eventHub struct {
mx sync.Mutex
chs map[int64]chan *resp
}
func (e *eventHub) register(correlationID int64) (<-chan *resp, context.CancelFunc) {
ch := make(chan *resp, 1)
e.mx.Lock()
defer e.mx.Unlock()
e.chs[correlationID] = ch
return ch, func() { e.unRegister(correlationID) }
}
func (e *eventHub) unRegister(correlationID int64) {
e.mx.Lock()
defer e.mx.Unlock()
delete(e.chs, correlationID)
}
func (e *eventHub) emitResp(correlationID int64, f *frame, err error) {
resp := &resp{err: err, frame: f}
e.mx.Lock()
defer e.mx.Unlock()
ch, ok := e.chs[correlationID]
if !ok {
return
}
select {
case ch <- resp:
default:
}
}
func (e *eventHub) emitError(err error) {
e.mx.Lock()
defer e.mx.Unlock()
for _, ch := range e.chs {
select {
case ch <- &resp{err: err}:
default:
}
}
}
type stream struct {
correlationId int64
unavailable bool
pipe *pipeline
reqCh chan struct{}
eventHub *eventHub
}
func newStream(conn net.Conn, maxConcurrency int) *stream {
pipe := newPipeline(conn)
s := &stream{
pipe: pipe,
reqCh: make(chan struct{}, maxConcurrency),
eventHub: &eventHub{chs: make(map[int64]chan *resp)},
}
go s.ioloop()
return s
}
func (s *stream) nextCorrelationId() int64 {
return atomic.AddInt64(&s.correlationId, 1)
}
func splitServiceNameandMethod(method string) (string, string, error) {
seqs := strings.SplitN(method, "/", 3)
if len(seqs) != 3 {
return "", "", fmt.Errorf("invalid method: %s", method)
}
return seqs[1], seqs[2], nil
}
func (s *stream) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error {
if s.unavailable {
return ErrUnavailable
}
correlationID := s.nextCorrelationId()
rpcOpts := newRPCOptions(opts...)
ch, cancal := s.eventHub.register(correlationID)
defer cancal()
if err := s.writeRequest(ctx, correlationID, method, args, rpcOpts); err != nil {
return err
}
select {
case rp := <-ch:
return s.processResp(rp, rpcOpts, reply)
case <-ctx.Done():
return ctx.Err()
}
}
func (s *stream) processResp(rp *resp, rpcOpts *rpcOptions, reply interface{}) error {
if rp.err != nil {
return rp.err
}
rpcResp := rp.frame.RPCMeta.Response
if rpcResp == nil {
return fmt.Errorf("miss RPCMeta Response")
}
if rpcResp.GetErrorCode() != 0 {
return &RPCError{Code: rpcResp.GetErrorCode(), Message: rpcResp.GetErrorText()}
}
attachmentSize := rp.frame.AttachmentSize()
offset := len(rp.frame.Body) - attachmentSize
if err := proto.Unmarshal(rp.frame.Body[:offset], leagcyproto.MessageV2(reply)); err != nil {
return nil
}
if rpcOpts.AttachmentWriter != nil {
rpcOpts.AttachmentWriter.Write(rp.frame.Body[offset:])
}
return nil
}
func (s *stream) writeRequest(ctx context.Context, correlationID int64, method string, args interface{}, rpcOpts *rpcOptions) error {
serviceName, methodName, err := splitServiceNameandMethod(method)
if err != nil {
return err
}
reqMeta := &policypb.RpcRequestMeta{
ServiceName: proto.String(serviceName),
MethodName: proto.String(methodName),
LogId: rpcOpts.LogID,
TraceId: rpcOpts.TraceID,
SpanId: rpcOpts.SpanID,
ParentSpanId: rpcOpts.ParentSpanID,
}
f := newReqFrame(correlationID, reqMeta)
if err = f.AppendMessage(leagcyproto.MessageV2(args)); err != nil {
return err
}
f.AppendAttachment(rpcOpts.Attachment)
select {
case s.reqCh <- struct{}{}:
default:
return ErrReachMaxConcurrency
}
return s.pipe.SendRequest(ctx, f)
}
func (s *stream) ioloop() {
for range s.reqCh {
if s.unavailable {
s.eventHub.emitError(ErrUnavailable)
}
f, err := s.pipe.ReceiveResponse(context.Background())
if err != nil {
s.eventHub.emitError(err)
continue
}
s.eventHub.emitResp(f.RPCMeta.GetCorrelationId(), f, nil)
}
}