Repository navigation
Create new API for the crate - #17
Conversation
| /// | ||
| /// All clients are required to send this information on startup. If the | ||
| /// client does not receive this information quickly enough, the service | ||
| /// will be killed. |
There was a problem hiding this comment.
Since this is the case, wouldn't it be better to enforce this at the type system level? For instance from_env() could return a non-complete UnstartedComms, which you would then turn into a Comms by calling pub fn startup(self, version: &str) -> std::io::Result<Comms>.
It's not a big deal, but since we're changing the API, we might as well get this type of thing right now.
There was a problem hiding this comment.
I thought about moving startup sending to be part of the initialization but then disregarded it as I wanted callers to be free to send it whenever the feel like is the right moment.
I like the proposal of UnstartedComms! I will create a separate PR for this though (the current API is quite similar to the old one but with provenance tracking through an instance so I think it is easier to review).
| /// | ||
| /// println!("Hello, {name}!"); | ||
| /// ``` | ||
| pub fn try_receive_with_heartbeat(&self, rate: Duration) -> std::io::Result<Option<Message>> { |
There was a problem hiding this comment.
Now that I'm looking at this again, I find that starting a new background thread at each received message is a bit much in terms of overhead (though I get that the scoped thread keeps the code nice and simple and cross-platform).
I'm thinking, since we have platform-specific impls of CommsRaw, could we add a function like read_with_timeout that uses a low-level syscall like select or poll (or windows equivalent) that accepts a timeout?
Just food for thought, won't block this PR for this.
There was a problem hiding this comment.
I created #18 to track that. Overall I think it might be not worth the complexity given that the only user (RRG) expects about a message per day, but who knows, maybe the implementation is actually as trivial as the current one.
| // We keep hearbeating until the sender disconnects (in | ||
| // which case the receiver will receive a disconnection | ||
| // error). | ||
| match receiver.recv_timeout(rate) { |
There was a problem hiding this comment.
Not sure if this can actually happen IRL but the fact that we use a duration rather than a set timeout time means that if we continuously receive messages faster than once a second, we'd never send a heartbeat back. Is this intended?
There was a problem hiding this comment.
Is this intended?
In a sense. Service is still expected to do its own heartbeating (the whole idea is to kill processes that are "stuck"), this function should only save you when you intentionally block on receiving a message (as naturally you cannot hearbeat during that but that does not mean you are stuck).
I added a paragraph about that to the method documentation.
|
(I do not have time to review this) |
This is another stab at #4, hopefully this time it fixes it once and forall. Which it should as it is more-or-less inline with @Manishearth's #4 (comment):
I also played with the API mirroring stdlib's
std::sync::*::channelorstd::io::pipe(with two ends: one for sending and one for receiving). Unfortunately, such approach does not play nicely with our hearbeat requirements where most of the time we actually want the recipient end also to send messages. So, a singleCommsit is (with a separateReceiveriterator that can be created out of it).The new API is also panic-free (except for lock poisoning) and always returns
std::io::Result. While terminating the process in case of connection error is still a good idea, now I think it should still be a decision of the library user when and how exactly to do that.