Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,50 @@ policies are unchanged.
`GET /api/v3/plugins/fetch-stats`.
- `fetch_service` is a core config section (`src/core_config_keys.py`).

### Shared fetch service (stage 2: one scoreboard cache key, a max-age response cache)

- **One cache key per ESPN scoreboard.** `espn_scoreboard_cache_key(sport,
league, dates)` in `src/common/espn_dates.py` names a scoreboard by ESPN's
own path (`football`/`nfl`) and its `dates=` value, so every consumer of
the same scoreboard shares one cached copy. Before, odds-ticker cached it as
`scoreboard_data_{sport}_{league}_{date}`, `APIHelper` as
`espn_{sport}_{league}_{date}` and the scoreboards as
`{sport_key}_schedule_{window}`.
- **Cache-through helpers.** `get_espn_scoreboard()` returns a cached copy at
most `max_age` seconds old and otherwise fetches with
`fetch_espn_scoreboard` and caches the result; `read_espn_scoreboard_cache()`
and `store_espn_scoreboard_cache()` are the two halves (the read takes an
`accept(data, age)` predicate, e.g. a shorter limit for a payload holding a
live game). A read checks the
record's own timestamp, so a writer's stored ttl can no longer make a
reader take data older than its own TTL; the shared entry stores no ttl.
Old keys are passed as `legacy_keys` and read after the canonical one for
one release, so an upgrade does not refetch every league at once.
- **Core callers use the key.** `APIHelper.fetch_espn_scoreboard` caches
under it by default (an explicit `cache_key` still works as before; the old
default key is read as a fallback). `SportsFetchMixin` gains
`_schedule_cache_key()` and `_cached_schedule()` for the scoreboards'
schedule windows (the same read as before, with the old key as fallback,
and a miss deletes the previous day's copy of a sliding window), and
`_fetch_season_directly(cache_key=None)` uses the canonical key. The
scoreboards and odds-ticker move to it in a plugins release that requires
this core.
- **Response cache.** A `200` with `Cache-Control: max-age=N` (minus `Age`)
answers an identical GET for N seconds without a request; ESPN sends no
validators, only max-age (1 to ~500 s, measured 2026-10-02). A caller says
how old a response it accepts with `fetch_get(..., cache_max_age=)` /
`fetch_espn_scoreboard(..., cache_max_age=)`; one that does not say gets at
most 30 s (`fetch_service.response_cache.default_max_age`).
`BaseOddsManager.get_odds` passes its update interval, `APIHelper.get` its
`cache_ttl`, and `get_espn_scoreboard` its `max_age`. `no-store`,
`no-cache`, `private`, `Vary: *` and `Set-Cookie` responses are never kept.
Bounded: 64 entries, 6 MB, 2 MB each; never longer than 10 minutes.
- **Counters.** `memo_hits` (answered by the response cache), `cache_hits`
(scoreboard fetches answered by a shared cache entry) and
`legacy_cache_hits` (reads from a pre-stage-2 key), per plugin and per
host, and the response cache's size, in `GET /api/v3/plugins/fetch-stats`.
- No new module; every change is additive to existing signatures.

### Control socket (stage 2: wake-ups, brightness, plugin reload)

- **Socket commands land at once.** Stage 1's socket was no faster than the
Expand Down
44 changes: 42 additions & 2 deletions docs/PLUGIN_API_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1054,8 +1054,16 @@ values, exceptions and retries are what they were.
next identical request revalidates, and a `304 Not Modified` comes back to
your code as the original `200` with its body. ESPN currently sends
neither, so this does nothing there.
- **Response cache.** A response whose server says `Cache-Control:
max-age=N` answers an identical GET for those N seconds without a
request (ESPN sends 1 to ~500 s). It never hands you a response older
than you accept: pass `cache_max_age=<your TTL>` to `fetch_get()` or
`fetch_espn_scoreboard()` (0 always asks the network); without it a
response is reused for at most 30 seconds.
- **Counters.** Requests, merged requests, bytes, 304s, errors and time spent
waiting are counted per plugin and per host, and published for the web UI
waiting, and requests answered without the network (`memo_hits` from the
response cache, `cache_hits` from a shared scoreboard cache entry), are
counted per plugin and per host, and published for the web UI
at `GET /api/v3/plugins/fetch-stats` (see
[REST_API_REFERENCE.md](REST_API_REFERENCE.md#get-fetch-statistics)). A
request is counted against your plugin when it runs inside your
Expand All @@ -1066,6 +1074,36 @@ What is not covered yet: requests a plugin makes with its own `requests.get()`
or `Session.get()` calls. They work as before but are invisible to the
budgets and counters.

### One cache key per ESPN scoreboard

Cache an ESPN scoreboard under `espn_scoreboard_cache_key(sport, league,
dates)` (`src.common.espn_dates`), not a key of your own, so every plugin
showing that league shares one fetch and one cached copy. `sport` and
`league` are ESPN's path segments (`football`, `college-football`), and
`dates` is what you send as `dates=` (`"20261004"`, `"202610"`,
`"20260925-20261016"`, a `date`, or `None` for the undated scoreboard).

```python
from src.common.espn_dates import get_espn_scoreboard

data = get_espn_scoreboard(
self.session, "football", "nfl", "20261004",
cache_manager=self.cache_manager,
max_age=300, # your TTL: nothing older comes back
legacy_keys=["my_old_key_20261004"], # read once while upgrading
)
```

`get_espn_scoreboard` returns a cached copy at most `max_age` seconds old,
whoever wrote it, and otherwise fetches with `fetch_espn_scoreboard`
(`limit=500`, ranges split the way ESPN requires) and caches the result
without a ttl, so each reader applies its own age limit. `max_age=0` always
fetches but still leaves the copy for others. For a two-step read, use
`read_espn_scoreboard_cache()` and `store_espn_scoreboard_cache()` around
your own fetch. Scoreboards built on `SportsFetchMixin` get
`_schedule_cache_key(datestring)` and `_cached_schedule(key, legacy_keys)`
for their schedule windows. All of this is in the core release after 3.8.0.

The settings live in `config.json` under `fetch_service`, read when the
display starts and on a config reload:

Expand All @@ -1084,7 +1122,9 @@ display starts and on a config reload:
the bare domain); `"per_second": 0` removes a budget. `"enabled": false`
turns the whole service into a plain `session.get()`. Two further switches,
`"single_flight": false` and `"conditional_get": false`, turn off merging and
revalidation.
revalidation. `"response_cache": {"enabled": false}` turns off the response
cache; its `default_max_age` (30) is the limit for callers that pass no
`cache_max_age`.

---

Expand Down
17 changes: 15 additions & 2 deletions docs/REST_API_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,8 @@ or `unknown` (nothing published; `data.data` is `null`).
"totals": {"requests": 412, "merged": 3, "not_modified": 0,
"errors": 1, "http_errors": 2, "retries": 0,
"throttled": 0, "overruns": 0, "bytes": 18234011,
"wait_seconds": 0.0},
"wait_seconds": 0.0, "memo_hits": 21, "cache_hits": 40,
"legacy_cache_hits": 2},
"plugins": {
"football-scoreboard": {"requests": 240, "merged": 2, "bytes": 9120330,
"hosts": {"site.api.espn.com": 180,
Expand All @@ -1059,9 +1060,11 @@ or `unknown` (nothing published; `data.data` is `null`).
"site.api.espn.com": {"requests": 301, "...": "as in totals"}
},
"validators": {"entries": 0, "bytes": 0},
"response_cache": {"entries": 3, "bytes": 412004},
"config": {"enabled": true, "single_flight": true,
"conditional_get": true, "max_wait_seconds": 2.0,
"rate_limits": {"*.espn.com": {"per_second": 20.0, "burst": 200.0}}}
"rate_limits": {"*.espn.com": {"per_second": 20.0, "burst": 200.0}},
"response_cache": true, "default_max_age": 30.0}
}
}
}
Expand All @@ -1073,6 +1076,16 @@ flight, `not_modified` 304s served from the stored body, `errors` transport
failures and `http_errors` responses with status 400 or above. `bytes` is the
decoded body size. `core` is everything no plugin made.

