-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Hardcore limiter and Reset function
- Loading branch information
1 parent
5ba326d
commit 2eb517e
Showing
4 changed files
with
240 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package rate5 | ||
|
||
import "fmt" | ||
|
||
func (q *Limiter) debugPrintf(format string, a ...interface{}) { | ||
q.debugMutex.RLock() | ||
defer q.debugMutex.RUnlock() | ||
if !q.debug { | ||
return | ||
} | ||
msg := fmt.Sprintf(format, a...) | ||
select { | ||
case q.debugChannel <- msg: | ||
default: | ||
println(msg) | ||
} | ||
} | ||
|
||
func (q *Limiter) setDebugEvict() { | ||
q.Patrons.OnEvicted(func(src string, count interface{}) { | ||
q.debugPrintf("ratelimit (expired): %s | last count [%d]", src, count) | ||
}) | ||
} | ||
|
||
func (q *Limiter) SetDebug(on bool) { | ||
q.debugMutex.Lock() | ||
if !on { | ||
q.debug = false | ||
q.Patrons.OnEvicted(nil) | ||
q.debugMutex.Unlock() | ||
return | ||
} | ||
q.debug = on | ||
q.setDebugEvict() | ||
q.debugMutex.Unlock() | ||
q.debugPrintf("rate5 debug enabled") | ||
} | ||
|
||
// DebugChannel enables debug mode and returns a channel where debug messages are sent. | ||
// NOTE: You must read from this channel if created via this function or it will block | ||
func (q *Limiter) DebugChannel() chan string { | ||
defer func() { | ||
q.debugMutex.Lock() | ||
q.debug = true | ||
q.debugMutex.Unlock() | ||
}() | ||
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, 25) | ||
q.setDebugEvict() | ||
return q.debugChannel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.