Implement rate limiting on log messages (#356)

Implement rate limiting on log messages

Addresses issue #317, where logs can get spammed with the same message
nonstop. Created a rate limiting closure on logging functions, which
limits the number of messages being logged per second based on format
string. To keep memory usage as constant as possible, the previous cache
purging at periodic time intervals has been replaced by an LRU that
discards the oldest string when the capacity of the cache is reached.


Signed-off-by: Wendi Yu <wendi.yu@yahoo.ca>
This commit is contained in:
Wendi Yu
2020-05-08 13:21:36 -06:00
committed by GitHub
parent 499c8fcbb3
commit 0c69b4e00d
5 changed files with 112 additions and 7 deletions

View File

@@ -13,6 +13,8 @@ import (
"strings"
"testing"
"tailscale.com/types/logger"
"tailscale.com/ipn"
"tailscale.com/ipn/ipnserver"
"tailscale.com/safesocket"
@@ -32,12 +34,14 @@ func TestRunMultipleAccepts(t *testing.T) {
defer os.RemoveAll(td)
socketPath := filepath.Join(td, "tailscale.sock")
logf := func(format string, args ...interface{}) {
ulogf := func(format string, args ...interface{}) {
format = strings.TrimRight(format, "\n")
println(fmt.Sprintf(format, args...))
t.Logf(format, args...)
}
logf := logger.RateLimitedFn(ulogf, 1, 1, 100)
connect := func() {
for i := 1; i <= 2; i++ {
logf("connect %d ...", i)