-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_session.py
More file actions
529 lines (488 loc) · 19.2 KB
/
client_session.py
File metadata and controls
529 lines (488 loc) · 19.2 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import asyncio
import logging
from collections.abc import AsyncIterable
from datetime import timedelta
from typing import Any, AsyncGenerator, Callable, Coroutine, assert_never
import nanoid
import websockets
from aiochannel import Channel
from aiochannel.errors import ChannelClosed
from opentelemetry.trace import Span
from websockets.exceptions import ConnectionClosed
from replit_river.error_schema import (
ERROR_CODE_CANCEL,
ERROR_CODE_STREAM_CLOSED,
RiverException,
RiverServiceException,
StreamClosedRiverServiceException,
exception_from_message,
)
from replit_river.messages import (
FailedSendingMessageException,
parse_transport_msg,
)
from replit_river.seq_manager import (
IgnoreMessage,
InvalidMessageException,
OutOfOrderMessageException,
)
from replit_river.session import Session
from replit_river.transport_options import MAX_MESSAGE_BUFFER_SIZE, TransportOptions
from .rpc import (
ACK_BIT,
STREAM_OPEN_BIT,
ErrorType,
InitType,
RequestType,
ResponseType,
)
logger = logging.getLogger(__name__)
STREAM_CLOSED_BIT = 0x0004 # Synonymous with the cancel bit in v2
class ClientSession(Session):
def __init__(
self,
transport_id: str,
to_id: str,
session_id: str,
websocket: websockets.WebSocketCommonProtocol,
transport_options: TransportOptions,
close_session_callback: Callable[[Session], Coroutine[Any, Any, Any]],
retry_connection_callback: (
Callable[
[],
Coroutine[Any, Any, Any],
]
| None
) = None,
) -> None:
super().__init__(
transport_id=transport_id,
to_id=to_id,
session_id=session_id,
websocket=websocket,
transport_options=transport_options,
close_session_callback=close_session_callback,
retry_connection_callback=retry_connection_callback,
)
async def do_close_websocket() -> None:
await self.close_websocket(
self._ws_wrapper,
should_retry=True,
)
await self._begin_close_session_countdown()
self._setup_heartbeats_task(do_close_websocket)
async def replace_with_new_websocket(
self, new_ws: websockets.WebSocketCommonProtocol
) -> None:
await super().replace_with_new_websocket(new_ws)
# serve() terminates itself when the ws dies, so we need to start it again
await self.start_serve_responses()
async def start_serve_responses(self) -> None:
self._task_manager.create_task(self.serve())
async def serve(self) -> None:
"""Serve messages from the websocket."""
self._reset_session_close_countdown()
try:
try:
await self._handle_messages_from_ws()
except ConnectionClosed:
if self._should_abort_streams_after_transport_failure():
await self.close()
if self._retry_connection_callback:
self._task_manager.create_task(self._retry_connection_callback())
await self._begin_close_session_countdown()
logger.debug("ConnectionClosed while serving", exc_info=True)
except FailedSendingMessageException:
# Expected error if the connection is closed.
if self._should_abort_streams_after_transport_failure():
await self.close()
logger.debug(
"FailedSendingMessageException while serving", exc_info=True
)
except Exception:
await self.close()
logger.exception("caught exception at message iterator")
except ExceptionGroup as eg:
_, unhandled = eg.split(lambda e: isinstance(e, ConnectionClosed))
if unhandled:
raise ExceptionGroup(
"Unhandled exceptions on River server", unhandled.exceptions
)
async def _handle_messages_from_ws(self) -> None:
logger.debug(
"%s start handling messages from ws %s",
"client",
self._ws_wrapper.id,
)
try:
ws_wrapper = self._ws_wrapper
async for message in ws_wrapper.ws:
try:
if not ws_wrapper.is_open():
# We should not process messages if the websocket is closed.
break
msg = parse_transport_msg(message)
if isinstance(msg, str):
logger.debug("Ignoring transport message", exc_info=True)
continue
logger.debug(f"{self._transport_id} got a message %r", msg)
# Update bookkeeping
match self._seq_manager.check_seq_and_update(msg):
case IgnoreMessage():
continue
case None:
pass
case other:
assert_never(other)
await self._buffer.remove_old_messages(
self._seq_manager.receiver_ack,
)
self._reset_session_close_countdown()
if msg.controlFlags & ACK_BIT != 0:
continue
stream = self._streams.get(msg.streamId, None)
if msg.controlFlags & STREAM_OPEN_BIT == 0:
if not stream:
logger.warning("no stream for %s", msg.streamId)
continue
if (
msg.controlFlags & STREAM_CLOSED_BIT != 0
and msg.payload.get("type", None) == "CLOSE"
):
# close message is not sent to the stream
pass
else:
try:
await stream.put(msg.payload)
except ChannelClosed:
# The client is no longer interested in this stream,
# just drop the message.
pass
except Exception as e:
raise InvalidMessageException(e) from e
else:
raise InvalidMessageException(
"Client should not receive stream open bit"
)
if msg.controlFlags & STREAM_CLOSED_BIT != 0:
if stream:
stream.close()
del self._streams[msg.streamId]
except OutOfOrderMessageException:
logger.exception("Out of order message, closing connection")
await ws_wrapper.close()
return
except InvalidMessageException:
logger.exception("Got invalid transport message, closing session")
await self.close()
return
except ConnectionClosed as e:
raise e
async def send_rpc(
self,
service_name: str,
procedure_name: str,
request: RequestType,
request_serializer: Callable[[RequestType], Any],
response_deserializer: Callable[[Any], ResponseType],
error_deserializer: Callable[[Any], ErrorType],
span: Span,
timeout: timedelta,
) -> ResponseType:
"""Sends a single RPC request to the server.
Expects the input and output be messages that will be msgpacked.
"""
stream_id = nanoid.generate()
output: Channel[Any] = Channel(1)
self._streams[stream_id] = output
await self.send_message(
stream_id=stream_id,
control_flags=STREAM_OPEN_BIT | STREAM_CLOSED_BIT,
payload=request_serializer(request),
service_name=service_name,
procedure_name=procedure_name,
span=span,
)
# Handle potential errors during communication
try:
try:
async with asyncio.timeout(timeout.total_seconds()):
response = await output.get()
except asyncio.TimeoutError as e:
# TODO(dstewart) After protocol v2, change this to STREAM_CANCEL_BIT
await self.send_message(
stream_id=stream_id,
control_flags=STREAM_CLOSED_BIT,
payload={"type": "CLOSE"},
service_name=service_name,
procedure_name=procedure_name,
span=span,
)
raise RiverException(ERROR_CODE_CANCEL, str(e)) from e
except ChannelClosed as e:
raise RiverServiceException(
ERROR_CODE_STREAM_CLOSED,
"Stream closed before response",
service_name,
procedure_name,
) from e
except Exception as e:
raise RiverException(ERROR_CODE_STREAM_CLOSED, str(e)) from e
if not response.get("ok", False):
try:
error = error_deserializer(response["payload"])
except Exception as e:
raise RiverException("error_deserializer", str(e)) from e
raise exception_from_message(error.code)(
error.code,
error.message,
service_name,
procedure_name,
underlying_error=error,
)
return response_deserializer(response["payload"])
except RiverException as e:
raise e
except Exception as e:
raise e
async def send_upload(
self,
service_name: str,
procedure_name: str,
init: InitType | None,
request: AsyncIterable[RequestType],
init_serializer: Callable[[InitType], Any] | None,
request_serializer: Callable[[RequestType], Any],
response_deserializer: Callable[[Any], ResponseType],
error_deserializer: Callable[[Any], ErrorType],
span: Span,
) -> ResponseType:
"""Sends an upload request to the server.
Expects the input and output be messages that will be msgpacked.
"""
stream_id = nanoid.generate()
output: Channel[Any] = Channel(1)
self._streams[stream_id] = output
first_message = True
try:
if init and init_serializer:
await self.send_message(
stream_id=stream_id,
control_flags=STREAM_OPEN_BIT,
service_name=service_name,
procedure_name=procedure_name,
payload=init_serializer(init),
span=span,
)
first_message = False
# If this request is not closed and the session is killed, we should
# throw exception here
async for item in request:
control_flags = 0
if first_message:
control_flags = STREAM_OPEN_BIT
first_message = False
await self.send_message(
stream_id=stream_id,
service_name=service_name,
procedure_name=procedure_name,
control_flags=control_flags,
payload=request_serializer(item),
span=span,
)
except Exception as e:
raise RiverServiceException(
ERROR_CODE_STREAM_CLOSED, str(e), service_name, procedure_name
) from e
await self.send_close_stream(
service_name,
procedure_name,
stream_id,
extra_control_flags=STREAM_OPEN_BIT if first_message else 0,
)
# Handle potential errors during communication
# TODO: throw a error when the transport is hard closed
try:
try:
response = await output.get()
except ChannelClosed as e:
raise RiverServiceException(
ERROR_CODE_STREAM_CLOSED,
"Stream closed before response",
service_name,
procedure_name,
) from e
except Exception as e:
raise RiverException(ERROR_CODE_STREAM_CLOSED, str(e)) from e
if not response.get("ok", False):
try:
error = error_deserializer(response["payload"])
except Exception as e:
raise RiverException("error_deserializer", str(e)) from e
raise exception_from_message(error.code)(
error.code,
error.message,
service_name,
procedure_name,
underlying_error=error,
)
return response_deserializer(response["payload"])
except RiverException as e:
raise e
except Exception as e:
raise e
async def send_subscription(
self,
service_name: str,
procedure_name: str,
request: RequestType,
request_serializer: Callable[[RequestType], Any],
response_deserializer: Callable[[Any], ResponseType],
error_deserializer: Callable[[Any], ErrorType],
span: Span,
) -> AsyncGenerator[ResponseType | ErrorType, None]:
"""Sends a subscription request to the server.
Expects the input and output be messages that will be msgpacked.
"""
stream_id = nanoid.generate()
output: Channel[Any] = Channel(MAX_MESSAGE_BUFFER_SIZE)
self._streams[stream_id] = output
await self.send_message(
service_name=service_name,
procedure_name=procedure_name,
stream_id=stream_id,
control_flags=STREAM_OPEN_BIT,
payload=request_serializer(request),
span=span,
)
# Handle potential errors during communication
try:
async for item in output:
if item.get("type", None) == "CLOSE":
break
if not item.get("ok", False):
try:
yield error_deserializer(item["payload"])
except Exception:
logger.exception(
f"Error during subscription error deserialization: {item}"
)
continue
yield response_deserializer(item["payload"])
except Exception as e:
raise RiverServiceException(
ERROR_CODE_STREAM_CLOSED,
"Stream closed before response",
service_name,
procedure_name,
) from e
finally:
output.close()
async def send_stream(
self,
service_name: str,
procedure_name: str,
init: InitType | None,
request: AsyncIterable[RequestType],
init_serializer: Callable[[InitType], Any] | None,
request_serializer: Callable[[RequestType], Any],
response_deserializer: Callable[[Any], ResponseType],
error_deserializer: Callable[[Any], ErrorType],
span: Span,
) -> AsyncGenerator[ResponseType | ErrorType, None]:
"""Sends a subscription request to the server.
Expects the input and output be messages that will be msgpacked.
"""
stream_id = nanoid.generate()
output: Channel[Any] = Channel(MAX_MESSAGE_BUFFER_SIZE)
self._streams[stream_id] = output
empty_stream = False
try:
if init and init_serializer:
await self.send_message(
service_name=service_name,
procedure_name=procedure_name,
stream_id=stream_id,
control_flags=STREAM_OPEN_BIT,
payload=init_serializer(init),
span=span,
)
else:
# Get the very first message to open the stream
request_iter = aiter(request)
first = await anext(request_iter)
await self.send_message(
service_name=service_name,
procedure_name=procedure_name,
stream_id=stream_id,
control_flags=STREAM_OPEN_BIT,
payload=request_serializer(first),
span=span,
)
except StopAsyncIteration:
empty_stream = True
except Exception as e:
raise StreamClosedRiverServiceException(
ERROR_CODE_STREAM_CLOSED, str(e), service_name, procedure_name
) from e
# Create the encoder task
async def _encode_stream() -> None:
if empty_stream:
await self.send_close_stream(
service_name,
procedure_name,
stream_id,
extra_control_flags=STREAM_OPEN_BIT,
)
return
async for item in request:
if item is None:
continue
await self.send_message(
service_name=service_name,
procedure_name=procedure_name,
stream_id=stream_id,
control_flags=0,
payload=request_serializer(item),
)
await self.send_close_stream(service_name, procedure_name, stream_id)
self._task_manager.create_task(_encode_stream())
# Handle potential errors during communication
try:
async for item in output:
if "type" in item and item["type"] == "CLOSE":
break
if not item.get("ok", False):
try:
yield error_deserializer(item["payload"])
except Exception:
logger.exception(
f"Error during subscription error deserialization: {item}"
)
continue
yield response_deserializer(item["payload"])
except Exception as e:
logger.exception("There was a problem")
raise RiverServiceException(
ERROR_CODE_STREAM_CLOSED,
"Stream closed before response",
service_name,
procedure_name,
) from e
async def send_close_stream(
self,
service_name: str,
procedure_name: str,
stream_id: str,
extra_control_flags: int = 0,
) -> None:
# close stream
await self.send_message(
service_name=service_name,
procedure_name=procedure_name,
stream_id=stream_id,
control_flags=STREAM_CLOSED_BIT | extra_control_flags,
payload={
"type": "CLOSE",
},
)