Three counters are requests that never reached the network: `memo_hits`
were answered from the short response cache (a response still inside the
`Cache-Control: max-age` its server gave it), and `cache_hits` were
scoreboard fetches answered from a shared ESPN scoreboard cache entry
(`espn_scoreboard_cache_key`). `legacy_cache_hits` counts reads served from a
key that predates the shared one; it should fall to zero within a day of an
upgrade. A plugin's `hosts` counts are requests plus merged requests,
`memo_hits` and `cache_hits`: everything it asked for.
`response_cache` is the size of the response cache now.

### Get/Set Plugin Limits

**GET** `/api/v3/plugins/limits/<plugin_id>`
Expand Down
5 changes: 4 additions & 1 deletion src/base_odds_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ def get_odds(self, sport: str | None, league: str | None, event_id: str,
url = f"{self.base_url}/{sport}/leagues/{espn_league}/events/{event_id}/competitions/{event_id}/odds"
self.logger.debug(f"Requesting odds from URL: {url}")

response = fetch_get(self.session, url, timeout=self.request_timeout)
# The response cache may answer only inside this caller's own
# interval, the age at which its cached odds expire anyway.
response = fetch_get(self.session, url, timeout=self.request_timeout,
cache_max_age=interval)
response.raise_for_status()
raw_data = response.json()

Expand Down
59 changes: 46 additions & 13 deletions src/common/api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
import time
from datetime import datetime
from types import MappingProxyType
from src.common.espn_dates import ESPN_MAX_LIMIT
from src.common.espn_dates import (
ESPN_MAX_LIMIT,
espn_scoreboard_cache_key,
read_espn_scoreboard_cache,
store_espn_scoreboard_cache,
)
from src.common.fetch_service import fetch_get, fetch_post, share_connection_pool
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, cast

Expand Down Expand Up @@ -117,6 +122,14 @@ def get(self, url: str, params: Optional[Dict] = None,
Returns:
Response data as dictionary or None if request fails
"""
return self._get(url, params, headers, timeout, cache_key, cache_ttl,
cache_ttl if cache_key else None)

def _get(self, url: str, params: Optional[Dict], headers: Optional[Dict],
timeout: Optional[int], cache_key: Optional[str], cache_ttl: int,
cache_max_age: Optional[float]) -> Optional[Dict]:
""":meth:`get`, saying how old a response the fetch service's short
response cache may hand back (``cache_max_age``, the caller's TTL)."""
if cache_key and self.cache_manager:
cached = self._get_from_cache(cache_key, cache_ttl)
if cached is not None:
Expand All @@ -138,7 +151,8 @@ def get(self, url: str, params: Optional[Dict] = None,
url,
params=params,
headers=request_headers,
timeout=timeout or self.default_timeout
timeout=timeout or self.default_timeout,
cache_max_age=cache_max_age,
)
response.raise_for_status()

Expand Down Expand Up @@ -167,31 +181,50 @@ def fetch_espn_scoreboard(self, sport: str, league: str,
sport: Sport name (e.g., 'basketball', 'football')
league: League name (e.g., 'nba', 'nfl')
date: Date in YYYYMMDD format (defaults to today)
cache_key: Cache key for response
cache_ttl: Cache time-to-live in seconds

cache_key: Cache key for response. By default the canonical
``espn_scoreboard_cache_key(sport, league, date)``, shared
with every other consumer of this scoreboard, with the key
this used before (``espn_{sport}_{league}_{date}``) read as a
fallback for one release. An explicit key works as before.
cache_ttl: Cache time-to-live in seconds. A shared entry is
returned only while it is at most this old.

Returns:
ESPN API response data or None if request fails
"""
if date is None:
date = datetime.now().strftime('%Y%m%d')

# Build URL
url = f"https://site.api.espn.com/apis/site/v2/sports/{sport}/{league}/scoreboard"

# Build cache key if not provided
if cache_key is None:
cache_key = f"espn_{sport}_{league}_{date}"


# Set parameters
# limit above 500 makes ESPN truncate instead of erroring: college
# football came back with 25 of 68 games. See src/common/espn_dates.py.
params = {
'dates': date,
'limit': ESPN_MAX_LIMIT
}

return self.get(url, params=params, cache_key=cache_key, cache_ttl=cache_ttl)

if cache_key is not None:
return self.get(url, params=params, cache_key=cache_key, cache_ttl=cache_ttl)

legacy_key = f"espn_{sport}_{league}_{date}"
try:
shared_key = espn_scoreboard_cache_key(sport, league, date)
except ValueError:
# Not a path or date the canonical key covers: the old key.
return self.get(url, params=params, cache_key=legacy_key, cache_ttl=cache_ttl)
if self.cache_manager:
cached = read_espn_scoreboard_cache(
self.cache_manager, shared_key, cache_ttl, legacy_keys=(legacy_key,))
if cached is not None:
self.logger.debug(f"Using cached response for {shared_key}")
return cast(Dict[Any, Any], cached)
data = self._get(url, params, None, None, None, cache_ttl, cache_ttl)
if data is not None and self.cache_manager:
store_espn_scoreboard_cache(self.cache_manager, shared_key, data)
return data

def fetch_espn_standings(self, sport: str, league: str,
cache_key: Optional[str] = None,
Expand Down
Loading
Loading