/
_subscription_ws_handler.py
261 lines (218 loc) · 8.39 KB
/
_subscription_ws_handler.py
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
import json
from asyncio import ensure_future, shield, wait
from typing import Any, AsyncIterator, Dict, Optional, Set
from aiohttp import WSMsgType, web
from tartiflette_aiohttp._constants import (
GQL_COMPLETE,
GQL_CONNECTION_ACK,
GQL_CONNECTION_ERROR,
GQL_CONNECTION_INIT,
GQL_CONNECTION_TERMINATE,
GQL_DATA,
GQL_ERROR,
GQL_START,
GQL_STOP,
WS_PROTOCOL,
)
_ALLOWED_ERROR_TYPES = [GQL_CONNECTION_ERROR, GQL_ERROR]
def _get_graphql_params(payload: Dict[str, Any]) -> Dict[str, Any]:
return {
"query": payload.get("query"),
"variables": payload.get("variables"),
"operation_name": payload.get("operationName"),
"context": payload.get("context"),
}
class ConnectionClosedException(Exception):
pass
class AIOHTTPConnectionContext:
def __init__(self, socket: "WebSocketResponse") -> None:
self._socket: "WebSocketResponse" = socket
self._operations: Dict[str, AsyncIterator] = {}
@property
def operations(self) -> Dict[str, AsyncIterator]:
return self._operations
@property
def closed(self) -> bool:
return self._socket.closed
def has_operation(self, operation_id: str) -> bool:
return operation_id in self._operations
def register_operation(
self, operation_id: str, async_iterator: AsyncIterator
) -> None:
self._operations[operation_id] = async_iterator
def get_operation(self, operation_id: str) -> AsyncIterator:
return self._operations[operation_id]
def remove_operation(self, operation_id: str) -> None:
del self._operations[operation_id]
async def receive(self) -> str:
msg = await self._socket.receive()
if msg.type == WSMsgType.TEXT:
return msg.data
raise ConnectionClosedException()
async def send(self, data: str) -> None:
if self.closed:
return
await self._socket.send_str(data)
async def close(self, code: int) -> None:
await self._socket.close(code=code)
class AIOHTTPSubscriptionHandler:
def __init__(self, app: "Application") -> None:
self._app: "Application" = app
async def _send_message(
self,
connection_context: "AIOHTTPConnectionContext",
operation_id: Optional[str] = None,
op_type: Optional[str] = None,
payload: Optional[Any] = None,
) -> None:
message = {}
if operation_id is not None:
message["id"] = operation_id
if op_type is not None:
message["type"] = op_type
if payload is not None:
message["payload"] = payload
await connection_context.send(json.dumps(message))
async def _send_error(
self,
connection_context: "AIOHTTPConnectionContext",
operation_id: Optional[str],
error: Exception,
error_type: Optional[str] = None,
) -> None:
await self._send_message(
connection_context,
operation_id,
error_type if error_type in _ALLOWED_ERROR_TYPES else GQL_ERROR,
{"message": str(error)},
)
async def _on_connection_init(
self, connection_context: "AIOHTTPConnectionContext", operation_id: str
) -> None:
try:
return await self._send_message(
connection_context, op_type=GQL_CONNECTION_ACK
)
except Exception as e: # pylint: disable=broad-except
await self._send_error(
connection_context, operation_id, e, GQL_CONNECTION_ERROR
)
return await connection_context.close(1011)
async def _on_connection_terminate(
self, connection_context: "AIOHTTPConnectionContext"
) -> None:
await connection_context.close(1011)
async def _unsubscribe(
self, connection_context: "AIOHTTPConnectionContext", operation_id: str
) -> None:
try:
await connection_context.get_operation(operation_id).aclose()
connection_context.remove_operation(operation_id)
except KeyError:
pass
async def _on_start(
self,
connection_context: "AIOHTTPConnectionContext",
operation_id: str,
payload: Dict[str, Any],
) -> None:
params = _get_graphql_params(payload)
if not isinstance(params, dict):
return await self._send_error(
connection_context,
operation_id,
Exception("Received invalid params."),
)
if connection_context.has_operation(operation_id):
await self._unsubscribe(connection_context, operation_id)
iterator = self._app["ttftt_engine"].subscribe(**params)
connection_context.register_operation(operation_id, iterator)
try:
async for result in iterator:
if not connection_context.has_operation(operation_id):
break
await self._send_message(
connection_context, operation_id, GQL_DATA, result
)
except Exception: # pylint: disable=broad-except
await self._send_error(
connection_context, operation_id, Exception("Internal Error")
)
await self._send_message(
connection_context, operation_id, GQL_COMPLETE
)
async def _on_stop(
self, connection_context: "AIOHTTPConnectionContext", operation_id: str
) -> None:
await self._unsubscribe(connection_context, operation_id)
async def _process_message(
self,
connection_context: "AIOHTTPConnectionContext",
parsed_message: Dict[str, Any],
) -> None:
op_type = parsed_message.get("type")
operation_id = parsed_message.get("id")
payload = parsed_message.get("payload")
if op_type == GQL_CONNECTION_INIT:
return await self._on_connection_init(
connection_context, operation_id
)
if op_type == GQL_START:
return await self._on_start(
connection_context, operation_id, payload
)
if op_type == GQL_STOP:
return await self._on_stop(connection_context, operation_id)
if op_type == GQL_CONNECTION_TERMINATE:
return await self._on_connection_terminate(connection_context)
return await self._send_error(
connection_context,
operation_id,
Exception(f"Unhandled message type < {op_type} >."),
)
async def _on_message(
self, connection_context: "AIOHTTPConnectionContext", message: str
) -> None:
try:
if not isinstance(message, dict):
parsed_message = json.loads(message)
if not isinstance(parsed_message, dict):
raise TypeError("Payload must be an object.")
else:
parsed_message = message
except Exception as e: # pylint: disable=broad-except
return await self._send_error(connection_context, None, e)
return await self._process_message(connection_context, parsed_message)
async def _on_close(
self,
connection_context: "AIOHTTPConnectionContext",
tasks: Set["Task"],
) -> None:
for operation_id in connection_context.operations:
await self._unsubscribe(connection_context, operation_id)
for task in tasks:
task.cancel()
async def _handle_request(self) -> None:
connection_context = AIOHTTPConnectionContext(self._socket)
tasks: Set["Task"] = set()
while True:
try:
if connection_context.closed:
raise ConnectionClosedException()
message = await connection_context.receive()
except ConnectionClosedException:
break
finally:
if tasks:
_, tasks = await wait(tasks, timeout=0)
tasks.add(
ensure_future(self._on_message(connection_context, message))
)
await self._on_close(connection_context, tasks)
async def __call__(self, request: "Request") -> "WebSocketResponse":
self._socket = web.WebSocketResponse( # pylint: disable=attribute-defined-outside-init
protocols=(WS_PROTOCOL,)
)
await self._socket.prepare(request)
await shield(self._handle_request())
return self._socket