-
Notifications
You must be signed in to change notification settings - Fork 254
feat(iroh): add new protocols to router at runtime #3355
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
base: main
Are you sure you want to change the base?
Conversation
Documentation for this PR has been generated and is available at: https://n0-computer.github.io/iroh/pr/3355/docs/iroh/ Last updated: 2025-06-26T11:14:04Z |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
largely looks good to me, nice to have this!
iroh/src/protocol.rs
Outdated
#[derive(Debug, Snafu)] | ||
#[non_exhaustive] | ||
pub enum RouterError { | ||
#[snafu(display("The router actor closed"))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"router actor" is something rather internal, no? Can we not say "the router closed" or maybe even "the endpoint closed"?
iroh/src/protocol.rs
Outdated
/// Configures the router to accept the [`ProtocolHandler`] when receiving a connection | ||
/// with this `alpn`. | ||
/// | ||
/// Once the function yields, new connections with this `alpn` will be handled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"yields" or returns? As far as I read the code it should be "returns"?
iroh/src/protocol.rs
Outdated
/// | ||
/// Once the function yields, new connections with this `alpn` will be handled. | ||
/// | ||
/// If a protocol handler was already registered for `alpn`, the previous handler will be shutdown. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate more on what happens to existing connections? Will they all be closed by the old handler? How does that work? If you're repeating how handlers shut down connections that's fine, docs should repeat things.
iroh/src/protocol.rs
Outdated
/// Stops accepting a protocol. | ||
/// | ||
/// Note that this has only an effect on new connections. Existing connections that were | ||
/// accepted with `alpn` won't be closed when calling [`Router::stop_accepting`]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you call out the differences in behaviour with calling accept
where a previous handler was already handling this protocol?
@flub Thanks for the review, clarified the docs. LMK if there's still something unclear from reading the docs. |
unsure if I should post here or open an issue. is there some way that we can allow end-users to have a Use case: facilitating third-party routers, or facilitating storing protocol handlers in structs, such as for the purposes of configuration: #[derive(Debug, bon::Builder)]
pub struct Agent {
#[builder(field)]
handlers: HashMap<Alpn, Box<dyn ProtocolHandler>>,
/// Provide a specific secret key for mTLS, instead of dynamically generating one
secret_key: Option<iroh::SecretKey>,
#[builder(default)]
discovery: EnabledDiscoveryServices,
/// in QUIC, the ALPN allows for multiple connections on same port, and it is ideal
/// to circumvent firewalls to always use port 443 (same as https). You can also set
/// this to 0 to dynamically choose a port.
#[builder(default = 443)]
port: u16,
}
impl<S: agent_builder::State> AgentBuilder<S> {
fn router(
mut self,
alpn: &'static str,
router: impl Into<Box<dyn ProtocolHandler>>,
) -> Self {
self.handlers.insert(Alpn(alpn), router.into());
self
}
}
//...
let mut router = iroh::protocol::Router::builder(endpoint);
for (alpn, handler) in self.handlers {
router = router.accept(alpn.0, handler);
} |
2aa24ec
to
9f01dd5
Compare
That's valid, IMO. I opened #3366. |
Description
This allows adding new protocols and removing protocols to an iroh
Router
after the router is built.Also adds a timeout to shutdown for a protocol handler of 30s.
Also wrote a test for this.
Breaking Changes
Notes & open questions
Change checklist