Last active
June 16, 2022 18:26
-
-
Save iameli/af6465ed89a0cff599633528959a8d23 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
| // To execute Go code, please declare a func main() in a package "main" | |
| package main | |
| import ( | |
| "fmt" | |
| "math/rand" | |
| "time" | |
| ) | |
| type event string | |
| var MAX_QUEUE_SIZE int = 2000 | |
| func main() { | |
| eventCh := make(chan event) | |
| go func() { | |
| for { | |
| fmt.Println("Sending event!") | |
| eventCh <- "hi" | |
| dur := time.Duration(rand.Intn(1000)) | |
| time.Sleep(dur * time.Millisecond) | |
| } | |
| }() | |
| reconcile := func() { | |
| fmt.Println("Reconciling...") | |
| time.Sleep(750 * time.Millisecond) | |
| } | |
| inbox := make(chan event, 1) | |
| go func() { | |
| for { | |
| e := <-eventCh | |
| select { | |
| case inbox <- e: | |
| // Event is now in the inbox | |
| fmt.Println("Event queued in inbox") | |
| default: | |
| // Overflow event gets dropped | |
| fmt.Println("Discarded overflow event") | |
| } | |
| } | |
| }() | |
| for { | |
| <-inbox | |
| reconcile() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment