Documentation
¶
Overview ¶
Package proxy is the second inference flow: transparent forwarding of caller-supplied upstream credentials. Dispatched from the inference handler when X-WR-Proxy-Mode: Proxy is set.
Differences from app/pipeline:
- No Policy / Model / HostBinding resolution. The caller picks the upstream Host by slug via X-WR-Upstream-Host; the inference handler hands us the resolved BaseURL.
- No keypool, no per-key circuit breaker, no retry/failover. The caller's Authorization is the upstream credential; one attempt.
- An authed request holds its caller ladder's counters as normal mode does, with the inference-api-proxy system bucket as a further ceiling: bringing an upstream key must not lift the operator's limits. Anonymous requests hold only inference-api-proxy-anonymous, keyed by client IP.
- Same tee + detached post-flight pattern as app/pipeline: the post-flight goroutine commits the reservation and extracts tokens for usage logging. Never blocks the response.
The package is HTTP-aware only at the upstream-call boundary (one outbound http.Request). The inbound *http.Request is consumed by the handler; this package takes pre-resolved bytes + headers + the upstream Authorization value.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrLimiterMissing = errors.New("proxy: limiter not configured")
ErrLimiterMissing signals a request with counters reached a Pipeline with no limiter; refused rather than forwarded unmetered.
var ErrNoUpstreamAuth = errors.New("proxy: upstream authorization required")
ErrNoUpstreamAuth signals the handler forgot to populate UpstreamAuth.
Functions ¶
This section is empty.
Types ¶
type BodyError ¶ added in v0.11.7
BodyError is returned by a Request.Body reader that refuses the inbound body while it streams upstream, so the upstream received it incomplete. The failure is the caller's request, not the upstream; Kind names it in telemetry.
type Pipeline ¶
type Pipeline struct {
// Limiter holds and settles Request.Counters; required when a request has any.
Limiter limits.Limiter
Lifecycle *lifecycle.Registry
Client *http.Client
Logger *slog.Logger
// Settles shares a served request's commits with other requests' in one round trip on the post-flight store; nil settles each on its own.
Settles *settle.Queue
}
Pipeline is the orchestrator. Constructed once at boot; Run() is goroutine-safe.
func New ¶
New constructs a Pipeline with a sensible http.Client default. The client has no overall timeout — streaming responses can take minutes. Use net/http transport timeouts for hop-level safety instead.
lifecycle is optional; pass nil if post-flight observers aren't wired in this deployment (tests, minimal smoke).
type RefusingBody ¶ added in v0.11.7
type RefusingBody interface {
Refusal() *BodyError
}
RefusingBody is a Request.Body that can refuse the inbound body after the upstream has answered, when Run has already returned. Post-flight asks it: a refused request is recorded under the refusal's kind, not as a success; its reservation still settles with what the upstream reported, since the upstream served it.
type Request ¶
type Request struct {
// Method + Path are the inbound HTTP method and path appended to
// HostBaseURL when building the upstream URL. Path includes the
// leading slash (e.g. "/v1/messages").
Method string
Path string
// Body is the inbound request body. Streamed through to upstream
// verbatim; the proxy does not buffer it. Transport-layer code that
// wants to inspect the body (for usage stamping, deep validation,
// etc.) should ReadAll first and pass a reqbody.NewReader here — a
// *bytes.Reader stays reachable from the transport for the whole
// response. Run sets Body to nil once the upstream answers.
Body io.Reader
// ContentLength is the inbound request's Content-Length, applied to
// the upstream request explicitly because a non-seekable streamed Body
// gives http.NewRequest no length to infer — without it the forward
// degrades to chunked transfer. <= 0 means unknown.
ContentLength int64
// Headers is the inbound header set with relay-internal headers
// already stripped (X-WR-*, Cookie, hop-by-hop).
// Authorization is set separately via UpstreamAuth — do not include
// it here.
Headers http.Header
// HostBaseURL is the resolved upstream base URL from the Host row.
HostBaseURL string
// UpstreamAuth is the verbatim "Authorization" value the caller
// supplied (typically "Bearer …"). Forwarded as-is.
UpstreamAuth string
// Counters are what the request must fit: an authed caller's own counters and the proxy ceiling, or the anonymous ceiling alone. Empty disables limiting.
Counters []limits.Counter
// Pricing prices the tokens settled against budgets; nil when the request routed to no priced binding.
Pricing *pricing.Pricing
// Extractor parses the upstream response body for usage logging.
// nil disables extraction (post-flight still commits the reservation).
Extractor TokenExtractor
// ModelName is for logging only — proxy mode does not consult the
// catalog Model row.
ModelName string
// Lifecycle is the per-request shared context, constructed by the
// handler before Run. Post-flight observers see it via the registered
// lifecycle observers (Finalize). Optional — nil skips observer dispatch.
Lifecycle *lifecycle.Context
}
Request is the pre-resolved input to Run.
type ResponseSummarizer ¶ added in v0.11.7
ResponseSummarizer is the optional capability of a TokenExtractor that reads a response's canonical Summary, which then serves both the commit and the usage hook. false means it has no translator for the body.
type Result ¶
type Result struct {
Status int
Headers http.Header
// Body MUST be Closed by the caller. Closing triggers the post-
// flight goroutine (if it hasn't run already).
Body io.ReadCloser
// Abort closes the upstream response under a Body still being read, from any goroutine: the read fails, and post-flight still waits for Body's own Close, which the reader makes.
Abort func()
}
Result is what the handler streams back to the caller.
type TokenExtractor ¶
TokenExtractor extracts upstream usage from a response body. The inference handler picks the right extractor by inbound endpoint (OpenAI shape for /v1/chat/completions, Anthropic shape for /v1/messages) and passes it in.