Skip to content

Instantly share code, notes, and snippets.

@Raghav2211
Last active September 10, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raghav2211/4f318f26b9a2f7ff50d5ee62675cd5c9 to your computer and use it in GitHub Desktop.
Save Raghav2211/4f318f26b9a2f7ff50d5ee62675cd5c9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
var jobs = []int {1,2, 3, 4,5,6,7,8,9,10}
func producer ( link chan int , backpressure <- chan int) {
for {
backpressureEvent := <- backpressure
if len(jobs) == 0 {
close(link) // close link channel
break
}
eles := jobs[ 0: backpressureEvent]
jobs = jobs[ backpressureEvent : len(jobs) ]
for ele:= range eles {
fmt.Println("Send job with id -- ",eles[ele])
link <- eles[ele]
}
}
}
func consumer(link <- chan int , backpressure chan int , isProcessDone chan bool) {
for {
backpressure <- 1
jobid, isClose := <- link
if !isClose {
close(backpressure) // close backpressure event channel
isProcessDone <- true
break
} else {
fmt.Println("Receive job with id -- ",jobid)
}
}
}
func main() {
link := make(chan int)
backpressure := make(chan int)
done := make(chan bool)
go producer(link,backpressure)
go consumer(link,backpressure ,done)
<-done
fmt.Println("All Done !! :)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment