mirror of
https://github.com/yunginnanet/Rate5
synced 2024-06-30 19:10:51 +00:00
add: ability to retrieve very basic statistics (count)
This commit is contained in:
parent
36294aeeac
commit
b33adc7f76
@ -90,15 +90,26 @@ func init() {
|
||||
|
||||
pre := "[Rate5] "
|
||||
go func() {
|
||||
var lastcount = 0
|
||||
var count = 0
|
||||
for {
|
||||
select {
|
||||
case msg := <-rd:
|
||||
println(pre + "Limit: " + msg)
|
||||
fmt.Printf("%s Limit: %s \n", pre, msg)
|
||||
count++
|
||||
case msg := <-rrd:
|
||||
println(pre + "RegLimit: " + msg)
|
||||
fmt.Printf("%s RegLimit: %s \n", pre, msg)
|
||||
count++
|
||||
case msg := <-crd:
|
||||
println(pre + "CmdLimit: " + msg)
|
||||
fmt.Printf("%s CmdLimit: %s \n", pre, msg)
|
||||
count++
|
||||
default:
|
||||
if count - lastcount >= 25 {
|
||||
lastcount = count
|
||||
fmt.Println("Rater: ", Rater.GetGrandTotalRated())
|
||||
fmt.Println("RegRater: ", RegRater.GetGrandTotalRated())
|
||||
fmt.Println("CmdRater: ", CmdRater.GetGrandTotalRated())
|
||||
}
|
||||
time.Sleep(time.Duration(10) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package ratelimit
|
||||
package rate5
|
||||
|
||||
import (
|
||||
"sync"
|
||||
@ -31,8 +31,9 @@ type Limiter struct {
|
||||
delivered through a channel. See: DebugChannel() */
|
||||
Debug bool
|
||||
|
||||
count int
|
||||
known map[interface{}]int
|
||||
mu *sync.Mutex
|
||||
mu *sync.RWMutex
|
||||
}
|
||||
|
||||
// Policy defines the mechanics of our ratelimiter
|
||||
|
@ -1,4 +1,4 @@
|
||||
package ratelimit
|
||||
package rate5
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -37,7 +37,7 @@ func newLimiter(window int, burst int, strict bool) *Limiter {
|
||||
}
|
||||
q.Patrons = cache.New(time.Duration(q.Ruleset.Window)*time.Second, 5*time.Second)
|
||||
q.known = make(map[interface{}]int)
|
||||
q.mu = &sync.Mutex{}
|
||||
q.mu = &sync.RWMutex{}
|
||||
return q
|
||||
}
|
||||
|
||||
@ -69,6 +69,7 @@ func (q *Limiter) Check(from Identity) bool {
|
||||
if count > q.Ruleset.Burst {
|
||||
if !q.Ruleset.Strict {
|
||||
q.debugPrint("ratelimit (limited): ", count, " ", src)
|
||||
q.increment()
|
||||
return true
|
||||
}
|
||||
q.mu.Lock()
|
||||
@ -83,6 +84,7 @@ func (q *Limiter) Check(from Identity) bool {
|
||||
println("Rate5: " + err.Error())
|
||||
}
|
||||
q.debugPrint("ratelimit (strictly limited): ", count, " ", src)
|
||||
q.increment()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@ -97,6 +99,19 @@ func (q *Limiter) Peek(from Identity) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (q *Limiter) increment() {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
q.count++
|
||||
}
|
||||
|
||||
// GetGrandTotalRated returns the historic total amount of times we have ever reported something as ratelimited
|
||||
func (q *Limiter) GetGrandTotalRated() int {
|
||||
q.mu.RLock()
|
||||
defer q.mu.RUnlock()
|
||||
return q.count
|
||||
}
|
||||
|
||||
func (q *Limiter) debugPrint(a ...interface{}) {
|
||||
if q.Debug {
|
||||
debugChannel <- fmt.Sprint(a...)
|
||||
|
Loading…
Reference in New Issue
Block a user