|
|
@ -15,6 +15,7 @@ import (
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
@ -63,6 +64,13 @@ type limitData struct {
|
|
|
|
|
|
|
|
|
|
|
|
var disableRateLimit = os.Getenv("TS_DEBUG_LOG_RATE") == "all"
|
|
|
|
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.
|
|
|
|
// 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
|
|
|
|
// 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.
|
|
|
|
// 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 {
|
|
|
|
judge := func(format string) verdict {
|
|
|
|
|
|
|
|
for _, pfx := range rateFreePrefix {
|
|
|
|
|
|
|
|
if strings.HasPrefix(format, pfx) {
|
|
|
|
|
|
|
|
return allow
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
rl, ok := msgLim[format]
|
|
|
|
rl, ok := msgLim[format]
|
|
|
|