Skip to content

Create new API for the crate - #17

Merged
panhania merged 23 commits into
google:masterfrom
panhania:feature/channel-api
Sep 30, 2026
Merged

panhania merged 23 commits into
google:masterfrom
panhania:feature/channel-api

Conversation

@panhania

@panhania panhania commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

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

Another one would be to have it not be a lazy_static and pass it down via arguments; so you can ensure its provenance, and then declare that binaries using this need to basically declare that they are unsafe when called in ways other than as a child process.

I also played with the API mirroring stdlib's std::sync::*::channel or std::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 single Comms it is (with a separate Receiver iterator 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.

@panhania
panhania requested a review from CRefice September 24, 2026 10:19
///
/// 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Manishearth

Copy link
Copy Markdown
Collaborator

(I do not have time to review this)

@panhania
panhania removed the request for review from Manishearth September 29, 2026 15:16
@panhania
panhania merged commit 0fcdb81 into google:master Sep 30, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants