Source code for mstar.communication.codec

"""The encode/decode seam of the ZMQ control mesh, mirroring the Rust
``Codec`` trait::

    pub trait Codec<M> {
        fn encode(msg: &M) -> Result<Vec<u8>, CommError>;
        fn decode(bytes: &[u8]) -> Option<M>;
    }

The transport never looks inside the bytes, so moving an edge to another
encoding is a codec change, never a transport change.
"""
import logging
import os
from collections.abc import Iterable

logger = logging.getLogger(__name__)


[docs] class Codec:
[docs] @staticmethod def encode(msg) -> bytes: raise NotImplementedError
[docs] @staticmethod def decode(data: bytes): raise NotImplementedError
[docs] class MsgpackCodec(Codec): """Raw msgpack, no schema: for edges whose payloads are already plain dicts and whose other end is not Python — the Rust ``mstar-server`` frontend bridge. ``default=str`` is lossy, so this is NOT a general replacement for pickle; use :class:`WireCodec` for message dataclasses. """
[docs] @staticmethod def encode(msg) -> bytes: import msgpack return msgpack.packb(msg, default=str)
[docs] @staticmethod def decode(data: bytes): import msgpack return msgpack.unpackb(data, raw=False)
[docs] class WireCodec(Codec): """Typed msgpack (``mstar.communication.wire``): language-neutral and lossless for the message dataclasses. The default."""
[docs] @staticmethod def encode(msg) -> bytes: from mstar.communication import wire return wire.encode(msg)
[docs] @staticmethod def decode(data: bytes): from mstar.communication import wire return wire.decode(data)
[docs] def decode_each(codec: type[Codec], frames: Iterable[bytes], who: str) -> list: """Decode a drained batch one frame at a time. A frame that fails to decode is logged and skipped. Raising instead would lose every frame already drained with it -- control messages for other requests included -- to one bad payload. """ out = [] for frame in frames: try: out.append(codec.decode(frame)) except Exception: logger.exception( "%s: dropping a %d-byte frame that failed to decode", who, len(frame), ) return out
[docs] def default_codec() -> type[Codec]: """Select typed msgpack; reject the removed pickle codec at startup. Both ends of an edge must agree, so this is all-or-nothing for a mesh — a single process set differently will not be able to talk to its peers. """ choice = os.getenv("MSTAR_WIRE_CODEC", "msgpack").lower() if choice != "msgpack": raise ValueError(f"MSTAR_WIRE_CODEC must be msgpack; got {choice!r}") import mstar.communication.wire_types # noqa: F401 (registers the tags) return WireCodec