fix: keep a smaller GoatCounter window once full

A day over the excluded-path cap was read whole, then again in halves,
and the next day the same. Four busy weeks spent the 500 requests
GoatCounter allows per hour before the report was written. The window
now keeps the size that fit, so the overflow is paid once.
This commit is contained in:
Abdessamad Derraz committed 2026-11-20 04:37:46 +01:00
1 parent e1c207ded4
commit 075ae0d9ad
2 files changed
+27 -15

No files matched your search

+15 -15
View File
@@ -162,29 +162,29 @@ def _goat_window(start: dt.datetime, end: dt.datetime, auth: dict[str, str]) ->
def _goat_hits(start: dt.datetime, end: dt.datetime, auth: dict[str, str]) -> list[tuple[str, int]]:
"""Counts by path over the whole span, summed day by day: GoatCounter
keeps hourly counts, so windows add up. The API pages with
"""Counts by path over the whole span, summed window by window:
GoatCounter keeps hourly counts, so windows add up. The API pages with
exclude_paths, which lengthens the address by every path already read
(GOAT_MAX_EXCLUDED). A day with too many paths is read again in
halves, down to one hour. Days, not weeks: a window that overflows is
read twice, and four weeks cost 432 of the 500 requests GoatCounter
allows per window."""
(GOAT_MAX_EXCLUDED). A window with too many paths is read again at half
the size, down to one hour, and the windows after it keep that size: a
busy day is followed by busy days, and reading each one whole first
paid every page twice and spent the 500 requests GoatCounter allows
per hour on four weeks."""
totals: dict[str, int] = defaultdict(int)
day = dt.timedelta(days=1)
pending = [(low, min(low + day, end)) for low in
(start + n * day for n in range(-(-(end - start) // day)))]
while pending:
low, high = pending.pop()
hour = dt.timedelta(hours=1)
size = dt.timedelta(days=1)
low = start
while low < end:
high = min(low + size, end)
counts = _goat_window(low, high, auth)
if counts is None:
hours = int((high - low).total_seconds()) // 3600
if hours <= 1:
if high - low <= hour:
raise SystemExit(f"GoatCounter holds more than {GOAT_MAX_EXCLUDED} paths in the hour from {low}")
middle = low + dt.timedelta(hours=hours // 2)
pending += [(low, middle), (middle, high)]
size = max(hour, (high - low) // 2 // hour * hour)
continue
for path, count in counts.items():
totals[path] += count
low = high
return list(totals.items())
+12
View File
@@ -273,6 +273,18 @@ class GoatCounterPaging(unittest.TestCase):
self.assertEqual(got, {f"/p{n}": n + 1 for n in range(250)})
self.assertGreater(server.calls, 4)
def test_busy_days_pay_the_overflow_once(self) -> None:
# 150 paths every day, at most 100 a window: each day takes two half
# days of one page each, and only the first day is read whole first.
hourly = {(f"/d{day}p{n}", self.START + dt.timedelta(days=day, hours=n % 24)): 1
for day in range(28) for n in range(150)}
server = FakeGoatCounter(hourly)
with mock.patch.object(insights, "GOAT_MAX_EXCLUDED", 100):
got = self.hits(server)
self.assertEqual(len(got), 28 * 150)
self.assertEqual(sum(got.values()), 28 * 150)
self.assertEqual(server.calls, 1 + 28 * 2)
def test_a_long_rate_limit_ends_the_run_instead_of_sleeping(self) -> None:
headers = email.message.Message()
headers["X-Rate-Limit-Reset"] = "2355"