Last active
September 10, 2020 16:47
-
-
Save Raghav2211/4f318f26b9a2f7ff50d5ee62675cd5c9 to your computer and use it in GitHub Desktop.
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" | |
) | |
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