Lazy Pirate pattern with real request data

I am learning zmq with PyZMQ binding and I am having problems with Lazy Pirate. So, here is the code for the Lazy Pirate server and Lazy Pirate client .

In the example, the client sends a request, but this is a prime. How can I make a real request with text data and save the template implementation?

Also, I don’t fully understand the sequence variable in client code - it increases endlessly => will python fail when sequence reaches the maximum int variable?

+4
source share
2 answers

To get started, the sequence variable in the code is basically the message identifier. There are two ways in which a single request attempt can fail:

  • Request message simply cannot send
  • A request message is sent, but the client gets tired of waiting and the time runs out before the response returns to it.

In the second case, if you do not have a sequence number, you do not know which of your requests is actually the one that succeeded.

Consider this customer story:

  • Send Request # 1
  • Time-out
  • Send Request # 2
  • Time-out
  • Send Request # 3
  • Get an answer

Which request triggered the response? This can be any of the three requests due to the second type of request failure mentioned above. The sequence number in the response, we can know exactly which request is processed by the server.

The idea of ​​an “incorrect response from the server” in the client’s sample is that if the client is on request No. 3 (waiting time on requests No. 1 and No. 2), he discards answers #n (where n <3) until he receives a response # 3, which he accepts.

To send more than the serial number, use the serialization format and send the whole object.

For example, I could define a class MyRequest { int sequence; string text; } class MyRequest { int sequence; string text; } class MyRequest { int sequence; string text; } , and then send it as JSON to the server.

An infinitely increasing sequence variable could be replaced with int64, and then that would be nice, or you could do something like a GUID as an identified request.

+3
source

Unfortunately, Timothy Shields' answer above is 100% incorrect. You can read Hintjen's comments in this free version of his book, Connected Code Volume 1, for download . The important bit is given on page 141 as follows:

The client sequences each message and verifies that the replies are returned exactly in order: that no queries or replies are lost, and no replies are returned more than once, or out of order. Run the test several times until you are convinced that this mechanism really works. You do not need serial numbers in the production application; they just help us trust our design.

For an interesting discussion of the paths that may arise from open TCP, this is a good discussion . Read the comments. I have no doubt that Tim is very knowledgeable, he simply slipped, not realizing that the Lazy Pirate Pattern, in particular ONLY , uses REQ-REP, and therefore is more reliable than his comments.

+1
source

All Articles