|
| 1 | +package holdoff |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +// A vendor's per-second quota answers a burst with many 429s inside one window. Each used to add |
| 11 | +// a strike, so seven 429s in one second escalated a 30 s hold-off to the 30-minute cap |
| 12 | +// (MAG-4386, measured on QuickNode: 28 real 429s in ~2 s held the url off for 31-35 min). A 429 |
| 13 | +// that lands while the url is already held off adds nothing: the burst is one strike, and the |
| 14 | +// hold-off still ends when the first one said. |
| 15 | +func TestBurstWithinOneHoldoffIsOneStrike(t *testing.T) { |
| 16 | + r, now := newTestRegistry(t0) |
| 17 | + |
| 18 | + first := r.RecordRateLimit("vendor", "u", 0) |
| 19 | + require.Equal(t, InitialHoldoff, first) |
| 20 | + |
| 21 | + for i := 1; i < 7; i++ { |
| 22 | + *now = t0.Add(time.Duration(i) * 100 * time.Millisecond) |
| 23 | + remaining := r.RecordRateLimit("vendor", "u", 0) |
| 24 | + require.Equal(t, t0.Add(InitialHoldoff).Sub(*now), remaining, |
| 25 | + "429 #%d inside the window reports what is left of the hold-off, not a new one", i+1) |
| 26 | + } |
| 27 | + require.Equal(t, t0.Add(InitialHoldoff), r.ReadyAt("vendor", "u"), |
| 28 | + "the burst did not move the end of the hold-off") |
| 29 | + |
| 30 | + // The window passes; the next 429 is the second strike, not the eighth. |
| 31 | + *now = t0.Add(InitialHoldoff + time.Second) |
| 32 | + require.Equal(t, 2*InitialHoldoff, r.RecordRateLimit("vendor", "u", 0)) |
| 33 | +} |
| 34 | + |
| 35 | +// An upstream that asks for longer than what is left gets it, and that is still not a strike. |
| 36 | +func TestLongerRetryAfterInsideTheWindowExtendsWithoutAStrike(t *testing.T) { |
| 37 | + r, now := newTestRegistry(t0) |
| 38 | + r.RecordRateLimit("vendor", "u", 0) // strike 1: 30 s |
| 39 | + |
| 40 | + *now = t0.Add(time.Second) |
| 41 | + require.Equal(t, 5*time.Minute, r.RecordRateLimit("vendor", "u", 5*time.Minute)) |
| 42 | + require.Equal(t, t0.Add(time.Second+5*time.Minute), r.ReadyAt("vendor", "u")) |
| 43 | + |
| 44 | + // A shorter ask inside the window never shortens it. |
| 45 | + *now = t0.Add(2 * time.Second) |
| 46 | + require.Equal(t, 5*time.Minute-time.Second, r.RecordRateLimit("vendor", "u", time.Second)) |
| 47 | + require.Equal(t, t0.Add(time.Second+5*time.Minute), r.ReadyAt("vendor", "u")) |
| 48 | + |
| 49 | + // Past it, the next 429 is the second strike: the extension did not count as one. |
| 50 | + *now = t0.Add(time.Second + 5*time.Minute + time.Second) |
| 51 | + require.Equal(t, 2*InitialHoldoff, r.RecordRateLimit("vendor", "u", 0)) |
| 52 | +} |
| 53 | + |
| 54 | +// A 429 that changes nothing is not a recorded hold-off: the burst shows once on the |
| 55 | +// recorded counter and once in the duration histogram, an extension shows as one more. |
| 56 | +func TestMetrics_BurstRecordsOneHoldoff(t *testing.T) { |
| 57 | + r, now := newTestRegistry(t0) |
| 58 | + base := eventCount(t, "vendorBurst", eventRecorded) |
| 59 | + |
| 60 | + r.RecordRateLimit("vendorBurst", "u", 0) |
| 61 | + for i := 1; i < 7; i++ { |
| 62 | + *now = t0.Add(time.Duration(i) * 100 * time.Millisecond) |
| 63 | + r.RecordRateLimit("vendorBurst", "u", 0) |
| 64 | + } |
| 65 | + require.Equal(t, base+1, eventCount(t, "vendorBurst", eventRecorded)) |
| 66 | + |
| 67 | + r.RecordRateLimit("vendorBurst", "u", 10*time.Minute) |
| 68 | + require.Equal(t, base+2, eventCount(t, "vendorBurst", eventRecorded), "an extension is a hold-off applied") |
| 69 | +} |
0 commit comments