-
Notifications
You must be signed in to change notification settings - Fork 0
/
curio_channel.py
302 lines (226 loc) · 9.95 KB
/
curio_channel.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
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
import collections
import itertools
import traceback
import time
import curio
from exceptions import RemoteException
import protocol
async def open_connection(host, port, *, create_connection=None):
"""Return a :class:`Channel` connected to *addr*."""
if create_connection is None:
create_connection = protocol.Connection.get_factory()
async with curio.timeout_after(1):
while True:
try:
socket = await curio.open_connection(host, port)
break
except ConnectionRefusedError:
await curio.sleep(0.1)
connection = create_connection()
return await Channel.create(socket, connection)
async def start_server(host, port, client_connected_task, *,
create_connection=None):
"""Start a server listening on *addr* and call *client_connected_cb*
for every client connecting to it.
"""
if create_connection is None:
create_connection = protocol.Connection.get_factory()
async def task(socket, _addr):
connection = create_connection()
channel = await Channel.create(socket, connection)
return await client_connected_task(channel)
return await curio.spawn(curio.tcp_server(host, port, task))
class Message:
"""Outgoing message that will eventually receive the peer's reply.
We need this b/c :class:`curio.Event` cannot contain user data.
"""
__no_value = object()
def __init__(self):
self._exception = self.__no_value
self._value = self.__no_value
self._event = curio.Event()
def is_set(self):
return self._event.is_set()
async def set_reply(self, value):
self._exception = False
self._value = value
await self._event.set()
async def set_exception(self, value):
self._exception = True
self._value = value
await self._event.set()
async def reply(self):
await self._event.wait()
if self._exception:
raise self._value
else:
return self._value
class Request:
"""Represents a request returned by :meth:`Channel.recv()`. You shoudn't
instantiate it yourself.
*channel* is the :class:`Channel` instance that received the request.
*content* contains the incoming message.
*msg_id* is the ID for that message. It is unique within a channel.
To reply to that request you can ``await`` :meth:`Request.reply()` or
:meth:`Request.fail()`.
"""
def __init__(self, channel, message_id, content):
self._channel = channel
self._msg_id = message_id
self._content = content
@property
def content(self):
"""The content of the incoming message."""
return self._content
async def reply(self, result):
"""Reply to the request with the provided result."""
await self._channel._send_result(self._msg_id, result)
async def fail(self, exception):
"""Indicate a failure described by the *exception* instance.
This will raise a :exc:`~aiomas.exceptions.RemoteException` on the
other side of the channel.
"""
stacktrace = traceback.format_exception(exception.__class__, exception,
exception.__traceback__)
await self._channel._send_exception(self._msg_id, ''.join(stacktrace))
class Channel:
"""A Channel represents a request-reply channel between two endpoints. An
instance of it is returned by :func:`open_connection()` or is passed to the
callback of :func:`start_server()`.
*protocol* is an instance of :class:`ChannelProtocol`.
*transport* is an :class:`asyncio.BaseTransport`.
*loop* is an instance of an :class:`asyncio.AbstractEventLoop`.
"""
def __init__(self, socket, connection):
self._socket = socket
self._connection = connection
self._message_id = itertools.count()
self._out_messages = {} # message_id -> message
self._in_queue = collections.deque()
self._waiter = None # A future.
self._receiver = None
@classmethod
async def create(cls, socket, connection):
channel = cls(socket, connection)
channel._receiver = await curio.spawn(channel._recv_messages())
return channel
async def send(self, content):
"""Send a request *content* to the other end and return a future which
is triggered when a reply arrives.
One of the following exceptions may be raised:
- :exc:`ValueError` if the message is too long (the length of the
encoded message does not fit into a *long*, which is ~ 4 GiB).
- :exc:`~aiomas.exceptions.RemoteException`: The remote site raised an
exception during the computation of the result.
- :exc:`ConnectionError` (or its subclass :exc:`ConnectionResetError`):
The connection was closed during the request.
- :exc:`RuntimeError`:
- If an invalid message type was received.
- If the future returned by this method was already triggered or
canceled by a third party when an answer to the request arrives
(e.g., if a task containing the future is cancelled). You get
more detailed exception messages if you `enable asyncio's debug
mode`__
__ https://docs.python.org/3/library/asyncio-dev.html
.. code-block:: python
try:
result = await channel.send('ohai')
except RemoteException as exc:
print(exc)
"""
message_id = next(self._message_id)
out_message = Message()
self._out_messages[message_id] = out_message
await self._send_request(message_id, content)
return await out_message.reply()
async def recv(self):
"""Wait for an incoming :class:`Request` and return it.
May raise one of the following exceptions:
- :exc:`ConnectionError` (or its subclass :exc:`ConnectionResetError`):
The connection was closed during the request.
- :exc:`RuntimeError`: If two processes try to read from the same
channel or if an invalid message type was received.
"""
if not self._in_queue:
if self._waiter is not None:
raise RuntimeError('recv() called while another coroutine is '
'already waiting for incoming data.')
self._waiter = curio.Event()
try:
await self._waiter.wait()
finally:
# FIXME: Curio events don't have values and exceptions.
#
# In case of an exception, "self._waiter" is already set to
# "None" by "self._set_exception()":
self._waiter = None
return self._in_queue.popleft()
async def close(self):
await self._socket.close()
await self._receiver.cancel()
async def _send_request(self, msg_id, content):
return await self._send(protocol.REQUEST, msg_id, content)
async def _send_result(self, msg_id, content):
return await self._send(protocol.RESULT, msg_id, content)
async def _send_exception(self, msg_id, content):
return await self._send(protocol.EXCEPTION, msg_id, content)
async def _send(self, msg_type, msg_id, content):
data = self._connection.produce_bytes(msg_type, msg_id, content)
await self._socket.sendall(data)
async def _recv_messages(self):
"""Called by :class:`ChannelProtocol` when a new message arrived."""
while True:
data = await self._socket.recv(1024)
if not data:
break
msgs = self._connection.receive_bytes(data)
for msg_type, msg_id, content in msgs:
if msg_type == protocol.REQUEST:
# Received new request
message = Request(self, msg_id, content)
self._in_queue.append(message)
waiter = self._waiter
if waiter is not None:
self._waiter = None
await waiter.set()
elif msg_type in (protocol.RESULT, protocol.EXCEPTION):
# Received reply to a request
message = self._out_messages.pop(msg_id)
if message.is_set():
errmsg = 'Request reply already set.'
# if message.cancelled():
# errmsg = 'Request was cancelled.'
raise RuntimeError(errmsg)
if msg_type == protocol.RESULT:
await message.set_reply(content)
else:
origin = self._socket.getpeername()
exc = RemoteException(origin, content)
await message.set_exception(exc)
else:
raise RuntimeError(f'Invalid message type {msg_type}')
def main(host, port, client_count, msg_count, latencies):
async def client(host, port, client_id, msg_count):
channel = await open_connection(host, port)
for i in range(msg_count):
start = time.monotonic()
reply = await channel.send(f'ohai {client_id}-{i}')
latencies.append(time.monotonic() - start)
await channel.close()
async def handle_client(channel):
try:
while True:
request = await channel.recv()
# print(request.content)
await request.reply(request.content)
except curio.CancelledError:
await channel.close()
async def run():
server = await start_server(host, port, handle_client)
async with curio.TaskGroup() as g:
for i in range(client_count):
await g.spawn(client(host, port, i, msg_count))
await server.cancel()
curio.run(run())
if __name__ == '__main__':
main('localhost', 5555, 1, 1, [])