Skip to content

Instantly share code, notes, and snippets.

@iameli
Last active June 16, 2022 18:26
Show Gist options
  • Select an option

  • Save iameli/af6465ed89a0cff599633528959a8d23 to your computer and use it in GitHub Desktop.

Select an option

Save iameli/af6465ed89a0cff599633528959a8d23 to your computer and use it in GitHub Desktop.
// 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