user3554230
user3554230

Reputation: 283

ZeroMQ knows which go routine a request came from

I am trying to figure out if i have two routines calling the same zeromq socket if when the return from socket.recv() on routine1 returns.. the socket will know to return to routine1 (ditto for routine2, ... routinen).

Hence... using a messaging bus with request/response semantics, what is the best pattern to approach this in go?

If it helps.. think about the simple pirate pattern and implementing an RPC style call set in that pattern with zeromq.

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()

so in this case i have no idea if the response from routine1 or routine2 will come back first. How can i make sure that when i get a response to socket... i can notify the correct routines' recv() function.

Thanks!

Upvotes: 4

Views: 268

Answers (1)

David Budworth
David Budworth

Reputation: 11626

I don't believe a socket can be shared, concurrently, between threads using zeromq

Likely you need to do some kind of goroutine that listens for requests, sends / receives zmq, and then replies.

for example: (untested code)

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() 
    }
}

Upvotes: 2

Related Questions