forked from goodboy/tractor
1
0
Fork 0
tractor/tractor/_ipc.py

243 lines
7.5 KiB
Python
Raw Normal View History

2018-05-30 16:36:23 +00:00
"""
Inter-process comms abstractions
"""
import typing
2018-08-31 21:16:24 +00:00
from typing import Any, Tuple, Optional
from functools import partial
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
log = get_logger('ipc')
2018-05-30 16:36:23 +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
class MsgpackStream:
"""A ``trio.SocketStream`` delivering ``msgpack`` formatted data.
2018-05-30 16:36:23 +00:00
"""
2018-08-31 21:16:24 +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
# should both be IP sockets
lsockname = stream.socket.getsockname()
assert isinstance(lsockname, tuple)
self._laddr = lsockname[:2]
rsockname = stream.socket.getpeername()
assert isinstance(rsockname, tuple)
self._raddr = rsockname[:2]
2018-05-30 16:36:23 +00:00
self._agen = self._iter_packets()
2018-12-15 07:20:19 +00:00
self._send_lock = trio.StrictFIFOLock()
2018-05-30 16:36:23 +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.
"""
unpacker = Unpacker(
raw=False,
use_list=False,
)
2018-05-30 16:36:23 +00:00
while True:
try:
data = await self.stream.receive_some(2**10)
2018-08-31 21:16:24 +00:00
log.trace(f"received {data}") # type: ignore
2018-11-09 06:53:15 +00:00
except trio.BrokenResourceError:
log.warning(f"Stream connection {self.raddr} broke")
2018-05-30 16:36:23 +00:00
return
if data == b'':
log.debug(f"Stream connection {self.raddr} was closed")
2018-05-30 16:36:23 +00:00
return
unpacker.feed(data)
for packet in unpacker:
yield packet
@property
2019-12-10 05:55:03 +00:00
def laddr(self) -> Tuple[Any, ...]:
return self._laddr
@property
2019-12-10 05:55:03 +00:00
def raddr(self) -> Tuple[Any, ...]:
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:
async with self._send_lock:
return await self.stream.send_all(
msgpack.dumps(data, use_bin_type=True))
2018-05-30 16:36:23 +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
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
class Channel:
"""An inter-process channel for communication between (remote) actors.
2018-05-30 16:36:23 +00:00
Currently the only supported transport is a ``trio.SocketStream``.
2018-05-30 16:36:23 +00:00
"""
def __init__(
self,
2018-08-31 21:16:24 +00:00
destaddr: Optional[Tuple[str, int]] = None,
on_reconnect: typing.Callable[..., typing.Awaitable] = None,
auto_reconnect: bool = False,
stream: trio.SocketStream = None, # expected to be active
) -> None:
2018-05-30 16:36:23 +00:00
self._recon_seq = on_reconnect
self._autorecon = auto_reconnect
self.msgstream: Optional[MsgpackStream] = MsgpackStream(
2018-08-31 21:16:24 +00:00
stream) if stream else None
if self.msgstream and destaddr:
raise ValueError(
f"A stream was provided with local addr {self.laddr}"
)
self._destaddr = self.msgstream.raddr if self.msgstream else destaddr
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
# set if far end actor errors internally
self._exc: Optional[Exception] = None
self._agen = self._aiter_recv()
def __repr__(self) -> str:
if self.msgstream:
return repr(
2019-12-10 05:55:03 +00:00
self.msgstream.stream.socket._sock).replace( # type: ignore
"socket.socket", "Channel")
return object.__repr__(self)
@property
2019-12-10 05:55:03 +00:00
def laddr(self) -> Optional[Tuple[Any, ...]]:
return self.msgstream.laddr if self.msgstream else None
@property
2019-12-10 05:55:03 +00:00
def raddr(self) -> Optional[Tuple[Any, ...]]:
return self.msgstream.raddr if self.msgstream else None
async def connect(
2019-12-10 05:55:03 +00:00
self, destaddr: Tuple[Any, ...] = None,
**kwargs
) -> trio.SocketStream:
2018-07-04 07:16:00 +00:00
if self.connected():
raise RuntimeError("channel is already connected?")
destaddr = destaddr or self._destaddr
2019-12-10 05:55:03 +00:00
assert isinstance(destaddr, tuple)
stream = await trio.open_tcp_stream(*destaddr, **kwargs)
self.msgstream = MsgpackStream(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:
log.trace(f"send `{item}`") # type: ignore
assert self.msgstream
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:
assert self.msgstream
2018-05-30 16:36:23 +00:00
try:
return await self.msgstream.recv()
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()
async def aclose(self) -> None:
2018-07-04 07:16:00 +00:00
log.debug(f"Closing {self}")
assert self.msgstream
await self.msgstream.stream.aclose()
2018-05-30 16:36:23 +00:00
async def __aenter__(self):
await self.connect()
2018-05-30 16:36:23 +00:00
return self
async def __aexit__(self, *args):
await self.aclose(*args)
def __aiter__(self):
return self._agen
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
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(
f"Connection to {self.raddr} went down, waiting"
2018-05-30 16:36:23 +00:00
" for re-establishment")
await trio.sleep(1)
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.
"""
assert self.msgstream
2018-05-30 16:36:23 +00:00
while True:
try:
async for item in self.msgstream:
2018-05-30 16:36:23 +00:00
yield item
# sent = yield item
# if sent is not None:
# # optimization, passing None through all the
# # time is pointless
# await self.msgstream.send(sent)
2018-11-09 06:53:15 +00:00
except trio.BrokenResourceError:
2018-05-30 16:36:23 +00:00
if not self._autorecon:
raise
2018-07-04 07:16:00 +00:00
await self.aclose()
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
def connected(self) -> bool:
return self.msgstream.connected() if self.msgstream else False
2018-07-11 22:08:57 +00:00
@asynccontextmanager
async def _connect_chan(
host: str, port: int
2018-08-31 21:16:24 +00:00
) -> typing.AsyncGenerator[Channel, None]:
"""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()