-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] - Ability to setup TCP Socket timeout in workers #1018
Comments
Hi @dm161, This is a good idea, but would require a bit more than just a PR in workerd. On Cloudflare, TCP connections go through at outbound proxy which would need to be responsible for setting the timeout. We also need to decide things like what should happen if the connection is answered by another Worker (once Workers gain the ability to accept connections) and thus never actually crosses TCP -- is the timeout ignored or is it emulated? Somewhat related, do we want to express timeouts as literally just a time duration or do we want to provide control over the various low-level TCP keepalive knobs? (I would vote for just an interval, but not sure how complicated it may be to map to the low-level options.) cc'ing @irvinebroque to think about where this fits on the roadmap. |
thanks for chiming in @kentonv - I would assume there is no timeout as of right now when creating a TCP connection. From the sound of it might require extensive changes to the platform/infra. A solution I was thinking was to wrap the TCP connection into an AbortController in the Cloudflare worker and terminate the connection if a timeout is reached directly from the client code instead. Slightly hijacking this thread onto an adjacent topic, but still within workerd and network APIs: Where can I open an item for PerformanceResourceTiming support in Workers? Right now fetch is pretty limited if you want to inspect response timings and other breakdowns (DNS time, TTFB). Thanks again for the great work |
I'm curious if something as simple as a One challenge is that you cannot currently detect when the socket connection is established (as we lack an |
An opened promise would exactly be the right and optimal compromise for this. Can I help in any way on the codebase? My cpp is rusty but don’t think will be too difficult if it’s just a matter of bubbling it up to the ts code |
There are really two timeouts needed, I would think: (a) a time limit for establishing the connection and (b) an idle timeout that closes the connection if it remains idle for too long. For (a) something as simple as an // If the connection is not established within 10 seconds, abort the attempt
const s = connect('...', { signal: AbortSignal.timeout(10_000) }); The idle timeout case (b) is more difficult, and as @kentonv suggests requires deeper work in both workerd and the internal infrastructure. |
agreed @jasnell - keep similar API as fetch so we can propagate arbitrary cancellation signals. A bit like how you propagate wth Go context.Context |
True, we can match @dm161 if you want to give writing a PR a go: following how the
Why is that? Similarly to (a) wouldn't a naive approach of closing the socket when no data is received work well for most use cases? |
I suppose it could. Every bit of data transmitted or received could reset a timer. |
Fixed on the client side for now using setTimeout and Promise.race as follows
Still having |
One more vote for an |
|
It's not clear what the default TCP socket timeout is nor it's possible to specify one https://github.com/cloudflare/workerd/blob/main/src/cloudflare/internal/sockets.d.ts#L24
Would be awesome to have socketTimeout in the SocketOptions object.
If you point me to how to update documentation for default tcp timeout or point me how to add this I can send a PR
The text was updated successfully, but these errors were encountered: