host: use dependency injection (Fx) to construct services, and start and stop them
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 6.9k
- Forks
- 1.3k
- Avg merge
- 13d 21h
- Merged PRs (30d)
- 1
Description
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:
```go
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.:
```go
libp2p.FillHost(struct { *identify.Service, *autonat.Service }) (host.Host, error)
```
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the host's current construction and lifecycle handling for services such as AutoNAT, Identify, and hole punching. Define the Service interface and determine how Fx should resolve dependencies, start services in order, stop them, and expose selected services to applications; done means these concerns no longer rely on hardcoded host references.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- networking
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100