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

RFC: Invert DI Constructor Config Handling #6232

Closed
Stebalien opened this issue Apr 18, 2019 · 1 comment
Closed

RFC: Invert DI Constructor Config Handling #6232

Stebalien opened this issue Apr 18, 2019 · 1 comment
Assignees

Comments

@Stebalien
Copy link
Member

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:

  • Ideally, applications using libp2p will configure libp2p through options instead of through a config file. This isn't currently possible
  • I believe this will make it easier to reason about the config (centralizes everything).

Thoughts @magik6k?

@Stebalien
Copy link
Member Author

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.

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

No branches or pull requests

2 participants