Skip to content

Commit

Permalink
config: use Fx hook to close the quicreuse connection manager
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 2, 2023
1 parent a4b4e83 commit 8d52991
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
"github.com/quic-go/quic-go"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
)
Expand Down Expand Up @@ -255,7 +256,16 @@ func (cfg *Config) addTransports() ([]fx.Option, error) {
if cfg.QUICReuse != nil {
fxopts = append(fxopts, cfg.QUICReuse...)
} else {
fxopts = append(fxopts, fx.Provide(quicreuse.NewConnManager)) // TODO: close the ConnManager when shutting down the node
fxopts = append(fxopts,
fx.Provide(func(key quic.StatelessResetKey, _ *swarm.Swarm, lifecycle fx.Lifecycle) (*quicreuse.ConnManager, error) {
cm, err := quicreuse.NewConnManager(key)
if err != nil {
return nil, err
}
lifecycle.Append(fx.StopHook(cm.Close))
return cm, nil
}),
)
}

fxopts = append(fxopts, fx.Invoke(
Expand Down Expand Up @@ -328,7 +338,10 @@ func (cfg *Config) NewNode() (host.Host, error) {
lifecycle.Append(fx.StopHook(sw.Close))
return sw, nil
}),
fx.Decorate(func(sw *swarm.Swarm, lifecycle fx.Lifecycle) *swarm.Swarm {
// Make sure the swarm constructor depends on the quicreuse.ConnManager.
// That way, the ConnManager will be started before the swarm, and more importantly,
// the swarm will be stopped before the ConnManager.
fx.Decorate(func(sw *swarm.Swarm, _ *quicreuse.ConnManager, lifecycle fx.Lifecycle) *swarm.Swarm {
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
// TODO: This method succeeds if listening on one address succeeds. We
Expand Down

0 comments on commit 8d52991

Please sign in to comment.