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

Support for concurrent initialization #373

Open
bcspragu opened this issue Sep 7, 2022 · 0 comments
Open

Support for concurrent initialization #373

bcspragu opened this issue Sep 7, 2022 · 0 comments

Comments

@bcspragu
Copy link

bcspragu commented Sep 7, 2022

Is your feature request related to a problem? Please describe.

Our service has a few dozen dependencies of various complexity. As part of initializing some of them, we'll 'ping' the API in question so that the service can fail fast if something is wrong (e.g. auth or some other configuration). Doing all of these serially means our service takes several seconds to start up, which is noticeable when using 'serverless' systems that scale down to zero, like GCP's Cloud Run.

Describe the solution you'd like

I'd like wire to generate code that parallelizes initialization when possible, e.g.:

func initializeBaz(ctx context.Context) foobarbaz.Baz {
    fooChan, barChan := [...]
    go func() { fooChan <- foobarbaz.ProvideFoo() }
    go func() { barChan <- foobarbaz.ProvideBar() }
    return foobarbaz.ProvideBaz(ctx, <-fooChan, <-barChan)
}

(Note: I recognize this contrived example ignores nested things and error-handling, which make this slightly more complicated)

And tangentially, but usefully, each provider could be wrapped in a sync.Once and cached.

Describe alternatives you've considered

The caching can be built into the provider itself by doing something like:

var onceFoo sync.Once
var fooVal Foo
func ProvideFoo() Foo {
  onceFoo.Do(provideFoo)
  return fooVal
}

But the async/concurrent bit can't be done natively, as far as I know.

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

1 participant