util/limiter: don't panic when dumping a new Limiter
Fixes #18439 Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
2cb86cf65e
commit
6dc0bd834c
@@ -187,6 +187,9 @@ func (lm *Limiter[K]) collectDump(now time.Time) []dumpEntry[K] {
|
|||||||
lm.mu.Lock()
|
lm.mu.Lock()
|
||||||
defer lm.mu.Unlock()
|
defer lm.mu.Unlock()
|
||||||
|
|
||||||
|
if lm.cache == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
ret := make([]dumpEntry[K], 0, lm.cache.Len())
|
ret := make([]dumpEntry[K], 0, lm.cache.Len())
|
||||||
lm.cache.ForEach(func(k K, v *bucket) {
|
lm.cache.ForEach(func(k K, v *bucket) {
|
||||||
lm.updateBucketLocked(v, now) // so stats are accurate
|
lm.updateBucketLocked(v, now) // so stats are accurate
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package limiter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -175,6 +176,10 @@ func TestDumpHTML(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDumpHTMLEmpty(t *testing.T) {
|
||||||
|
new(Limiter[string]).DumpHTML(io.Discard, false) // should not panic
|
||||||
|
}
|
||||||
|
|
||||||
func allowed(t *testing.T, limiter *Limiter[string], key string, count int, now time.Time) {
|
func allowed(t *testing.T, limiter *Limiter[string], key string, count int, now time.Time) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
for i := range count {
|
for i := range count {
|
||||||
|
|||||||
Reference in New Issue
Block a user