Repository navigation
Improvements for httpx2.Client.sse timeouts #1177
Replies: 3 comments
|
You are not missing anything obvious here. There are two separate layers involved:
So a ping comment should still reset the read timer if the bytes actually arrive before the deadline, even though the parser yields no event for it. If the server can stay completely silent for longer than 5 seconds, configure a longer read timeout or disable just the read timeout for the SSE request, for example: timeout = httpx2.Timeout(10.0, read=None)(or use a suitably large |
|
Thanks for the confirmation. Your proposal is exactly what I ended up doing in my implementation - I believe that |
|
Thanks for confirming the read=None workaround. I checked the current sync and async paths: SSE comment lines are discarded only after response bytes have passed through the transport. Each socket read applies the read timeout to that individual receive, so a ping comment that actually arrives before the deadline already resets it. If you still see a timeout with pings sent more often than the read interval, buffering by the server or a proxy would be worth checking. Making read=None the SSE default is a separate API choice for the maintainers; your current per-request override keeps the connection and other timeout settings explicit. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
When using Server-Sent Events, the timeout parameter defaults
USE_CLIENT_DEFAULTwhich defaults to the httpx2 default ofDEFAULT_TIMEOUT_CONFIG(Timeout(timeout=5.0)at time of writing). The current implementation seems to reset the timeout any time a real SSE is read (I had trouble finding the actual implementation here including where the "timed out" message gets printed from).My understanding is that
pingevents/messages in SSEs are generally implemented as "comments" and are ignored per the specand are ignored due to this block
httpx2/src/httpx2/httpx2/_sse.py
Lines 87 to 88 in 4bb6753
The effect of this is that clients using default parameters will disconnect from SSE streams any time there's not a real message every 5 seconds.
I'm implementing both an SSE producer (sse-starlette cause I'm fastapi version limited) and SSE consumer (httpx2). Is there a best practice that I'm not aware of to send "real" messages frequently? If not, I'd like to discuss the possibility of handling SSE timeouts differently in httpx2 either by potentially letting comment/ping messages reset the timeout or by setting a higher read timeout by default.
Other than this, SSEs in httpx2 have been really easy to work with and I'm really grateful for the work done on this fork to modernize httpx.
All reactions