forked from libp2p/go-libp2p-daemon
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpersistent_stream.go
385 lines (318 loc) · 9.53 KB
/
persistent_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package p2pd
import (
"context"
"fmt"
"io"
"github.com/google/uuid"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
ggio "github.com/gogo/protobuf/io"
"github.com/learning-at-home/go-libp2p-daemon/internal/utils"
pb "github.com/learning-at-home/go-libp2p-daemon/pb"
"github.com/libp2p/go-libp2p/core/protocol"
)
func (d *Daemon) handlePersistentConn(r ggio.Reader, unsafeW ggio.WriteCloser) {
w := utils.NewSafeWriter(unsafeW)
var streamHandlers []string
defer func() {
d.mx.Lock()
defer d.mx.Unlock()
for _, proto := range streamHandlers {
p := protocol.ID(proto)
d.registeredUnaryProtocols[p].Remove(w)
if d.registeredUnaryProtocols[p].Len() == 0 {
d.host.RemoveStreamHandler(p)
delete(d.registeredUnaryProtocols, p)
}
}
}()
if d.cancelTerminateTimer != nil {
d.cancelTerminateTimer()
}
d.terminateWG.Add(1)
defer d.terminateWG.Done()
d.terminateOnce.Do(func() { go d.awaitTermination() })
if err := w.WriteMsg(&pb.Response{Type: pb.Response_OK.Enum()}); err != nil {
log.Debugw("error writing message", "error", err)
return
}
for {
var req pb.PersistentConnectionRequest
if err := r.ReadMsg(&req); err != nil {
if err != io.EOF {
log.Debugw("error reading message", "error", err)
}
return
}
go d.handlePersistentConnRequest(req, w, &streamHandlers)
}
}
func (d *Daemon) handlePersistentConnRequest(req pb.PersistentConnectionRequest, w ggio.WriteCloser, streamHandlers *[]string) {
callID, err := uuid.FromBytes(req.CallId)
if err != nil {
log.Debugw("bad call id: ", "error", err)
return
}
switch req.Message.(type) {
case *pb.PersistentConnectionRequest_AddUnaryHandler:
resp := d.doAddUnaryHandler(w, callID, req.GetAddUnaryHandler())
d.mx.Lock()
if _, ok := resp.Message.(*pb.PersistentConnectionResponse_DaemonError); !ok {
*streamHandlers = append(
*streamHandlers,
*req.GetAddUnaryHandler().Proto,
)
}
d.mx.Unlock()
if err := w.WriteMsg(resp); err != nil {
log.Debugw("error writing message", "error", err)
return
}
case *pb.PersistentConnectionRequest_RemoveUnaryHandler:
resp := d.doRemoveUnaryHandler(w, callID, req.GetRemoveUnaryHandler())
d.mx.Lock()
if _, ok := resp.Message.(*pb.PersistentConnectionResponse_DaemonError); !ok {
for index, proto := range *streamHandlers {
if proto == *req.GetRemoveUnaryHandler().Proto {
*streamHandlers = append((*streamHandlers)[:index], (*streamHandlers)[index+1:]...)
break
}
}
}
d.mx.Unlock()
if err := w.WriteMsg(resp); err != nil {
log.Debugw("error writing message", "error", err)
return
}
case *pb.PersistentConnectionRequest_CallUnary:
ctx, cancel := context.WithCancel(d.ctx)
d.cancelUnary.Store(callID, cancel)
defer cancel()
defer d.cancelUnary.Delete(callID)
resp := d.doUnaryCall(ctx, callID, &req)
if err := w.WriteMsg(resp); err != nil {
log.Debugw("error reading message", "error", err)
return
}
case *pb.PersistentConnectionRequest_UnaryResponse:
d.sendReponseToRemote(&req)
case *pb.PersistentConnectionRequest_Cancel:
cf, found := d.cancelUnary.Load(callID)
if !found {
return
}
cf.(context.CancelFunc)()
}
}
func (d *Daemon) doAddUnaryHandler(w ggio.Writer, callID uuid.UUID, req *pb.AddUnaryHandlerRequest) *pb.PersistentConnectionResponse {
d.mx.Lock()
defer d.mx.Unlock()
p := protocol.ID(*req.Proto)
round_robin, ok := d.registeredUnaryProtocols[p]
if !ok {
d.registeredUnaryProtocols[p] = utils.NewRoundRobin(false)
d.registeredUnaryProtocols[p].Append(w)
d.host.SetStreamHandler(p, d.persistentStreamHandler)
} else if !req.GetBalanced() {
return errorUnaryCallString(
callID,
fmt.Sprintf("handler for protocol %s already set", *req.Proto),
)
} else {
round_robin.Append(w)
}
return okUnaryCallResponse(callID)
}
func (d *Daemon) doRemoveUnaryHandler(w ggio.Writer, callID uuid.UUID, req *pb.RemoveUnaryHandlerRequest) *pb.PersistentConnectionResponse {
d.mx.Lock()
defer d.mx.Unlock()
p := protocol.ID(*req.Proto)
round_robin, ok := d.registeredUnaryProtocols[p]
if !ok {
return errorUnaryCallString(
callID,
fmt.Sprintf("handler for protocol %s does not exist", *req.Proto),
)
}
ok = round_robin.Remove(w)
if !ok {
return errorUnaryCallString(
callID,
fmt.Sprintf("handler for protocol %s was not created in this persistent connection", *req.Proto),
)
}
if round_robin.Len() == 0 {
d.host.RemoveStreamHandler(p)
delete(d.registeredUnaryProtocols, p)
}
return okUnaryCallResponse(callID)
}
func (d *Daemon) doUnaryCall(ctx context.Context, callID uuid.UUID, req *pb.PersistentConnectionRequest) *pb.PersistentConnectionResponse {
pid, err := peer.IDFromBytes(req.GetCallUnary().Peer)
if err != nil {
return errorUnaryCall(callID, err)
}
remoteStream, err := d.host.NewStream(
ctx,
pid,
protocol.ID(*req.GetCallUnary().Proto),
)
if err != nil {
return errorUnaryCall(callID, err)
}
defer remoteStream.Close()
select {
case response := <-d.exchangeMessages(ctx, remoteStream, req):
return response
case <-ctx.Done():
return okUnaryCallCancelled(callID)
}
}
func (d *Daemon) exchangeMessages(ctx context.Context, s network.Stream, req *pb.PersistentConnectionRequest) <-chan *pb.PersistentConnectionResponse {
callID, _ := uuid.FromBytes(req.CallId)
rc := make(chan *pb.PersistentConnectionResponse)
go func() {
defer close(rc)
if err := ggio.NewDelimitedWriter(s).WriteMsg(req); ctx.Err() != nil {
return
} else if err != nil {
rc <- errorUnaryCall(callID, err)
return
}
remoteResp := &pb.PersistentConnectionRequest{}
if err := ggio.NewDelimitedReader(s, d.persistentConnMsgMaxSize).ReadMsg(remoteResp); ctx.Err() != nil {
return
} else if err != nil {
rc <- errorUnaryCall(callID, err)
return
}
resp := okUnaryCallResponse(callID)
resp.Message = &pb.PersistentConnectionResponse_CallUnaryResponse{
CallUnaryResponse: remoteResp.GetUnaryResponse(),
}
select {
case rc <- resp:
return
case <-ctx.Done():
return
}
}()
return rc
}
// notifyWhenClosed writers to a semaphor channel if the given io.Reader fails to
// read before the context was cancelled
func notifyWhenClosed(ctx context.Context, r io.Reader) <-chan struct{} {
event := make(chan struct{})
go func() {
defer close(event)
buff := make([]byte, 1)
if _, err := r.Read(buff); err != nil {
select {
case event <- struct{}{}:
case <-ctx.Done():
}
}
}()
return event
}
// getPersistentStreamHandler returns a libp2p stream handler tied to a
// given persistent client stream
func (d *Daemon) persistentStreamHandler(s network.Stream) {
defer s.Close()
p := s.Protocol()
d.mx.Lock()
cws, ok := d.registeredUnaryProtocols[p]
var cw ggio.Writer
if ok {
cw = cws.Next().(ggio.Writer)
}
d.mx.Unlock()
if !ok {
log.Debugw("unexpected persistent stream", "protocol", p)
return
}
req := &pb.PersistentConnectionRequest{}
if err := ggio.NewDelimitedReader(s, d.persistentConnMsgMaxSize).ReadMsg(req); err != nil {
log.Debugw("failed to read proto from incoming p2p stream", "error", err)
return
}
if req.GetCallUnary() == nil {
log.Debug("proto is expected to include callUnary but does not have it")
return
}
// now the peer field stores the caller's peer id
req.GetCallUnary().Peer = []byte(s.Conn().RemotePeer())
callID, err := uuid.FromBytes(req.CallId)
if err != nil {
log.Debugw("bad call id in p2p handler", "error", err)
return
}
rc := make(chan *pb.PersistentConnectionRequest)
d.responseWaiters.Store(callID, rc)
defer d.responseWaiters.Delete(callID)
ctx, cancel := context.WithCancel(d.ctx)
defer cancel()
resp := &pb.PersistentConnectionResponse{
CallId: req.CallId,
Message: &pb.PersistentConnectionResponse_RequestHandling{
RequestHandling: req.GetCallUnary(),
},
}
if err := cw.WriteMsg(resp); err != nil {
log.Debugw("failed to write message to client", "error", err)
return
}
select {
case <-notifyWhenClosed(ctx, s):
if err := cw.WriteMsg(
&pb.PersistentConnectionResponse{
CallId: callID[:],
Message: &pb.PersistentConnectionResponse_Cancel{
Cancel: &pb.Cancel{},
},
},
); err != nil {
log.Debugw("failed to write to client", "error", err)
}
case response := <-rc:
w := ggio.NewDelimitedWriter(s)
if err := w.WriteMsg(response); err != nil {
log.Debugw("failed to write message to remote", "error", err)
}
}
}
func (d *Daemon) sendReponseToRemote(req *pb.PersistentConnectionRequest) {
callID, err := uuid.FromBytes(req.CallId)
if err != nil {
log.Debugf("failed to unmarshal call id from bytes: %v", err)
return
}
rc, found := d.responseWaiters.Load(callID)
if !found {
log.Debugf("could not find request awaiting response for following call id: %s", callID.String())
return
}
rc.(chan *pb.PersistentConnectionRequest) <- req
}
func errorUnaryCall(callID uuid.UUID, err error) *pb.PersistentConnectionResponse {
return errorUnaryCallString(callID, err.Error())
}
func errorUnaryCallString(callID uuid.UUID, errMsg string) *pb.PersistentConnectionResponse {
return &pb.PersistentConnectionResponse{
CallId: callID[:],
Message: &pb.PersistentConnectionResponse_DaemonError{
DaemonError: &pb.DaemonError{Message: &errMsg},
},
}
}
func okUnaryCallResponse(callID uuid.UUID) *pb.PersistentConnectionResponse {
return &pb.PersistentConnectionResponse{CallId: callID[:]}
}
func okUnaryCallCancelled(callID uuid.UUID) *pb.PersistentConnectionResponse {
return &pb.PersistentConnectionResponse{
CallId: callID[:],
Message: &pb.PersistentConnectionResponse_Cancel{
Cancel: &pb.Cancel{},
},
}
}