Created
May 2, 2018 03:30
-
-
Save nileshsimaria/71c38e3eae8b9d870b1e1207f4662bf4 to your computer and use it in GitHub Desktop.
This file contains 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" | |
) | |
const BufferedChLen = 1024 | |
func consumer() chan<- int { | |
bch := make(chan int, BufferedChLen) | |
// On every 100th ms, consume whatever is available in the channel | |
ticker := time.NewTicker(100 * time.Millisecond) | |
go func() { | |
for range ticker.C { | |
n := len(bch) | |
for i := 0; i < n; i++ { | |
<-bch | |
} | |
fmt.Printf("consumed %d\n", n) | |
} | |
}() | |
return bch | |
} | |
func main() { | |
bch := consumer() | |
// start couple of producers | |
for p := 0; p < 2; p++ { | |
go func(data int) { | |
for { | |
bch <- data | |
time.Sleep(1 * time.Millisecond) | |
} | |
}(p) | |
} | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment