Crate threadsafe_zmq

Crate threadsafe_zmq 

Source
Expand description

§Thread-Safe ZeroMQ

A thread-safe wrapper around ZeroMQ sockets providing safe concurrent access through crossbeam channels, with optional async support via Tokio.

§The Problem

ZeroMQ sockets are explicitly NOT thread-safe. The ZMQ guide states: “Do not use or close sockets except in the thread that created them.” This is a fundamental design choice in libzmq for performance reasons.

However, real-world applications often need to send/receive from multiple threads. This library solves that by isolating all socket operations to dedicated background threads and exposing thread-safe channels to user code.

§Architecture

The key insight is that while we can’t share ZMQ sockets across threads, we CAN share channels. So we:

  1. Dedicate a thread to own and operate the ZMQ socket
  2. Use crossbeam channels to communicate between user threads and that socket thread
  3. Use internal ZMQ PAIR sockets to signal the socket thread (because zmq_poll can’t wait on channels, only sockets)

This gives us O(1) message passing with minimal synchronization overhead.

Structs§

AsyncChannelPair
AsyncChannelPairBuilder
ChannelPair
Thread-safe wrapper around a ZeroMQ socket.
ChannelPairBuilder
Builder for ChannelPair with fluent configuration API.
Receiver
Cloneable handle for receiving messages.
Sender
Cloneable handle for sending messages.

Enums§

AsyncChannelCapacity
ChannelCapacity
Channel capacity configuration.
ChannelPairError

Type Aliases§

ZmqMessage
A multipart ZeroMQ message represented as a vector of byte frames.