|
1 | 1 | import asyncio |
2 | 2 | import enum |
3 | 3 | import logging |
4 | | -from typing import Any, Awaitable, Callable, Coroutine, Protocol |
| 4 | +from typing import Any, Callable, Coroutine, Protocol |
5 | 5 |
|
6 | 6 | from opentelemetry.trace import Span |
7 | 7 | from websockets import WebSocketCommonProtocol |
|
12 | 12 | WebsocketClosedException, |
13 | 13 | send_transport_message, |
14 | 14 | ) |
15 | | -from replit_river.rpc import ACK_BIT, TransportMessage |
| 15 | +from replit_river.rpc import TransportMessage |
16 | 16 |
|
17 | 17 | logger = logging.getLogger(__name__) |
18 | 18 |
|
@@ -52,86 +52,6 @@ class SessionState(enum.Enum): |
52 | 52 | TerminalStates = set([SessionState.CLOSING, SessionState.CLOSED]) |
53 | 53 |
|
54 | 54 |
|
55 | | -async def setup_heartbeat( |
56 | | - session_id: str, |
57 | | - heartbeat_ms: float, |
58 | | - heartbeats_until_dead: int, |
59 | | - get_state: Callable[[], SessionState], |
60 | | - get_closing_grace_period: Callable[[], float | None], |
61 | | - close_websocket: Callable[[], Awaitable[None]], |
62 | | - send_message: SendMessage, |
63 | | - increment_and_get_heartbeat_misses: Callable[[], int], |
64 | | -) -> None: |
65 | | - while True: |
66 | | - await asyncio.sleep(heartbeat_ms / 1000) |
67 | | - state = get_state() |
68 | | - if state == SessionState.CONNECTING: |
69 | | - logger.debug("Websocket is not connected, not sending heartbeat") |
70 | | - continue |
71 | | - if state in TerminalStates: |
72 | | - logger.debug( |
73 | | - "Session is closed, no need to send heartbeat, state : " |
74 | | - "%r close_session_after_this: %r", |
75 | | - {state}, |
76 | | - {get_closing_grace_period()}, |
77 | | - ) |
78 | | - # session is closing / closed, no need to send heartbeat anymore |
79 | | - return |
80 | | - try: |
81 | | - await send_message( |
82 | | - stream_id="heartbeat", |
83 | | - # TODO: make this a message class |
84 | | - # https://github.com/replit/river/blob/741b1ea6d7600937ad53564e9cf8cd27a92ec36a/transport/message.ts#L42 |
85 | | - payload={ |
86 | | - "ack": 0, |
87 | | - }, |
88 | | - control_flags=ACK_BIT, |
89 | | - procedure_name=None, |
90 | | - service_name=None, |
91 | | - span=None, |
92 | | - ) |
93 | | - |
94 | | - if increment_and_get_heartbeat_misses() > heartbeats_until_dead: |
95 | | - if get_closing_grace_period() is not None: |
96 | | - # already in grace period, no need to set again |
97 | | - continue |
98 | | - logger.info( |
99 | | - "%r closing websocket because of heartbeat misses", |
100 | | - session_id, |
101 | | - ) |
102 | | - await close_websocket() |
103 | | - continue |
104 | | - except FailedSendingMessageException: |
105 | | - # this is expected during websocket closed period |
106 | | - continue |
107 | | - |
108 | | - |
109 | | -async def check_to_close_session( |
110 | | - transport_id: str, |
111 | | - close_session_check_interval_ms: float, |
112 | | - get_state: Callable[[], SessionState], |
113 | | - get_current_time: Callable[[], Awaitable[float]], |
114 | | - get_close_session_after_time_secs: Callable[[], float | None], |
115 | | - do_close: Callable[[], Awaitable[None]], |
116 | | -) -> None: |
117 | | - while True: |
118 | | - await asyncio.sleep(close_session_check_interval_ms / 1000) |
119 | | - if get_state() in TerminalStates: |
120 | | - # already closing |
121 | | - return |
122 | | - # calculate the value now before comparing it so that there are no |
123 | | - # await points between the check and the comparison to avoid a TOCTOU |
124 | | - # race. |
125 | | - current_time = await get_current_time() |
126 | | - close_session_after_time_secs = get_close_session_after_time_secs() |
127 | | - if not close_session_after_time_secs: |
128 | | - continue |
129 | | - if current_time > close_session_after_time_secs: |
130 | | - logger.info("Grace period ended for %s, closing session", transport_id) |
131 | | - await do_close() |
132 | | - return |
133 | | - |
134 | | - |
135 | 55 | async def buffered_message_sender( |
136 | 56 | connection_condition: asyncio.Condition, |
137 | 57 | message_enqueued: asyncio.Semaphore, |
|
0 commit comments