fix(espn): fetch a window's edge months whole, cap chunk requests per process - #751
Open
ChuckBuilds wants to merge 1 commit into
Open
ChuckBuilds wants to merge 1 commit into
ChuckBuilds wants to merge 1 commit into
Conversation
… process A soccer board with eight leagues logged ~90 NameResolutionErrors for site.api.espn.com within a minute of every start on ledpi, plus "update() timed out". ESPN rejects date ranges, so every league's fortnight-either-side window became 29 day requests, several managers fetched it at once, and each window got its own six chunk threads: ~450 requests per start with ~45 in flight, each request past a session's pool a new connection and a new DNS lookup. - fetch_espn_date_chunks() asks for a partial edge month whole once the window covers ESPN_MONTH_COVER_MIN_DAYS (7) of its days, and trims the answer to the window by each event's US Eastern start date, the day ESPN's dates=YYYYMMDD means (417/417 live soccer events matched). A 29-day window over two months is 2 requests, not 29. A live poll's 1-2 days stay day requests. A capped trimmed month re-asks only the window's days; an undatable event is kept. New espn_request_chunks(). - Chunk requests share one process-wide cap of ESPN_CHUNK_WORKERS in flight instead of six per window. - A new process starts as if a range had just been rejected, so it no longer spends a doomed 400 per window at every start. Live ESPN, soccer-scoreboard 2.39.2, alternating runs: ~450 -> 46 requests per start, peak ~45 -> 13 in flight, ~75 -> 30 DNS lookups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 55 minutes. View limit detailsLimit details: You’ve used the included review currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (5)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| Security | 1 medium |
🟢 Metrics 33 complexity
Metric Results Complexity 33
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On ledpi (soccer-scoreboard 2.39.2, 8 leagues, core main), every start or reload logged ~90
NameResolutionErrors forsite.api.espn.comwithin a minute (89 from soccer) plusupdate() timed out after 30.0s. Over 6 hours soccer made 3,812 requests (807 throttled by the fetch service's token bucket, 410 retries, 198 s of rate-limit wait). DNS works otherwise; the burst overwhelms the Pi's resolver.Root cause
ESPN answers
dates=YYYYMMDD-YYYYMMDDwith 400, soespn_datesre-asks a range as whole months plus one request per day for partial edge months. A scoreboard's default window is a fortnight either side of today: 29 days across two partial months, so 29 day requests per league per fetch. Soccer fetches that window from several managers at once (the plugin half is in the plugins PR), and eachfetch_espn_date_chunks()call had its own pool of six threads, so ~40-50 requests were in flight. Every one past a session's connection pool opened a new connection, and each new connection meant a freshgetaddrinfo(Python has no DNS cache). On top of that, every window spent one doomed range request on the first fetch after start, because the "ranges are rejected" memo starts empty: 11 at once.Fix (core:
src/common/espn_dates.py)ESPN_MONTH_COVER_MIN_DAYS(7) or more days of a partial month, that month is oneYYYYMMrequest. The answer is trimmed to the window's days using each event's US Eastern start date, which is the day ESPN'sdates=YYYYMMDDmeans. I checked this against the live API on 2026-10-03: for 5 soccer leagues over Sep, Mar and Nov 2026 (Nov includes the end of DST), 417 of 417 events came back from the day query named by their Eastern date. A month query also matched the sum of its day queries exactly (eng.1 30/30, UCL 18/18, MLS 74/74, ger.1 27/27). A 29-day window now costs 2 requests, down from 29.espn_request_chunks().espn_date_chunks()is unchanged.BoundedSemaphore(ESPN_CHUNK_WORKERS)is held around every chunk request, so the limit is 6 in flight across all windows, not 6 per window._ranges_rejected_untilstarts atnow + RANGE_RETRY_SECONDS. The range is still retried 6 h in, so the workaround still retires itself if ESPN reverts.Trade-off: a whole month is about 2-3x the bytes of the half that a fortnight window holds (measured, decoded JSON: eng.1 0.34 → 0.71 MB, MLS 0.71 → 1.63 MB, college football 3.58 → 8.87 MB). In exchange, requests drop from 29 to 2, and the requests are what break the resolver. Trimming happens in the worker, so only window events are held.
Measurements (live ESPN, Windows desktop, 8 leagues = ledpi's config, cold cache, alternating order B C P F F P C B B F)
The harness builds
SoccerScoreboardPlugin, runs oneupdate(), then waits for the background service to go quiet. It countsHTTPAdapter.sendandsocket.getaddrinfocalls, with peak concurrency for each.update()Simulated hourly refresh (clock advanced 1 h,
update()again, 3 cycles): B 451-478 requests/h, F 20-24/h.Tests
test/test_espn_dates.py, new cases:test/test_fetch_service.py: the caller-identity chunk count now usesespn_request_chunks.origin/mainbaseline worktree: the sorted FAILED/ERROR lists are identical (62 failed / 6 errors, the known Windows set). Passing went from 8296 to 8310.scripts/check_types.py, mypy 1.20.2): same 2 pre-existingfetch_service.pyunused-ignore notes as on main;espn_dates.pyis clean.Companion
ChuckBuilds/ledmatrix-plugins#615 (soccer-scoreboard 2.39.3) removes the plugin's duplicate window fetches and shares the connection pool. Each PR helps on its own; together, a start drops from ~450 requests to 39.
Not merging; for review.
🤖 Generated with Claude Code