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.

Signed-off-by: Wendi Yu <wendi.yu@yahoo.ca>
This commit is contained in:
Wendi Yu
2020-05-06 17:27:32 -06:00
parent e3559d1c45
commit ca6675386b
5 changed files with 94 additions and 4 deletions

View File

@@ -23,6 +23,7 @@ import (
"tailscale.com/control/controlclient"
"tailscale.com/tailcfg"
"tailscale.com/tstest"
"tailscale.com/types/logger"
"tailscale.com/wgengine"
"tailscale.com/wgengine/magicsock"
"tailscale.com/wgengine/router"
@@ -191,12 +192,16 @@ type testNode struct {
// Create a new IPN node.
func newNode(t *testing.T, prefix string, https *httptest.Server, weirdPrefs bool) testNode {
t.Helper()
logfe := func(fmt string, args ...interface{}) {
ulogfe := func(fmt string, args ...interface{}) {
t.Logf(prefix+".e: "+fmt, args...)
}
logf := func(fmt string, args ...interface{}) {
logfe := logger.RateLimitedFn(ulogfe, 1, 1)
ulogf := func(fmt string, args ...interface{}) {
t.Logf(prefix+": "+fmt, args...)
}
logf := logger.RateLimitedFn(ulogf, 1, 1)
var err error
httpc := https.Client()

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)
connect := func() {
for i := 1; i <= 2; i++ {
logf("connect %d ...", i)