Skip to content

Instantly share code, notes, and snippets.

@theckman theckman/cr_15954.go

Last active Apr 17, 2018
What would you like to do?
func doWork(s string, ch <-chan struct{}, wg *sync.WaitGroup) {
defer func() {
<-ch // free up space in the semaphore
wg.Done() // tell the WaitGroup we're finished
func execute(work []string) {
wg := &sync.WaitGroup{}
sema := make(chan struct{}, 10) // concurrency limit of 10
for _, url := range work {
// if there are 10 items in flight, channel is full / will block
// unblocks when a worker finishes
sema <- struct{}{}
go doWork(url, sema)
// close the channel as nothing else should write
// wait for all goroutines to finish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.