/usr/share/gocode/src/github.com/paulbellamy/ratecounter/ratecounter.go is in golang-github-paulbellamy-ratecounter-dev 0.0~git20151119.0.5a11f58-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package ratecounter
import (
"strconv"
"time"
)
// A RateCounter is a thread-safe counter which returns the number of times
// 'Incr' has been called in the last interval
type RateCounter struct {
counter Counter
interval time.Duration
}
// Constructs a new RateCounter, for the interval provided
func NewRateCounter(intrvl time.Duration) *RateCounter {
return &RateCounter{
interval: intrvl,
}
}
// Add an event into the RateCounter
func (r *RateCounter) Incr(val int64) {
r.counter.Incr(val)
go r.scheduleDecrement(val)
}
func (r *RateCounter) scheduleDecrement(amount int64) {
time.Sleep(r.interval)
r.counter.Incr(-1 * amount)
}
// Return the current number of events in the last interval
func (r *RateCounter) Rate() int64 {
return r.counter.Value()
}
func (r *RateCounter) String() string {
return strconv.FormatInt(r.counter.Value(), 10)
}
|