-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
RFC: Invert DI Constructor Config Handling #6232
Comments
For example, we'd replace options like: func Pubsub(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, cfg *config.Config) (service *pubsub.PubSub, err error) {
var pubsubOptions []pubsub.Option
if cfg.Pubsub.DisableSigning {
pubsubOptions = append(pubsubOptions, pubsub.WithMessageSigning(false))
}
if cfg.Pubsub.StrictSignatureVerification {
pubsubOptions = append(pubsubOptions, pubsub.WithStrictSignatureVerification(true))
}
switch cfg.Pubsub.Router {
case "":
fallthrough
case "floodsub":
service, err = pubsub.NewFloodSub(lifecycleCtx(mctx, lc), host, pubsubOptions...)
case "gossipsub":
service, err = pubsub.NewGossipSub(lifecycleCtx(mctx, lc), host, pubsubOptions...)
default:
err = fmt.Errorf("Unknown pubsub router %s", cfg.Pubsub.Router)
}
return service, err
} With: func FloodSub(pubsubOptions ...pubsub.Option) interface{} {
return func (mctx MetricsCtx, lc fx.Lifecycle, host host.Host) (service *pubsub.PubSub, err error) {
return pubsub.NewFloodSub(lifecycleCtx(mctx, lc), host, pubsubOptions...)
}
}
func GossipSub(pubsubOptions ...pubsub.Option) interface{} {
return func (mctx MetricsCtx, lc fx.Lifecycle, host host.Host) (service *pubsub.PubSub, err error) {
return pubsub.NewGossipSub(lifecycleCtx(mctx, lc), host, pubsubOptions...)
}
}
func FromConfig(cfg *config.Config) (fx.Option, error) {
var opts []fx.Option
// ...
// Could live in a different function that _returns_ an `fx.Option` but it doesn't have to.
var pubsubOptions []pubsub.Option
if cfg.Pubsub.DisableSigning {
pubsubOptions = append(pubsubOptions, pubsub.WithMessageSigning(false))
}
if cfg.Pubsub.StrictSignatureVerification {
pubsubOptions = append(pubsubOptions, pubsub.WithStrictSignatureVerification(true))
}
switch cfg.Pubsub.Router {
case "":
fallthrough
case "floodsub":
opts = append(opts, FloodSub(pubsubOptions))
case "gossipsub":
opts = append(opts, GossipSub(pubsubOptions))
default:
return nil, fmt.Errorf("Unknown pubsub router %s", cfg.Pubsub.Router)
}
// ...
return fx.Options(opts...), nil
} That way we parse the config all at once but still start everything in the right order. |
This was referenced Apr 23, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The DI-based constructor currently has a bunch of options that take the config and then spit out some service.
I'd like to consider replacing these with a single super-option constructor that takes the config and then constructs the options necessary to start the node described in that config.
Rational:
Thoughts @magik6k?
The text was updated successfully, but these errors were encountered: