From 0ea51872c9085c468bb1ad2213234b638856ab49 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 3 Jul 2020 13:09:29 -0700 Subject: [PATCH] types/logger: add rateFreePrefix rate-limiting-exempt log format prefixes Per conversation with @danderson. --- types/logger/logger.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/types/logger/logger.go b/types/logger/logger.go index 42b520460..c4c11c151 100644 --- a/types/logger/logger.go +++ b/types/logger/logger.go @@ -15,6 +15,7 @@ import ( "io/ioutil" "log" "os" + "strings" "sync" "time" @@ -63,6 +64,13 @@ type limitData struct { var disableRateLimit = os.Getenv("TS_DEBUG_LOG_RATE") == "all" +// rateFreePrefix are format string prefixes that are exempt from rate limiting. +// Things should not be added to this unless they're already limited otherwise. +var rateFreePrefix = []string{ + "magicsock: disco: ", + "magicsock: CreateEndpoint:", +} + // RateLimitedFn returns a rate-limiting Logf wrapping the given logf. // Messages are allowed through at a maximum of one message every f (where f is a time.Duration), in // bursts of up to burst messages at a time. Up to maxCache strings will be held at a time. @@ -85,6 +93,12 @@ func RateLimitedFn(logf Logf, f time.Duration, burst int, maxCache int) Logf { ) judge := func(format string) verdict { + for _, pfx := range rateFreePrefix { + if strings.HasPrefix(format, pfx) { + return allow + } + } + mu.Lock() defer mu.Unlock() rl, ok := msgLim[format]