2018-05-30 16:36:23 +00:00
|
|
|
"""
|
|
|
|
Inter-process comms abstractions
|
|
|
|
"""
|
2020-08-03 22:41:21 +00:00
|
|
|
from functools import partial
|
2021-06-11 20:38:25 +00:00
|
|
|
import struct
|
|
|
|
import typing
|
|
|
|
from typing import Any, Tuple, Optional
|
2018-05-30 16:36:23 +00:00
|
|
|
|
2021-06-11 20:38:25 +00:00
|
|
|
from tricycle import BufferedReceiveStream
|
2018-05-30 16:36:23 +00:00
|
|
|
import msgpack
|
|
|
|
import trio
|
2018-07-11 22:08:57 +00:00
|
|
|
from async_generator import asynccontextmanager
|
2018-05-30 16:36:23 +00:00
|
|
|
|
|
|
|
from .log import get_logger
|
2021-06-24 22:49:51 +00:00
|
|
|
from ._exceptions import TransportClosed
|
2021-06-11 20:38:25 +00:00
|
|
|
log = get_logger(__name__)
|
2018-05-30 16:36:23 +00:00
|
|
|
|
2020-08-03 22:41:21 +00:00
|
|
|
# :eyeroll:
|
|
|
|
try:
|
|
|
|
import msgpack_numpy
|
|
|
|
Unpacker = msgpack_numpy.Unpacker
|
|
|
|
except ImportError:
|
|
|
|
# just plain ``msgpack`` requires tweaking key settings
|
|
|
|
Unpacker = partial(msgpack.Unpacker, strict_map_key=False)
|
|
|
|
|
2018-05-30 16:36:23 +00:00
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
class MsgpackTCPStream:
|
2021-06-11 20:38:25 +00:00
|
|
|
'''A ``trio.SocketStream`` delivering ``msgpack`` formatted data
|
|
|
|
using ``msgpack-python``.
|
2021-05-30 21:19:20 +00:00
|
|
|
|
2021-06-11 20:38:25 +00:00
|
|
|
'''
|
2021-05-30 21:19:20 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
stream: trio.SocketStream,
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
2018-05-30 16:36:23 +00:00
|
|
|
self.stream = stream
|
2019-12-10 05:55:03 +00:00
|
|
|
assert self.stream.socket
|
2021-05-30 21:19:20 +00:00
|
|
|
|
2019-12-10 05:55:03 +00:00
|
|
|
# should both be IP sockets
|
|
|
|
lsockname = stream.socket.getsockname()
|
|
|
|
assert isinstance(lsockname, tuple)
|
|
|
|
self._laddr = lsockname[:2]
|
2021-05-30 21:19:20 +00:00
|
|
|
|
2019-12-10 05:55:03 +00:00
|
|
|
rsockname = stream.socket.getpeername()
|
|
|
|
assert isinstance(rsockname, tuple)
|
|
|
|
self._raddr = rsockname[:2]
|
|
|
|
|
2021-06-27 04:47:49 +00:00
|
|
|
# start first entry to read loop
|
2018-05-30 16:36:23 +00:00
|
|
|
self._agen = self._iter_packets()
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-12-15 07:20:19 +00:00
|
|
|
self._send_lock = trio.StrictFIFOLock()
|
2018-05-30 16:36:23 +00:00
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
async def _iter_packets(self) -> typing.AsyncGenerator[dict, None]:
|
2018-05-30 16:36:23 +00:00
|
|
|
"""Yield packets from the underlying stream.
|
|
|
|
"""
|
2020-08-03 22:41:21 +00:00
|
|
|
unpacker = Unpacker(
|
|
|
|
raw=False,
|
|
|
|
use_list=False,
|
|
|
|
)
|
2018-05-30 16:36:23 +00:00
|
|
|
while True:
|
2021-06-24 22:49:51 +00:00
|
|
|
data = await self.stream.receive_some(2**10)
|
|
|
|
log.trace(f"received {data}") # type: ignore
|
2018-05-30 16:36:23 +00:00
|
|
|
|
|
|
|
if data == b'':
|
2021-06-24 22:49:51 +00:00
|
|
|
raise TransportClosed(
|
|
|
|
f'transport {self} was already closed prior ro read'
|
|
|
|
)
|
2018-05-30 16:36:23 +00:00
|
|
|
|
|
|
|
unpacker.feed(data)
|
|
|
|
for packet in unpacker:
|
|
|
|
yield packet
|
|
|
|
|
2018-06-07 04:19:31 +00:00
|
|
|
@property
|
2019-12-10 05:55:03 +00:00
|
|
|
def laddr(self) -> Tuple[Any, ...]:
|
2018-06-07 04:19:31 +00:00
|
|
|
return self._laddr
|
|
|
|
|
|
|
|
@property
|
2019-12-10 05:55:03 +00:00
|
|
|
def raddr(self) -> Tuple[Any, ...]:
|
2018-06-07 04:19:31 +00:00
|
|
|
return self._raddr
|
|
|
|
|
2019-12-10 05:55:03 +00:00
|
|
|
# XXX: should this instead be called `.sendall()`?
|
|
|
|
async def send(self, data: Any) -> None:
|
2018-06-07 04:19:31 +00:00
|
|
|
async with self._send_lock:
|
|
|
|
return await self.stream.send_all(
|
2021-06-11 20:38:25 +00:00
|
|
|
msgpack.dumps(data, use_bin_type=True)
|
2021-05-30 21:19:20 +00:00
|
|
|
)
|
2018-05-30 16:36:23 +00:00
|
|
|
|
2019-03-12 02:46:49 +00:00
|
|
|
async def recv(self) -> Any:
|
2018-05-30 16:36:23 +00:00
|
|
|
return await self._agen.asend(None)
|
|
|
|
|
2018-07-25 04:27:13 +00:00
|
|
|
def __aiter__(self):
|
2018-05-30 16:36:23 +00:00
|
|
|
return self._agen
|
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
def connected(self) -> bool:
|
2018-07-04 07:16:00 +00:00
|
|
|
return self.stream.socket.fileno() != -1
|
|
|
|
|
2018-05-30 16:36:23 +00:00
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
class MsgspecTCPStream(MsgpackTCPStream):
|
2021-06-11 20:38:25 +00:00
|
|
|
'''A ``trio.SocketStream`` delivering ``msgpack`` formatted data
|
|
|
|
using ``msgspec``.
|
|
|
|
|
|
|
|
'''
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
stream: trio.SocketStream,
|
|
|
|
prefix_size: int = 4,
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
super().__init__(stream)
|
|
|
|
self.recv_stream = BufferedReceiveStream(transport_stream=stream)
|
|
|
|
self.prefix_size = prefix_size
|
|
|
|
|
2021-07-01 13:41:23 +00:00
|
|
|
import msgspec
|
|
|
|
|
|
|
|
# TODO: struct aware messaging coders
|
|
|
|
self.encode = msgspec.Encoder().encode
|
|
|
|
self.decode = msgspec.Decoder().decode # dict[str, Any])
|
|
|
|
|
2021-06-11 20:38:25 +00:00
|
|
|
async def _iter_packets(self) -> typing.AsyncGenerator[dict, None]:
|
|
|
|
"""Yield packets from the underlying stream.
|
|
|
|
"""
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
header = await self.recv_stream.receive_exactly(4)
|
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
except (ValueError):
|
|
|
|
raise TransportClosed(
|
|
|
|
f'transport {self} was already closed prior ro read'
|
|
|
|
)
|
2021-06-11 20:38:25 +00:00
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
if header == b'':
|
|
|
|
raise TransportClosed(
|
|
|
|
f'transport {self} was already closed prior ro read'
|
|
|
|
)
|
2021-06-11 20:38:25 +00:00
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
size, = struct.unpack("<I", header)
|
2021-06-11 20:38:25 +00:00
|
|
|
|
2021-07-01 18:52:52 +00:00
|
|
|
log.trace(f'received header {size}') # type: ignore
|
2021-06-11 20:38:25 +00:00
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
msg_bytes = await self.recv_stream.receive_exactly(size)
|
2021-06-11 20:38:25 +00:00
|
|
|
|
|
|
|
log.trace(f"received {msg_bytes}") # type: ignore
|
2021-07-01 13:41:23 +00:00
|
|
|
yield self.decode(msg_bytes)
|
2021-06-11 20:38:25 +00:00
|
|
|
|
|
|
|
async def send(self, data: Any) -> None:
|
|
|
|
async with self._send_lock:
|
|
|
|
|
2021-07-01 18:52:52 +00:00
|
|
|
bytes_data: bytes = self.encode(data)
|
2021-06-11 20:38:25 +00:00
|
|
|
|
|
|
|
# supposedly the fastest says,
|
|
|
|
# https://stackoverflow.com/a/54027962
|
2021-07-01 18:52:52 +00:00
|
|
|
size: bytes = struct.pack("<I", len(bytes_data))
|
2021-06-11 20:38:25 +00:00
|
|
|
|
|
|
|
return await self.stream.send_all(size + bytes_data)
|
|
|
|
|
|
|
|
|
2018-06-07 04:19:31 +00:00
|
|
|
class Channel:
|
2019-03-12 02:46:49 +00:00
|
|
|
"""An inter-process channel for communication between (remote) actors.
|
2018-05-30 16:36:23 +00:00
|
|
|
|
2019-03-12 02:46:49 +00:00
|
|
|
Currently the only supported transport is a ``trio.SocketStream``.
|
2018-05-30 16:36:23 +00:00
|
|
|
"""
|
|
|
|
def __init__(
|
2021-06-11 20:38:25 +00:00
|
|
|
|
2018-06-07 04:19:31 +00:00
|
|
|
self,
|
2018-08-31 21:16:24 +00:00
|
|
|
destaddr: Optional[Tuple[str, int]] = None,
|
|
|
|
on_reconnect: typing.Callable[..., typing.Awaitable] = None,
|
2018-06-07 04:19:31 +00:00
|
|
|
auto_reconnect: bool = False,
|
|
|
|
stream: trio.SocketStream = None, # expected to be active
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-06-07 04:19:31 +00:00
|
|
|
) -> None:
|
2021-06-11 20:38:25 +00:00
|
|
|
|
2018-05-30 16:36:23 +00:00
|
|
|
self._recon_seq = on_reconnect
|
|
|
|
self._autorecon = auto_reconnect
|
2021-07-01 13:41:23 +00:00
|
|
|
|
2021-07-01 18:52:52 +00:00
|
|
|
stream_serializer_type = MsgpackTCPStream
|
|
|
|
|
2021-07-01 13:41:23 +00:00
|
|
|
try:
|
|
|
|
# if installed load the msgspec transport since it's faster
|
|
|
|
import msgspec # noqa
|
2021-07-01 18:52:52 +00:00
|
|
|
stream_serializer_type = MsgspecTCPStream
|
2021-07-01 13:41:23 +00:00
|
|
|
except ImportError:
|
2021-07-01 18:52:52 +00:00
|
|
|
pass
|
2021-07-01 13:41:23 +00:00
|
|
|
|
2021-06-11 20:38:25 +00:00
|
|
|
self.stream_serializer_type = stream_serializer_type
|
2021-07-01 18:52:52 +00:00
|
|
|
self.msgstream = stream_serializer_type(stream) if stream else None
|
2021-06-11 20:38:25 +00:00
|
|
|
|
2019-03-12 02:46:49 +00:00
|
|
|
if self.msgstream and destaddr:
|
2018-06-07 04:19:31 +00:00
|
|
|
raise ValueError(
|
|
|
|
f"A stream was provided with local addr {self.laddr}"
|
|
|
|
)
|
2021-06-11 20:38:25 +00:00
|
|
|
|
2019-03-12 02:46:49 +00:00
|
|
|
self._destaddr = self.msgstream.raddr if self.msgstream else destaddr
|
2021-07-01 18:52:52 +00:00
|
|
|
|
2018-06-21 21:09:22 +00:00
|
|
|
# set after handshake - always uid of far end
|
2018-08-31 21:16:24 +00:00
|
|
|
self.uid: Optional[Tuple[str, str]] = None
|
2021-07-01 18:52:52 +00:00
|
|
|
|
2018-08-31 21:16:24 +00:00
|
|
|
# set if far end actor errors internally
|
|
|
|
self._exc: Optional[Exception] = None
|
2018-08-06 17:40:37 +00:00
|
|
|
self._agen = self._aiter_recv()
|
2018-06-07 04:19:31 +00:00
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
self._closed: bool = False
|
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
def __repr__(self) -> str:
|
2019-03-12 02:46:49 +00:00
|
|
|
if self.msgstream:
|
2018-06-07 04:19:31 +00:00
|
|
|
return repr(
|
2019-12-10 05:55:03 +00:00
|
|
|
self.msgstream.stream.socket._sock).replace( # type: ignore
|
2018-06-07 04:19:31 +00:00
|
|
|
"socket.socket", "Channel")
|
|
|
|
return object.__repr__(self)
|
|
|
|
|
|
|
|
@property
|
2019-12-10 05:55:03 +00:00
|
|
|
def laddr(self) -> Optional[Tuple[Any, ...]]:
|
2019-03-12 02:46:49 +00:00
|
|
|
return self.msgstream.laddr if self.msgstream else None
|
2018-06-07 04:19:31 +00:00
|
|
|
|
|
|
|
@property
|
2019-12-10 05:55:03 +00:00
|
|
|
def raddr(self) -> Optional[Tuple[Any, ...]]:
|
2019-03-12 02:46:49 +00:00
|
|
|
return self.msgstream.raddr if self.msgstream else None
|
2018-06-07 04:19:31 +00:00
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
async def connect(
|
2021-06-24 22:49:51 +00:00
|
|
|
self,
|
|
|
|
destaddr: Tuple[Any, ...] = None,
|
2019-12-10 05:55:03 +00:00
|
|
|
**kwargs
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
) -> trio.SocketStream:
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-07-04 07:16:00 +00:00
|
|
|
if self.connected():
|
2018-06-07 04:19:31 +00:00
|
|
|
raise RuntimeError("channel is already connected?")
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-06-07 04:19:31 +00:00
|
|
|
destaddr = destaddr or self._destaddr
|
2019-12-10 05:55:03 +00:00
|
|
|
assert isinstance(destaddr, tuple)
|
2021-06-24 22:49:51 +00:00
|
|
|
|
|
|
|
stream = await trio.open_tcp_stream(
|
|
|
|
*destaddr,
|
|
|
|
**kwargs
|
|
|
|
)
|
2021-06-11 20:38:25 +00:00
|
|
|
self.msgstream = self.stream_serializer_type(stream)
|
2018-05-30 16:36:23 +00:00
|
|
|
return stream
|
|
|
|
|
2018-08-31 21:16:24 +00:00
|
|
|
async def send(self, item: Any) -> None:
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-08-31 21:16:24 +00:00
|
|
|
log.trace(f"send `{item}`") # type: ignore
|
2019-03-12 02:46:49 +00:00
|
|
|
assert self.msgstream
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2019-03-12 02:46:49 +00:00
|
|
|
await self.msgstream.send(item)
|
2018-05-30 16:36:23 +00:00
|
|
|
|
2018-08-31 21:16:24 +00:00
|
|
|
async def recv(self) -> Any:
|
2019-03-12 02:46:49 +00:00
|
|
|
assert self.msgstream
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-05-30 16:36:23 +00:00
|
|
|
try:
|
2019-03-12 02:46:49 +00:00
|
|
|
return await self.msgstream.recv()
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-11-09 06:53:15 +00:00
|
|
|
except trio.BrokenResourceError:
|
2018-05-30 16:36:23 +00:00
|
|
|
if self._autorecon:
|
|
|
|
await self._reconnect()
|
|
|
|
return await self.recv()
|
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
raise
|
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
async def aclose(self) -> None:
|
2018-07-04 07:16:00 +00:00
|
|
|
log.debug(f"Closing {self}")
|
2019-03-12 02:46:49 +00:00
|
|
|
assert self.msgstream
|
|
|
|
await self.msgstream.stream.aclose()
|
2021-06-24 22:49:51 +00:00
|
|
|
self._closed = True
|
|
|
|
log.error(f'CLOSING CHAN {self}')
|
2018-05-30 16:36:23 +00:00
|
|
|
|
|
|
|
async def __aenter__(self):
|
2018-06-07 04:19:31 +00:00
|
|
|
await self.connect()
|
2018-05-30 16:36:23 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
async def __aexit__(self, *args):
|
|
|
|
await self.aclose(*args)
|
|
|
|
|
2018-08-06 17:40:37 +00:00
|
|
|
def __aiter__(self):
|
|
|
|
return self._agen
|
2018-06-07 04:19:31 +00:00
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
async def _reconnect(self) -> None:
|
2018-05-30 16:36:23 +00:00
|
|
|
"""Handle connection failures by polling until a reconnect can be
|
|
|
|
established.
|
|
|
|
"""
|
|
|
|
down = False
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
with trio.move_on_after(3) as cancel_scope:
|
|
|
|
await self.connect()
|
|
|
|
cancelled = cancel_scope.cancelled_caught
|
|
|
|
if cancelled:
|
2018-09-10 19:19:49 +00:00
|
|
|
log.warning(
|
2018-05-30 16:36:23 +00:00
|
|
|
"Reconnect timed out after 3 seconds, retrying...")
|
|
|
|
continue
|
|
|
|
else:
|
2018-09-10 19:19:49 +00:00
|
|
|
log.warning("Stream connection re-established!")
|
2018-05-30 16:36:23 +00:00
|
|
|
# run any reconnection sequence
|
2018-06-07 04:19:31 +00:00
|
|
|
on_recon = self._recon_seq
|
|
|
|
if on_recon:
|
|
|
|
await on_recon(self)
|
2018-05-30 16:36:23 +00:00
|
|
|
break
|
|
|
|
except (OSError, ConnectionRefusedError):
|
|
|
|
if not down:
|
|
|
|
down = True
|
2018-09-10 19:19:49 +00:00
|
|
|
log.warning(
|
2018-06-07 04:19:31 +00:00
|
|
|
f"Connection to {self.raddr} went down, waiting"
|
2018-05-30 16:36:23 +00:00
|
|
|
" for re-establishment")
|
|
|
|
await trio.sleep(1)
|
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
async def _aiter_recv(
|
|
|
|
self
|
2018-08-31 21:16:24 +00:00
|
|
|
) -> typing.AsyncGenerator[Any, None]:
|
2018-05-30 16:36:23 +00:00
|
|
|
"""Async iterate items from underlying stream.
|
|
|
|
"""
|
2019-03-12 02:46:49 +00:00
|
|
|
assert self.msgstream
|
2018-05-30 16:36:23 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2019-03-12 02:46:49 +00:00
|
|
|
async for item in self.msgstream:
|
2018-05-30 16:36:23 +00:00
|
|
|
yield item
|
2018-06-07 04:19:31 +00:00
|
|
|
# sent = yield item
|
|
|
|
# if sent is not None:
|
|
|
|
# # optimization, passing None through all the
|
|
|
|
# # time is pointless
|
2019-03-12 02:46:49 +00:00
|
|
|
# await self.msgstream.send(sent)
|
2018-11-09 06:53:15 +00:00
|
|
|
except trio.BrokenResourceError:
|
2020-12-17 18:35:45 +00:00
|
|
|
|
2018-05-30 16:36:23 +00:00
|
|
|
if not self._autorecon:
|
|
|
|
raise
|
2020-12-17 18:35:45 +00:00
|
|
|
|
2018-07-04 07:16:00 +00:00
|
|
|
await self.aclose()
|
2020-12-17 18:35:45 +00:00
|
|
|
|
2018-05-30 16:36:23 +00:00
|
|
|
if self._autorecon: # attempt reconnect
|
|
|
|
await self._reconnect()
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return
|
2018-06-21 21:09:22 +00:00
|
|
|
|
2018-08-20 02:13:13 +00:00
|
|
|
def connected(self) -> bool:
|
2019-03-12 02:46:49 +00:00
|
|
|
return self.msgstream.connected() if self.msgstream else False
|
2018-07-11 22:08:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
2018-08-20 02:13:13 +00:00
|
|
|
async def _connect_chan(
|
|
|
|
host: str, port: int
|
2018-08-31 21:16:24 +00:00
|
|
|
) -> typing.AsyncGenerator[Channel, None]:
|
2018-08-20 02:13:13 +00:00
|
|
|
"""Create and connect a channel with disconnect on context manager
|
|
|
|
teardown.
|
2018-07-11 22:08:57 +00:00
|
|
|
"""
|
|
|
|
chan = Channel((host, port))
|
|
|
|
await chan.connect()
|
|
|
|
yield chan
|
|
|
|
await chan.aclose()
|