RateCounter
type RateCounter struct { // contains filtered or unexported fields}RateCounter is a named counter that can be incremented and queried.
Methods
Increment
View sourcefunc (rc *RateCounter) Increment(entry string, delta uint32) errorIncrement increments the rate counter for this entry by the given delta value. The minimum value is 0 and the maximum is 1000.
Parameters
- entry: string
- delta: uint32
Returns
error
LookupCount
View sourceLookupCount returns the estimated number of events in the given duration for this entry. The duration represents a discrete window, not a continuous one. See CounterDuration for more details.
Parameters
- entry: string
- duration: CounterDuration
Returns
(uint32, error)
12345678910111213fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) { rc := erl.OpenRateCounter("requests")
rc.Increment(r.RemoteAddr, 1)
count, err := rc.LookupCount(r.RemoteAddr, erl.CounterDuration60s) if err != nil { w.WriteHeader(fsthttp.StatusInternalServerError) return }
fmt.Fprintf(w, "Estimated count over the past 60 seconds: %d requests\n", count)})LookupRate
View sourceLookupRate returns the rate of events per second over the given rate window for this entry.
Parameters
- entry: string
- window: RateWindow
Returns
(uint32, error)
12345678910111213fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) { rc := erl.OpenRateCounter("requests")
rc.Increment(r.RemoteAddr, 1)
rate, err := rc.LookupRate(r.RemoteAddr, erl.RateWindow60s) if err != nil { w.WriteHeader(fsthttp.StatusInternalServerError) return }
fmt.Fprintf(w, "Rate over the past 60 seconds: %d requests per second\n", rate)})