foley.sources.music_library.fetch#
Fetching a library’s files politely, through foley’s transport and its resilience layer.
A library is someone’s website, and a long run (63 tracks against the Wayback
Machine) meets 429 Too Many Requests. So the default fetch goes through a
Transport (default
requests_transport(), which imports requests on its
first call only) wrapped, per host, by
foley.sources.resilience.resilient(): a pace from MUSIC_LIBRARY_HTTP’s
rate (2 s between two requests to one host), backoff on 429 / 5xx that
waits what a Retry-After header asks up to the config’s retry.max_delay_s, a
longer Retry-After given up at once
(RetryAfterTooLong: the next mirror is tried
instead of waiting the cap again and again), and a circuit breaker. Each host has
its own pace and breaker, so falling back to the archive spends no pause, and the
library’s site failing does not open the archive’s breaker. The runtime’s
http_resilience switch turns the wrapping off, as it does for every source.
fetch_audio() refuses what is not a track: a non-200 answer, a text page (an
error page served with 200), and a body over max_bytes (by its
Content-Length before the body is read, and by its length after).
A config is laid over MUSIC_LIBRARY_HTTP (music_library_config()), so
a caller states only what it changes.
Module Attributes
how |
|
How a music library is fetched (the keys a source config's resilience layer reads, plus the request's own). |
Functions
|
|
|
The |
|
The default |
|
|
|
|
- foley.sources.music_library.fetch.MUSIC_LIBRARY_HTTP: dict = {'data_egress': 'external', 'max_bytes': 67108864, 'name': 'music_library', 'pricing': {'unit': 'free'}, 'rate': {'per_min': 30}, 'retry': {'give_up_over_max_delay': True, 'max_delay_s': 120.0}, 'user_agent': 'foley-music-library (+https://github.com/reeleehq/foley)'}#
How a music library is fetched (the keys a source config’s resilience layer reads, plus the request’s own).
rate: one request per 2 s to one host (a library is someone’s website);retry.max_delay_s: the longestRetry-Afterhonoured (a longer one moves on to the next mirror);pricing: free, so a GET is re-sent after a dropped connection.
- foley.sources.music_library.fetch.fetch_audio(url, *, http, config=None)[source]#
url’s bytes, asked for with the config’s user agent.A body over
max_bytesis refused by its declaredContent-Lengthbeforeresp.contentis touched, and by its length after (a server may declare none, or lie). foley’sTransportreturns a whole response, so withrequests_transport()the body is already in memory when the header is read: the header check spares the copy, not the download. A streaming transport, which would stop atmax_bytes, is a follow-up.- Raises:
MusicLibraryError – On a non-200 answer, a
text/*page (an error page served with 200), a body overconfig['max_bytes'], or one shorter than its declaredContent-Length(a cut download, which a probe of its header alone would still accept).- Return type:
- foley.sources.music_library.fetch.library_fetch(*, fetch=None, transport=None, config=None, what='music library', clock=<built-in function monotonic>, sleep=<built-in function sleep>)[source]#
The
url -> bytesa library’s files are fetched with (one rule for every caller).A
fetchof the caller’s own is paced per host by the config’srate(pace_per_host()). Otherwise the call’s egress is checked (foley.runtime.require_egress(), sofoley.offline()refuses it, namingwhat) andmake_fetch()goes throughtransport.- Raises:
ValueError – When both
fetchandtransportare given.EgressBlocked – When the HTTP path is refused by an offline scope.
- Return type:
- foley.sources.music_library.fetch.make_fetch(*, transport=None, config=None, clock=<built-in function monotonic>, sleep=<built-in function sleep>)[source]#
The default
url -> bytes:fetch_audio()overpolite_transport()(configlaid overMUSIC_LIBRARY_HTTP).
- foley.sources.music_library.fetch.music_library_config(config=None)[source]#
configlaid overMUSIC_LIBRARY_HTTP(top-level keys: aretryorrategiven replaces the default one whole).>>> music_library_config({"rate": {"per_min": 6}})["rate"], music_library_config()["max_bytes"] ({'per_min': 6}, 67108864)
- Return type:
- foley.sources.music_library.fetch.polite_transport(transport=None, *, config=None, clock=<built-in function monotonic>, sleep=<built-in function sleep>)[source]#
transport(defaultrequests_transport()) with a resilient wrapper per host, built fromconfig(see the module docstring).Left unwrapped when the active runtime turns
http_resilienceoff.- Return type: