Skip to content

Instantly share code, notes, and snippets.

@songrgg
Last active July 2, 2017 15:28
Show Gist options
  • Save songrgg/db41b6b2085c98edf854e84fcf9e8c62 to your computer and use it in GitHub Desktop.
Save songrgg/db41b6b2085c98edf854e84fcf9e8c62 to your computer and use it in GitHub Desktop.
package main
import "time"
var metrics chan string
const (
THRESHOLD int = 100
BUFFER_SIZE int = 1000
)
func init() {
metrics = make(chan string, BUFFER_SIZE)
}
func processMetrics() {
var buf []string
t := time.NewTicker(1 * time.Second)
for {
select {
case s := <-metrics:
buf = append(buf, s)
if len(buf) >= THRESHOLD {
// flush the buffer
buf = nil
}
case <-t.C:
if len(buf) > 0 {
// flush the buffer
buf = nil
}
}
}
}
func main() {
go processMetrics()
for {
// metric collector will be blocking when channel is full
metrics <- "some metrics"
}
for {
// metric collector will be blocking when channel is full
select {
case metrics <- "some metrics":
default:
fmt.Println("metrics are discarding now, but no influence to service")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment