Skip to content
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

High Latency on First Request to localhost #544

Open
julmb opened this issue Sep 5, 2024 · 1 comment
Open

High Latency on First Request to localhost #544

julmb opened this issue Sep 5, 2024 · 1 comment

Comments

@julmb
Copy link

julmb commented Sep 5, 2024

I have this code:

main :: IO ()
main = do
    request <- parseRequest "http://localhost:3000/test"
    getCurrentTime >>= print
    httpBS request >>= T.putStr . T.decodeUtf8Lenient . getResponseBody
    getCurrentTime >>= print
    httpBS request >>= T.putStr . T.decodeUtf8Lenient . getResponseBody
    getCurrentTime >>= print

With a simple servant service at http://localhost:3000/test, I get:

2024-09-05 03:47:47.018037777 UTC
Hello, world!
2024-09-05 03:47:47.271253156 UTC
Hello, world!
2024-09-05 03:47:47.272148168 UTC

Given that this is all local, I would have expected these requests to finish very fast. However, the first request takes about 250ms, only subsequent ones are fast.

I did some investigating, and it seems that getAddrInfo returns [::1]:3000 and 127.0.0.1:3000, in that order. The function firstSuccessful then attempts to connect to each of these, staggered by 250ms, which only succeeds for the second address.

Not sure if there is an easy/elegant solution to this, but it is very unfortunate that a simple connection to localhost incurs such a high latency on the first request.

@julmb
Copy link
Author

julmb commented Oct 5, 2024

For what it's worth, this is how curl handles a connection to localhost:3000:

* Host localhost:3000 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:3000...
* connect to ::1 port 3000 from ::1 port 49152 failed: Connection refused
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000

It looks like it also tries to connect to the IPv6 address first, but instead of waiting 250ms, it seems to immediately try the IPv4 address once the connection is refused by the IPv6 address. The whole exchange takes less than 5ms.

From the comment above the function firstSuccessful, it seems like it is using the staggered approach to comply with RFC 8305. I am not sure if trying the next address immediately after the previous address has been refused would also be compliant or a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant