Remove unnecessary cache copying in ratelimiting

Signed-off-by: Wendi Yu <wendi.yu@yahoo.ca>
This commit is contained in:
Wendi Yu 2020-05-08 10:16:43 -06:00
parent 8a94ccc5a2
commit ac5039ab39
2 changed files with 3 additions and 14 deletions

View File

@ -196,12 +196,12 @@ func newNode(t *testing.T, prefix string, https *httptest.Server, weirdPrefs boo
logfe := func(fmt string, args ...interface{}) {
t.Logf(prefix+".e: "+fmt, args...)
}
logfe := logger.RateLimitedFn(logfe, 1, 1)
logfe = logger.RateLimitedFn(logfe, 1, 1, 100)
logf := func(fmt string, args ...interface{}) {
t.Logf(prefix+": "+fmt, args...)
}
logf := logger.RateLimitedFn(logf, 1, 1, 100)
logf = logger.RateLimitedFn(logf, 1, 1, 100)
var err error
httpc := https.Client()

View File

@ -95,20 +95,9 @@ func RateLimitedFn(logf Logf, f float64, b int, m int) Logf {
mu.Lock()
if msgCache.Len() > m {
delete(msgLim, msgCache.Back().Value.(string))
msgCache.Remove(msgCache.Back())
}
// Purge msgLim of outdated keys to reduce overhead. Must be done by copying
// over to a new map and allowing the garbage collector to eat the entire old one,
// since deleting keys in maps is done through a "zombie flag" on the data rather than
// actually clearing it from memory. See https://github.com/golang/go/issues/20135
if len(msgLim)-msgCache.Len() > 100 {
newList := make(map[string]*limitData)
for e := msgCache.Front(); e != nil; e = e.Next() {
newList[e.Value.(string)] = msgLim[e.Value.(string)]
}
msgLim = newList
}
mu.Unlock()
}
}