I'm running into an unusual scenario where:
- an external tool calls multiple endpoints of my API at the same time,
- all endpoints rely on the same config file hosted somewhere on S3.
That works but it's fetching the same config file many times concurrently when it could be fetched only once. To experiment with this I have a minimal version here https://go.dev/play/p/Nx-kidmprQx that gives back random ints rather than doing HTTP calls.
Currently it prints:
#2 start
#1 start
#1 result 5577006791947779410
#2 result 8674665223082153551
#3 start
#3 result 6129484611666145821
But I would like the first 2 calls to return the same value because they are done concurrently:
#2 start
#1 start
#1 result 5577006791947779410
#2 result 5577006791947779410
#3 start
#3 result 6129484611666145821
I'm struggling to imagine a solution for this. The fact that multiple goroutines should wait on a single result is confusing. How could it be done?