RateCounter

Structin erlcompute-sdk-go v1.10.0View source
type RateCounter struct {
// contains filtered or unexported fields
}

RateCounter is a named counter that can be incremented and queried.

Methods

Increment

View source
func (rc *RateCounter) Increment(entry string, delta uint32) error

Increment 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 source
func (rc *RateCounter) LookupCount(entry string, duration CounterDuration) (uint32, error)

LookupCount 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
fsthttp.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 source
func (rc *RateCounter) LookupRate(entry string, window RateWindow) (uint32, error)

LookupRate returns the rate of events per second over the given rate window for this entry.

Parameters

entry: string
window: RateWindow

Returns

(uint32, error)
1
2
3
4
5
6
7
8
9
10
11
12
13
fsthttp.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)
})