Lazy Pirate pattern with real request data
python, zeromq
Solution
To start, the `sequence` variable in the code is basically a message identifier. There are two ways in which a single request attempt can fail:
- The request message can simply never send
- The request message sends, but the client gets tired of waiting and times out before the response gets back to it
In the second case, if you don't have the `sequence` number, you don't know which of your requests is actually the one that succeeded.
Consider this client history:
- Send request #1
- Timeout
- Send request #2
- Timeout
- Send request #3
- Receive response
Which request trigged the response? It could be any of the three requests, because of the second kind of request failure mentioned above. With the `sequence` number in the response, we can know exactly which request is the one the server processed.
The idea behind the "Malformed reply from server" in the sample client is that, if the client is on request #3 (having timed out on requests #1 and #2), it throws away responses #n (where n < 3) until it gets to response #3, which it accepts.
To send more than the sequence number, use a serialization format and send a whole object.
For example, I could define `class MyRequest { int sequence; string text; }` and then send that as JSON to the server.
The infinitely incrementing `sequence` variable could be replaced with an int64 and then it would be fine, or you could do something like use a GUID as the request identified instead.
Problem
I am learning zmq with PyZMQ binding and i have trouble with Lazy Pirate pattern. So, here is a code for Lazy Pirate server and Lazy Pirate client. In example client sends request, but it is simple number. How can i make a real request with text data and keep pattern realization? Also, i don't trully understand `sequence` variable in client code - it is incrementing infinitely => won't python crash when `sequence` reaches int variable maximum?