ZeroMQ:多对一无回复aynsc消息

ZeroMQ: Many-to-one no-reply aynsc messages

我已经通读了 zguide,但还没有找到我要找的那种模式:

  1. 有一台中央服务器(具有已知端点)和许多客户端(可能来来去去)。
  2. 客户端不断向服务器发送心跳,但不希望服务器回复。
  3. 服务器收到心跳,但不回复客户端。
  4. 客户端和服务器断开连接时发送的心跳应该以某种方式被丢弃,以防止它们重新联机时出现心跳泛滥。

我能想到的壁橱是 DEALER-ROUTER 模式,但由于这是用作异步 REQ-REP 模式(不是吗?),我不确定如果服务器只是对传入保持沉默 "requests." 此外,当达到发送高水位线时,DEALER 套接字将阻止而不是开始丢弃心跳,这仍然会导致心跳泛滥。

PUSH/PULL 模式应该能满足您的需求。

# Client example
import zmq

class Client(object):
    def __init__(self, client_id):
        self.client_id = client_id
        ctx = zmq.Context.instance()
        self.socket = ctx.socket(zmq.PUSH)
        self.socket.connect("tcp://localhost:12345")

    def send_heartbeat(self):
        self.socket.send(str(self.client_id))


# Server example
import zmq

class Server(object):
    def __init__(self):
        ctx = zmq.Context.instance()
        self.socket = ctx.socket(zmq.PULL)
        self.socket.bind("tcp://*:12345")  # close quote

    def receive_heartbeat(self):
        return self.socket.recv() # returns the client_id of the message's sender

此 PUSH/PULL 模式可根据需要与多个客户端一起使用。服务器应该管理接收到的消息(即像 {client_id : last_received} 这样的字典,它在每条接收到的消息上用 datetime.utcnow() 更新。并实现一些内务处理功能以定期检查具有旧时间戳的客户端的管理。