Last active
November 26, 2020 00:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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