Function spawn_refresher
pub fn spawn_refresher(config: Arc<RwLock<Config>>)Expand description
Spawn the background price refresher, once per process.
No-op when no provider currently sets live_pricing = true: zero network,
zero task; the cost path keeps reading an empty snapshot. This cheap
pre-check runs before claiming the once-per-process guard, so a later
/admin/reload (or a re-start_channels) that newly enables a provider can
still start the refresher. Idempotent: a second concurrent caller (e.g. the
gateway after the channels supervisor, in a combined process) returns at the
guard without building any provider handles.
Every call re-binds CONFIG_HANDLE before anything else, and the running
task re-resolves it each cycle, so a daemon reload (which re-instantiates
the config Arc and re-runs both call sites) re-points the refresher at
the current config, and toggling live_pricing on a provider (or changing
its model/endpoint) is honored on the next refresh without a restart. Each
cycle rebuilds the per-gateway poll set via the normal factory path
(reusing each provider’s configured base_url, credentials, and options),
fetches one /models per gateway, and fills only the flagged models,
falling back to the models.dev catalog for models a gateway doesn’t price
(or providers with no HTTP listing, e.g. the kilocli subprocess gateway).
A fetch error for one source keeps the previous snapshot rather than
regressing good prices to empty; disabling live_pricing on the last
flagged provider instead clears the snapshot on the next cycle, so stale
prices stop filling after an opt-out.