rust-tcpstream-demo
rust-tcpstream-demo copied to clipboard
Couple of questions on Protocol
Hi again, 2 questions about the protocol part:
- Why does the length of the thing we are going to write to the buffer is
u16? - If you would have wanted to add a retrying mechanism to keep writing for
ntimes to the buffer if there's an error (e.g. in the real world - server downtime) - how would have you done it?
It's interesting, if you give a very large number to jumble there is an error and not a buffer overflow, is that a feature of Rust as well here or am I missing something?
Great tutorial, learned a lot! Thank you.
-
Is this regarding the
amountfield?. Assuming yes, that's just an arbitrary integer size I picked. It likely could have been a u8, but I wanted to make sure that the example was writing more than a single byte for that field. -
For this specific case of writing to the buffer, it's mostly likely that an error would not be recoverable with a retry. It would most likely be due to running out of memory locally or some memory allocation error that wouldn't be worth trying to work around.
However, the stream.flush() operation could be retryable as a full queue might eventually get more space. In this case (or any general case where you're wanting to retry failed operations), I would look to one of the following options:
- Use a retry library specifically for this use case.
- There can be many edge cases around retrying and work to make retrying ergonomic, best to use a battle-tested library for this purpose.
- Use something like
retryfor sync code ortokio_retryfor async code.
- If you really want to do your own retry functionality, you can write your own and even use macros to make it more ergonomic: retry macro example
Great questions, keep them coming! :)