9 ms·
I recently found myself in need of a semaphore in go. They're quite easy to make out of channels: Implementation: https://github.com/scraperwiki/s4log/blob/ma
by peterwaller 12y ago
I recently found myself in need of a semaphore in go. They're quite easy to make out of channels:
Implementation:
https://github.com/scraperwiki/s4log/blob/master/semaphore.go https://github.com/scraperwiki/s4log/blob/master/semaphore.g...
Use:
https://github.com/scraperwiki/s4log/blob/2714f9553880121ad6852846a4761f65a22967d8/commit.go#L103-L105 https://github.com/scraperwiki/s4log/blob/2714f9553880121ad6...
Useful for:
1. Writing a web handler which may use some CPU time and you don't want too many of them in flight at once (to prevent them from all slowing down to mud). (Best combined with something to abort if we had to wait to long before we could even do computation)
2. Execute a parallel upload to S3, but avoid doing too many at once (again, because you don't want very many of them contending on the network connection such that none proceed).