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

host: use dependency injection (Fx) to construct services, and start and stop them #1993

Open
marten-seemann opened this issue Jan 12, 2023 · 1 comment
Labels
effort/days Estimated to take multiple days, but less than a week exp/intermediate Prior experience is likely helpful kind/enhancement A net-new feature or improvement to an existing feature

Comments

@marten-seemann
Copy link
Contributor

Now that we’ve started using Fx to construct our transports, muxers and security protocols, we should start using it to construct libp2p services like AutoNAT, Identify, the hole punching service etc.

Constructing, Starting and Stopping Services

These services might depend on each other. For example, the hole puncher needs the identity service. Currently, these interdependencies are hardcoded (and the host keeps references to particular services), but we could just have Fx take care of resolving this dependency tree.

We should also clearly define what a libp2p service is:

type Service interface {
     Start() error
     io.Closer
}

In general, service should not start any Go routines in their constructor, but wait for the explicit start signal provided with the Start method.

The host would then just have to keep track of a slice of services, so it can cleanly shut them down. Starting and stopping the services should also be done by Fx, so services are started in the right order. Maybe we won’t even need the slice of services, assuming that Fx keeps track the dependency tree for shutdown.

How to expose existing services to the Application

An application might want to be able to interact with particular services, once they’re constructed. We could add getter functions to the host, but this quickly gets messy, and doesn’t work with injected services at all.

Instead we could have users pass in a service struct that Fx would fill with concrete values, e.g.:

libp2p.FillHost(struct { *identify.Service, *autonat.Service }) (host.Host, error)
@MarcoPolo
Copy link
Collaborator

I was in this code recently and was reminded of this issue. I think it's still a good idea 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/days Estimated to take multiple days, but less than a week exp/intermediate Prior experience is likely helpful kind/enhancement A net-new feature or improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants