LlmCall.Application (llm_call v0.4.0)

Copy Markdown View Source

Starts the two Finch pools this library calls through.

There are two, because a protocol is a property of the transport and a pool picks one:

  • LlmCall.Finch.HTTP2 — one connection that multiplexes, plus keep-alive PINGs. Serves https://.
  • LlmCall.Finch.HTTP1 — size connections with no multiplexing. Serves http://.

A provider that says nothing is routed by its base_url scheme, which is what curl does: https:// negotiates HTTP/2 through ALPN, and http:// is HTTP/1.1. The split exists because HTTP/2 over plaintext is h2c — there is no ALPN, so Mint assumes prior knowledge and sends PRI * HTTP/2.0 to a server that is speaking HTTP/1 text.

A provider can override the choice with pool::

use LlmCall.Provider,
  base_url: "http://localhost:11434",
  auth: :none,
  codecs: [chat: LlmCall.Dialect.OpenAI.Chat],
  pool: :http1                      # or :http2

pool: [size: 200, count: 4]         # Finch's own keys, on top of the defaults

A host that wants its own pool starts it in its own supervision tree and passes http_opts: [name: MyPool] to a provider or client; these stay unused in that case. If either name is taken when this application starts, that pool is left alone.

Overriding the defaults

config :llm_call, :pools may override either pool by name, with Finch's own keys:

config :llm_call,
  pools: %{
    LlmCall.Finch.HTTP1 => [size: 200],
    LlmCall.Finch.HTTP2 => [count: 2]
  }

The library's own shape is the base and the configuration is merged onto it, so a host that only wants a higher HTTP/1 ceiling says just that. See h1_pool_opts/0 and h2_pool_opts/0 for what the base is.

Why these numbers

Capacity is a property of a deployment, not of a library, so neither pool is sized generously. What is a library's business is the shape: HTTP/2 ignores :size and only :count adds connections, while HTTP/1 has no multiplexing at all, so size is a hard ceiling.

On the HTTP/2 pool the keep-alive PING is the one setting here that changes behaviour rather than capacity. A single long-lived connection that a NAT or load balancer drops without a RST leaves a corpse that the next request discovers; Finch sends the PING once the connection has been idle this long, and a write onto a dead socket moves the pool to :disconnected so it reconnects before a request lands.

Summary

Functions

Merges override onto base, recursing into nested sections.

Returns the children to start for one pool name, or none when it is registered.

The keep-alive ping_interval this library sets on its HTTP/2 pool.

The connection or shard count this library starts with.

The HTTP/1 connection ceiling this library starts with.

The name of the HTTP/1 pool this library starts.

The pool configuration this library gives an HTTP/1 origin.

The name of the HTTP/2 pool this library starts.

The pool configuration this library gives an HTTP/2 origin.

The name of the pool an origin is served by when a provider declares none.

The pool configuration for the origin of base_url.

The pool options a named pool gets after config :llm_call, :pools is applied.

Functions

deep_merge(base, override)

@spec deep_merge(keyword() | map(), keyword() | map()) :: keyword()

Merges override onto base, recursing into nested sections.

A plain Keyword.merge/2 would replace http2: [ping_interval: ...] wholesale when a host sets only http2: [max_connection_age: ...], losing the rest. Both inputs may be a keyword list or a map; the result is a keyword list, which is what Finch.start_link/1 and Finch.start_pool/3 take.

Public because LlmCall.Provider merges a pool: declaration onto the same shape.

default_children(pool)

@spec default_children(atom()) :: {Finch, keyword()} | []

Returns the children to start for one pool name, or none when it is registered.

Registration is checked here because a Finch child that finds the name taken returns {:error, {:already_started, pid}}, which would fail the whole application start rather than yield to the existing pool. The check is not atomic with Finch's own registration; a pool started in the same instant by another supervision tree is the one case this cannot order.

default_ping_interval()

@spec default_ping_interval() :: pos_integer()

The keep-alive ping_interval this library sets on its HTTP/2 pool.

Exposed so a host can compare against its own value instead of copying a number.

default_pool_count()

@spec default_pool_count() :: pos_integer()

The connection or shard count this library starts with.

default_pool_size()

@spec default_pool_size() :: pos_integer()

The HTTP/1 connection ceiling this library starts with.

h1_pool_name()

@spec h1_pool_name() :: atom()

The name of the HTTP/1 pool this library starts.

h1_pool_opts()

@spec h1_pool_opts() :: keyword()

The pool configuration this library gives an HTTP/1 origin.

HTTP/1 has no multiplexing, so size is a hard ceiling on concurrent requests and count shards it.

h2_pool_name()

@spec h2_pool_name() :: atom()

The name of the HTTP/2 pool this library starts.

h2_pool_opts()

@spec h2_pool_opts() :: keyword()

The pool configuration this library gives an HTTP/2 origin.

size is ignored on HTTP/2 — one connection multiplexes streams — so count is what adds connections. The keep-alive PING is what makes an idle connection's death visible.

pool_name_for(base_url)

@spec pool_name_for(String.t() | URI.t()) :: atom()

The name of the pool an origin is served by when a provider declares none.

pool_opts_for(base_url)

@spec pool_opts_for(String.t() | URI.t()) :: keyword()

The pool configuration for the origin of base_url.

https:// gets the HTTP/2 shape and everything else the HTTP/1 one. Only https counts as secure: http:// has no ALPN, so HTTP/2 there is h2c prior knowledge.

resolved_pool_opts(name)

@spec resolved_pool_opts(atom()) :: keyword()

The pool options a named pool gets after config :llm_call, :pools is applied.

The library's shape is the base and the configuration is merged onto it, so this is what default_children/0 hands Finch. Exposed because a host debugging its tuning wants to see the resolved value without reading a running pool's internals.