A simple Golang package that counts occurrences of a certain event.
import (
counters "github.com/SunSince90/hit-count"
)
func main() {
key404 = "404"
key500 = "500"
manager := counters.Manager("error-counters")
// Create counters with target 10.
// When that counter reaches 10, do something about this.
manager.Add(key404, counters.NewIntCounter(0, 10))
manager.Add(key500, counters.NewIntCounter(0, 10))
for {
// do something...
resp, err := something()
if err == Err404 {
if manager.Get(key404).Increase().Hit() {
// Got error 404 for 10 times **in a row**:
// Do something about this
}
}
if err == Err500 {
// Need error 404 to happen for ten times in a row.
// This time it didn't happen, so we reset the couter.
manager.Get(key404).Reset()
if manager.Get(key500).Increase().Hit() {
// Got error 500 for 10 times:
// Do something about this.
// NOTE: We don't need Err500 to happen 10 times in a row,
// that's why we don't reset it, like we did with key404
}
}
}
// Reset all counters...
manager.ResetAllExcept()
}