ZMQ study
ZeroMQ high-water mark (HWM) queues
These are the socket combinations that are valid for a connect-bind pair (either side can bind):
PUB and SUB
REQ and REP
REQ and ROUTER (take care, REQ inserts an extra null frame)
DEALER and REP (take care, REP assumes a null frame)
DEALER and ROUTER
DEALER and DEALER
ROUTER and ROUTER
PUSH and PULL
PAIR and PAIR
ZMQ_POLLIN
对于ZMQ socket来说,至少有一个消息在这个socket上被非阻塞的接收到了。对于标准socket来说,此标志和系统调用的poll()函数的POLLIN标志等价,而且通常意味着,fd文件描述符上至少非阻塞的接收到了1B的数据。
zmq_inproc – ØMQ 本地进程内(线程间)传输方式
zmq_proxy
Socket types
Thread safety: ZeroMQ的socket是非线程安全的,并且ZeroMQ本身不建议在多个线程中传递同一个Socket,即使保证了线程同步。
Socket types: ZeroMQ一共具有12种类型的socket,5种消息模式。
- 请求/应答模式:ZMQ_REQ、ZMQ_REP、ZMQ_DEALER、ZMQ_ROUTER
- 发布/订阅模式: ZMQ_PUB, ZMQ_SUB、ZMQ_XPUB、ZMQ_XSUB
- 管道模式:ZMQ_PUSH、ZMQ_PULL
- 配对模式:ZMQ_PAIR
- 本地模式:ZMQ_STREAM
Request-Reply 请求/应答模式
Here are some tips for remembering the semantics. DEALER is like an asynchronous REQ socket, and ROUTER is like an asynchronous REP socket. Where we use a REQ socket, we can use a DEALER; we just have to read and write the envelope ourselves. Where we use a REP socket, we can stick a ROUTER; we just need to manage the identities ourselves.
Think of REQ and DEALER sockets as “clients” and REP and ROUTER sockets as “servers”. Mostly, you’ll want to bind REP and ROUTER sockets, and connect REQ and DEALER sockets to them. It’s not always going to be this simple, but it is a clean and memorable place to start.
ZMQ_ROUTER
A socket of type ZMQ_ROUTER is an advanced pattern used for extending request/reply sockets.
ZMQ_DEALER
DEALER是一种用于请求/答应模式的更高级的扩展Socket,它可以自由的收发消息,没有ZMQ_REP/ZMQ_REQ那样的限制。
对于每一个连接,接收消息也是使用了公平队列,发送使用了循环队列(RR)。
ZMQ_DEALER受ZMQ_RCVHW和ZMQ_SNDHW两个阀值影响(可通过zmq_setsockopt函数设置),一旦发送或接收队列达到阀值,Socket就会进入mute状态,
此时对DEALER的任何zmq_send操作都会阻塞,直到mute状态结束。
如果当前没有有效的连接,zmq_send操作也会阻塞,直到有新的连接到来为止。
DEALER发生阻塞并不会丢弃消息。
注意:如果ZMQ_DEALER连接到ZMQ_REP,每一个消息包必须包含一个空帧,然后再紧跟着数据包体。
特点总结:
可兼容的Socket types: ZMQ_ROUTER, ZMQ_REP, ZMQ_DEALER
数据传输: 双向
发送/接收模式: 无限制
发送路由策略: Round-robin(循环队列)
接收路由策略: Fair-queued(公平队列)
进入mute状态后: 阻塞
PUB/SUB 发布/订阅模式
- ZMQ_PUB
ZMQ_PUB类型的Socket以发布者的身份向订阅者分发消息,消息以扇出的形式发送给所有订阅者连接。
ZMQ_PUB类型的Socket没有实现zmq_recv函数,所以不能对其调用zmq_recv函数!
当ZMQ_PUB Socket达到阀值时进入mute状态,此时后续发送的消息会被丢弃,直到mute状态结束。
对ZMQ_PUB Socket调用zmq_send永远不会发生阻塞。
特点总结:
可兼容的Socket types: ZMQ_SUB, ZMQ_XSUB
数据传输: 单向
发送/接收模式: 只能发送
接收路由策略: Fan out(扇出)
进入mute状态后: 丢弃消息
ZMQ_SUB
ZMQ_SUB类型的Socket以订阅者的身份接收消息。初始的ZMQ_SUB Socket没有订阅任何消息,可以通过设置ZMQ_SUBSRIBE选项来指定需要订阅的消息。
ZMQ_SUB Socket没有实现zmq_send函数,所以不能对其调用zmq_send函数!
特点总结:
- 可兼容的Socket types: ZMQ_PUB, ZMQ_XPUB
- 数据传输: 单向
- 发送/接收模式: 只能接收
- 接收路由策略: Fair-queued(公平队列)
管道模式 ZMQ_PUSH ZMQ_PULL
Fair Queuing
是单向的,即PUSH端发送消息,PULL端接收消息