Skip to content

Instantly share code, notes, and snippets.

@serafdev
Last active November 26, 2020 00:50
Show Gist options
  • Save serafdev/227f89bac32078d983eb3fbc15d71390 to your computer and use it in GitHub Desktop.
Save serafdev/227f89bac32078d983eb3fbc15d71390 to your computer and use it in GitHub Desktop.
Simplest way to limit number of concurrent go routines in a continuous feed
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan struct{}, 5)
for {
ch <- struct{}{}
fmt.Println(len(ch))
go worker(&ch)
time.Sleep(time.Second * 3)
}
}
func worker(ch *chan struct{}) {
time.Sleep(time.Second * 30)
<-*ch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment