GoLang Singleflight: Dont repeat yourself – at the same time



GoLang Singleflight: Dont repeat yourself – at the same time

GoLang Singleflight: Dont repeat yourself - at the same time

Discovered an awesome Go sub-package called singleflight.

https://godoc.org/golang.org/x/sync/singleflight

Executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.

Can be used for reducing effects of Thundering Herd Problem, speeding up cache lookups, or as replacement to any expensive pure function – as long as singleflight has a sensible cacheGroup key.