Support for concurrent initialization
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.