Skip to content

Instantly share code, notes, and snippets.

@nileshsimaria
Created May 2, 2018 03:30
Show Gist options
  • Save nileshsimaria/71c38e3eae8b9d870b1e1207f4662bf4 to your computer and use it in GitHub Desktop.
Save nileshsimaria/71c38e3eae8b9d870b1e1207f4662bf4 to your computer and use it in GitHub Desktop.
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