Created
October 24, 2019 12:59
-
-
Save disconnect3d/b2bf4c69541f6a2023ce287da0f1c6e0 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 ( | |
"time" | |
"fmt" | |
) | |
func main() { | |
bundles := make(chan int, 5) | |
cancelProcessing := make(chan struct{}) | |
go func() { | |
counter := 0 | |
for bundle := range bundles { | |
fmt.Println("[2] Counter =", counter, "Got bundle", bundle, "sleeping for 2s") | |
time.Sleep(time.Second*2) | |
fmt.Println("[2] Counter =", counter, "Got bundle", bundle, "WOKE UP") | |
if counter > 1 { | |
//time.Sleep(1 * time.Second) | |
fmt.Println("[2] Closing cancelProcessing...") | |
close(cancelProcessing) | |
fmt.Println("[2] Closed cancelProcessing") | |
break | |
} | |
counter++ | |
} | |
close(bundles) | |
}() | |
foo(bundles, cancelProcessing) | |
time.Sleep(time.Second * 10) | |
} | |
func foo(output chan<- int, cancel <-chan struct{}) { | |
batches := []int{11, 22, 33, 44, 55, 66} | |
for _, batch := range batches { | |
fmt.Println("Iteration with batch:", batch) | |
select { | |
case output <- batch: | |
fmt.Println("Sent batch", batch, "to output channel") | |
case <-cancel: | |
fmt.Println("Cancel") | |
break | |
case <-time.After(time.Second): | |
fmt.Println("Timeout!") | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment