Advanced Concurrency
Package singleflight
provides a duplicate function call suppression mechanism.
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.
Package errgroup
best described as a
sync.WaitGroup
but where the tasks return errors that are propagated back to the waiter.useful when you have multiple operations that you want to wait for, but you also want to determine if they all completed successfully.
Bounded concurrency via buffered channels
through the use of semaphores by keeping track of how many tasks are running, and to block until there is room for another task to start.
to allow up to 10 tasks to run at once, we create a channel with space for 10 items:
semaphore := make(chan struct{}, 10)
to start a new task, blocking if too many tasks are already running, we simply attempt to send a value on the channel:
semaphore <- struct{}{}
When a task completes, mark it as such by taking a value out of the channel:
<-semaphore
Weighted bounded concurrency
not all tasks are equally expensive => instead of reasoning about the number of tasks we want to run concurrently, we come up with a "cost" for every task and acquire and release that cost from a semaphore.
golang.org/x/sync/sempahore package provides a weighted semaphore implementation
sem <- struct{}{}
operation is called "Acquire"semaphore.Acquire
method returns an error; that is because it can be used with thecontext
package to abort the operation early.
<-sem
operation is called "Release"
Resources:
Last updated