https://github.com/asimpleidea/hit-count
  
  
    A simple counters manager written in golang. 
    https://github.com/asimpleidea/hit-count
  
counter occurrences target
        Last synced: 5 months ago 
        JSON representation
    
A simple counters manager written in golang.
- Host: GitHub
 - URL: https://github.com/asimpleidea/hit-count
 - Owner: asimpleidea
 - License: apache-2.0
 - Created: 2021-01-16T09:12:47.000Z (almost 5 years ago)
 - Default Branch: master
 - Last Pushed: 2021-01-16T14:37:30.000Z (almost 5 years ago)
 - Last Synced: 2023-07-11T06:46:26.068Z (over 2 years ago)
 - Topics: counter, occurrences, target
 - Language: Go
 - Homepage:
 - Size: 18.6 KB
 - Stars: 0
 - Watchers: 1
 - Forks: 0
 - Open Issues: 0
 - 
            Metadata Files:
            
- Readme: README.md
 - License: LICENSE
 
 
Awesome Lists containing this project
README
          # HitCount





A simple Golang package that counts occurrences of a certain event.
## Usage
```golang
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()
}
```