ZeroMQ 知道请求来自哪个 go routine

ZeroMQ knows which go routine a request came from

我想弄清楚我是否有两个例程调用同一个 zeromq 套接字,如果当例程 1 returns 上的 socket.recv() 中的 return.. 套接字将知道 return 到 routine1(同上 routine2,... routinen)。

因此...使用具有 request/response 语义的消息传递总线,在 go 中处理此问题的最佳模式是什么?

如果有帮助.. 考虑简单的盗版模式并使用 zeromq 在该模式中实现 RPC 样式调用集。

Socket; //zeromq socket
//routine 1
socket.send(data) // do i need some identifier here for this routine?
socket.recv() // wait until i get a response from that send
//routine 2 
socket.send(data)
socket.recv()

所以在这种情况下,我不知道例程 1 或例程 2 的响应是否会先返回。我如何确保当我收到对套接字的响应时...我可以通知正确的例程的 recv() 函数。

谢谢!

我不相信可以使用 zeromq 在线程之间同时共享套接字

您可能需要执行某种 goroutine 来侦听请求、发送/接收 zmq,然后回复。

例如:(未经测试的代码)

type Req struct {
   Data []byte 
   Reply chan []byte
}

go func() { // probably not the zmq api, but you should get the idea here
    for req := <- requests {
         socket.send(req.Data)
         req.Reply <- socket.recv() 
    }
}