Corrects an origin's pool when its host turns out not to speak HTTP/2.
Clients are routed by scheme: https:// gets the HTTP/2 pool (LlmCall.Finch.HTTP2) and
http:// gets the HTTP/1 one (LlmCall.Finch.HTTP1), which is curl's rule. A plaintext
origin therefore never needs this module.
What it cannot reach is a https:// host whose ALPN offers only http/1.1. Finch fixes
a pool's module when the pool starts, so there is no per-connection negotiation: the
request fails with reason: :pool_not_available, and it fails every request, each
after waiting the pool's :pool_timeout. That is too expensive to repeat, so the
correction is made once and remembered.
Why this is not a retry
Nothing here re-sends a request. downgrade/2 registers an HTTP/1 pool for one origin
and returns; the caller decides whether to attempt the request again, and the second
attempt resolves to the corrected pool through Finch's own per-origin lookup. A
library-side retry would be a policy decision this library does not make (see
LlmCall.Error on retryable?).
What to do with the first failure
The request that discovered the mismatch still failed, and it is
reason: :pool_not_available, retryable?: true. The shape a caller wants:
case LlmCall.complete(client, request) do
{:error, %LlmCall.Error{reason: :pool_not_available}} ->
:ok = LlmCall.Protocol.downgrade(client.http_opts[:name], LlmCall.Protocol.origin_of(client))
# retry once; the origin now has an HTTP/1 pool
other ->
other
endorigin_of/1 is what makes that possible: base_url carries the endpoint's path, and
Finch keys a pool on scheme, host and port only.
A plaintext base_url never needs this: LlmCall.Application.pool_name_for/1 gives
http:// an HTTP/1 pool in the first place, because Mint's HTTP/2 over http:// assumes
prior knowledge and would send h2 frames to a server speaking HTTP/1 text.
It interrupts requests in flight
Correcting an origin means stopping its pool, and Finch.stop_pool/2 terminates the
connections under it. Any request open on that origin at that moment fails with
reason: :connection_process_went_down, and a stream that was mid-body ends there.
This is why the module is aimed at the failure it describes rather than at routine recovery: the origin is already failing every request, so nothing usable is lost. Calling it on a healthy origin — one that was simply misdescribed — kills real traffic. Call it where the mismatch is discovered, and not in a loop over a mixed set of origins.
An origin already served by HTTP/1 is left alone: the call returns :ok without stopping
anything, so a caller that corrects on every failed request does not restart a healthy
pool each time.
Summary
Functions
Registers an HTTP/1 pool for origin under the Finch instance pool.
The origin of a client's base_url — scheme, host and port, without the path.
Functions
@spec downgrade(atom(), String.t() | URI.t(), keyword()) :: :ok | {:error, {:unknown_pool, atom()}} | {:error, {:unknown_origin, String.t() | URI.t()}} | {:error, {:wrong_pool, term()}}
Registers an HTTP/1 pool for origin under the Finch instance pool.
origin is a URL or a scheme://host[:port] string; Finch resolves the request origin
from the URL it is about to call, so the pool key has to match that exactly. Returns
:ok when the origin ends up on an HTTP/1 pool, {:error, {:unknown_pool, name}} when
the Finch instance is not running, {:error, {:unknown_origin, origin}} when origin is
not an http(s) URL, or {:error, {:wrong_pool, term()}} when the correction did not take.
Options are LlmCall.Application.h1_pool_opts/0 unless overridden, so a corrected origin
gets a real HTTP/1 ceiling rather than inheriting the HTTP/2 settings, where size is
ignored.
An origin that is already on HTTP/1 is left alone and the call returns :ok, so
correcting on every failed request converges. Passing options is the exception: a pool's
configuration cannot be read back from Finch, so a request for a specific one always
replaces the pool — and therefore interrupts whatever is running on it (see the
moduledoc).
@spec origin_of(LlmCall.Client.t()) :: String.t()
The origin of a client's base_url — scheme, host and port, without the path.
This is the key Finch resolves a request against, so it is what downgrade/2 needs.
base_url is the endpoint (https://api.example.test/v1), and the vendor prefix is not
part of pool identity.