Map of N locks contented by X goroutines. Example of locking workers (or http handlers) from running the same process if another goroutine is already working for that "key". In this example there are 2 keys (pid %2) shared between 10 threads. This means only two threads (2 unique keys) are ever processing in parallel. The other 8 threads wait their turn.
Developed when I had an expensive API request that the same client might end up calling repeatedly instead of only calling once - then sharing the result. Go had this issue with parallel requests for the same DNS record (though I don't know how they solved that).
Another suggestion was to create a
sync.Mapof locks.This looks like it works, but how do we clean up keys without race-conditions? One process might delete the key while another one has already put a new lock into the store (or pulled the existing one out).