Skip to content
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

Very WIP Experiment: don't require Box in protocol handler accept #3020

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rklaehn
Copy link
Contributor

@rklaehn rklaehn commented Dec 9, 2024

Description

Add an UnboxedProtocolHandler trait and try it out in the examples.

Breaking Changes

The blanket impls for Box and Arc conflict with this, so I had to comment them out

Notes & open questions

Is this worth the effort in simplifying writing protocols?
Should ProtocolHandler always be like this, and we do the transformation to something boxable internally?

Maybe the following would be good:

  • rename UnboxedProtocolHandler to just ProtocolHandler (or Protocol if we want to go this way)
  • keep ProtocolHandler as BoxedProtocolHandler but hide it away a bit

That way by default people get the simple interface, but if somebody has special requirements such as

  • they have an already boxed future and want to avoid boxing it again
  • they don't want owned self because they want to clone not all of self but only a part
    they can still do it by implementing BoxedProtocolHandler manually. All ProtocolHandler impls get a default BoxedProtocolHandler impl.

Change checklist

  • Self-review.
  • Documentation updates following the style guide, if relevant.
  • Tests if relevant.
  • All breaking changes documented.

Add an UnboxedProtocolHandler trait and try it out in the examples.
Copy link

github-actions bot commented Dec 9, 2024

Documentation for this PR has been generated and is available at: https://n0-computer.github.io/iroh/pr/3020/docs/iroh/

Last updated: 2024-12-09T09:03:27Z

Copy link

github-actions bot commented Dec 9, 2024

Netsim report & logs for this PR have been generated and is available at: LOGS
This report will remain available for 3 days.

Last updated for commit: 1883e22

@rklaehn
Copy link
Contributor Author

rklaehn commented Dec 9, 2024

Ok, let's see how our handlers are implemented:

Gossip

impl ProtocolHandler for Gossip {
    fn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>> {
        let this = self.clone();
        Box::pin(async move { this.handle_connection(conn.await?).await })
    }
}

Blobs

impl<S: crate::store::Store> ProtocolHandler for Blobs<S> {
    fn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>> {
        let db = self.store().clone();
        let events = self.events().clone();
        let rt = self.rt().clone();

        Box::pin(async move {
            crate::provider::handle_connection(conn.await?, db, events, rt).await;
            Ok(())
        })
    }

    fn shutdown(&self) -> BoxedFuture<()> {
        let store = self.store().clone();
        Box::pin(async move {
            store.shutdown().await;
        })
    }
}

Docs

impl<S: iroh_blobs::store::Store> ProtocolHandler for Docs<S> {
    fn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>> {
        let this = self.engine.clone();
        Box::pin(async move { this.handle_connection(conn).await })
    }

    fn shutdown(&self) -> BoxedFuture<()> {
        let this = self.engine.clone();
        Box::pin(async move {
            if let Err(err) = this.shutdown().await {
                tracing::warn!("shutdown error: {:?}", err);
            }
        })
    }
}

Gossip clones self, but arguably it should only clone inner.

So while in all examples we clone self, in all actual impls we try to clone as little as possible to move it into the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🏗 In progress
Development

Successfully merging this pull request may close these issues.

1 participant