Skip to content

TQ: Add NodeCtx for use in Node API #8629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2025
Merged

Conversation

andrewjstone
Copy link
Contributor

Rather than passing in separate parameters to Node methods, we pass in a context shared with the caller. Importantly, due to the use of wrapper traits, the caller and methods are only able to mutate things that they each should be able to mutate.

This makes the function signatures shorter and also eliminates the case where we end up returning an Option<PersistentState>. This was getting tedious, not just here, but in other code not yet pushed. Since the caller already has to check if there are any outgoing messages to send after each api call, they can also check to see if the persistent state has changed. This isn't much of a burden and simplifies the Node internals somewhat. It also eliminates a clone. We can add other state to the context as needed over time.

Rather than passing in separate parameters to `Node` methods, we pass in
a context shared with the caller. Importantly, due to the use of wrapper
traits, the caller and methods are only able to mutate things that they
each should be able to mutate.

This makes the function signatures shorter and also eliminates the
case where we end up returning an `Option<PersistentState>`. This was
getting tedious, not just here, but in other code not yet pushed. Since
the caller already has to check if there are any outgoing messages to
send after each api call, they can also check to see if the persistent
state has changed. This isn't much of a burden and simplifies the `Node`
internals somewhat. It also eliminates a clone. We can add other state
to the context as needed over time.
@@ -147,7 +148,8 @@ impl CoordinatorState {
//
// This method is "in progress" - allow unused parameters for now
#[expect(unused)]
pub fn send_msgs(&mut self, now: Instant, outbox: &mut Vec<Envelope>) {
pub fn send_msgs(&mut self, ctx: &mut impl NodeHandlerCtx) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The NodeHandlerCtx trait is not dyn safe (object safe) due to the generic parameter on update_persistent_state. Therefore we use a trait impl parameter. This also avoids dynamic dispatch, potentially at the cost of monomorphization during compilation.

Copy link
Contributor

@jgallagher jgallagher left a comment

Choose a reason for hiding this comment

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

I haven't looked at much of the TQ code, but these changes all look reasonable to me. I'll approve but would feel better if you waited for a second opinion for someone with more context (unless you think this is all fine and just wanted a sanity check).

@andrewjstone
Copy link
Contributor Author

I haven't looked at much of the TQ code, but these changes all look reasonable to me. I'll approve but would feel better if you waited for a second opinion for someone with more context (unless you think this is all fine and just wanted a sanity check).

Thanks @jgallagher. Yes, this is barely related to the TQ protocol itself and I really did just want a sanity check. It's just to make the API a touch easier to use (and build).

@andrewjstone andrewjstone merged commit 565e59d into main Jul 18, 2025
16 checks passed
@andrewjstone andrewjstone deleted the tq-persistent-state-cleanup branch July 18, 2025 19:18
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.

2 participants