-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
72 lines (66 loc) · 1.84 KB
/
debug.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package rate5
import (
"fmt"
"sync/atomic"
)
const (
msgRateLimitExpired = "ratelimit (expired): %s | last count [%d]"
msgDebugEnabled = "rate5 debug enabled"
msgRateLimitedRst = "ratelimit for %s has been reset"
msgRateLimitedNew = "ratelimit %s (new) "
msgRateLimited = "ratelimit %s: last count %d. time: %s"
msgRateLimitStrict = "%s ratelimit for %s: last count %d. time: %s"
)
func (q *Limiter) debugPrintf(format string, a ...interface{}) {
if atomic.CompareAndSwapUint32(&q.debug, DebugDisabled, DebugDisabled) {
return
}
if len(a) == 2 {
if _, ok := a[1].(*atomic.Int64); ok {
a[1] = a[1].(*atomic.Int64).Load()
}
}
msg := fmt.Sprintf(format, a...)
select {
case q.debugChannel <- msg:
//
default:
// drop the message but increment the lost counter
atomic.AddInt64(&q.debugLost, 1)
}
}
func (q *Limiter) setDebugEvict() {
q.Patrons.OnEvicted(func(src string, count interface{}) {
q.debugPrintf(msgRateLimitExpired, src, count.(*atomic.Int64).Load())
})
}
func (q *Limiter) SetDebug(on bool) {
switch on {
case true:
if atomic.CompareAndSwapUint32(&q.debug, DebugDisabled, DebugEnabled) {
q.debugPrintf(msgDebugEnabled)
}
case false:
atomic.CompareAndSwapUint32(&q.debug, DebugEnabled, DebugDisabled)
}
}
// DebugChannel enables debug mode and returns a channel where debug messages are sent.
//
// NOTE: If you do not read from this channel, the debug messages will eventually be lost.
// If this happens,
func (q *Limiter) DebugChannel() chan string {
defer func() {
atomic.CompareAndSwapUint32(&q.debug, DebugDisabled, DebugEnabled)
}()
q.debugMutex.RLock()
if q.debugChannel != nil {
q.debugMutex.RUnlock()
return q.debugChannel
}
q.debugMutex.RUnlock()
q.debugMutex.Lock()
defer q.debugMutex.Unlock()
q.debugChannel = make(chan string, 55)
q.setDebugEvict()
return q.debugChannel
}