You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using the default way of starting the server with start_http_server(). I noticed that the server responds with HTTP/1.0 to HTTP/1.1 requests. Is there a flag I need to set to support HTTP/1.1?
@jvperrin great find, thank you! This one change makes a huge difference in performance. With my simple test exporter that just exposes 1 metric, scraping from Grafana Alloy, the CPU utilization and network traffic of the Alloy container are both reduced by about 55%. If the exporter returns a large number of metrics then the difference would be less noticeable.
I'd like to fix this and wanted to check the approach first. The workaround above only changes the status line: wsgiref still closes after one request, now without Connection: close, so clients that reuse the connection fail.
Proposal, only in the private handler classes in exposition.py: answer HTTP/1.1 and keep the connection open only for HTTP/1.1 requests whose response has a Content-Length. HTTP/1.0, Connection: close, requests with a body, HEAD, and any response without a length (e.g. streamed output, the concern in #299) get Connection: close as today. Idle connections close after 5 minutes, so a client that disappears can't hold a thread and fd forever. MetricsHandler and the public API are unchanged. It saves a TCP (and TLS) handshake per scrape.
Would you take a PR along these lines? @csmarchbanks
Activity
jvperrin commentedon Sep 28, 2023
In case anyone else comes across this and wants to change the version, it's coming from
wsgiref.handlersin the python stdlib: https://github.com/python/cpython/blob/3.11/Lib/wsgiref/handlers.py#L104You can change to using
1.1by doing this:Then later on when you call
start_http_server(8000)(or similar) it will use this HTTP version and respond withHTTP/1.1 200 OKin responses.llamafilm commentedon Apr 4, 2025
@jvperrin great find, thank you! This one change makes a huge difference in performance. With my simple test exporter that just exposes 1 metric, scraping from Grafana Alloy, the CPU utilization and network traffic of the Alloy container are both reduced by about 55%. If the exporter returns a large number of metrics then the difference would be less noticeable.
xKrishnaSaxena commentedon Nov 29, 2026
I'd like to fix this and wanted to check the approach first. The workaround above only changes the status line: wsgiref still closes after one request, now without
Connection: close, so clients that reuse the connection fail.Proposal, only in the private handler classes in
exposition.py: answer HTTP/1.1 and keep the connection open only for HTTP/1.1 requests whose response has aContent-Length. HTTP/1.0,Connection: close, requests with a body, HEAD, and any response without a length (e.g. streamed output, the concern in #299) getConnection: closeas today. Idle connections close after 5 minutes, so a client that disappears can't hold a thread and fd forever.MetricsHandlerand the public API are unchanged. It saves a TCP (and TLS) handshake per scrape.Would you take a PR along these lines? @csmarchbanks