Skip to content

Use keep-alive #2357

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

Merged
merged 1 commit into from
Mar 8, 2025
Merged

Use keep-alive #2357

merged 1 commit into from
Mar 8, 2025

Conversation

DL6ER
Copy link
Member

@DL6ER DL6ER commented Mar 8, 2025

What does this implement/fix?

Use HTTP/1.1 keep-alive to allows clients to reuse TCP connection for subsequent HTTP requests, which can improve performance dramatically as no further TLS handshakes and connection opening/closing are required. This reduces pressure especially on low-end hardware lacking hardware-crypto for TLS.

This is disabled by default in CivetWeb as it puts additional compliance constraints on the request handlers. However, Pi-hole's API already fully complies with these additional constraints so no other changes than enabling this are needed.

We set a default timeout of 5 seconds after which the server closes the ready-to-be-used-again connections to free them for other requests. We intentionally keep such a long timeout so that requests being fired on the background (e.g. the regular update of GET /api/summary) can benefit here as well without needing additional handshakes.

Note that, while this reduces the overhead for opening and closing connections when loading several resources from one server, it also blocks one port and one thread at the server during the lifetime of this connection. Unfortunately, most browsers do not seem to close the keep-alive connection after loading all resources required to show a website. The server closes a keep-alive connection, if there is no additional request from the client during this timeout. If there are really many clients using the webserver at the same time, this may require you to increase webserver.threads when you see timeouts.

Note

Don't get me wrong here. This does not reduce the number of TLS handshakes from, say, 35 to 1. It just allows already established connections to be reused. If the web interface makes five requests exactly parallel, there will be five handshakes.
However, if requests happens sequentially, the total number of handshakes is drastically reduced (see TL;DR below). Even on my fairly fast x86_64 microserver running Pi-hole the speed enhancement was noticeable.

TL;DR: On development, we see roughly 70 individual TLS handshakes but only six on this branch.

Before this PR

Each connection needed a connection-establishment incl. a TLS handshake on its own.

Screenshot from 2025-03-08 09-51-30
Screenshot from 2025-03-08 09-50-59

After this PR

Only the first connection needs the full handshake, following connections can reuse the existing connection without having to do any additional handshaking on their own.

Screenshot from 2025-03-08 09-52-09
Screenshot from 2025-03-08 09-51-27


Related issue or feature (if applicable): N/A

Pull request in docs with documentation (if applicable): N/A


By submitting this pull request, I confirm the following:

  1. I have read and understood the contributors guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented my proposed changes within the code.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the EUPL 1.2 license
  5. I have squashed any insignificant commits. (git rebase)

Checklist:

  • The code change is tested and works locally.
  • I based my code and PRs against the repositories developmental branch.
  • I signed off all commits. Pi-hole enforces the DCO for all contributions
  • I signed all my commits. Pi-hole requires signatures to verify authorship
  • I have read the above and my PR is ready for review.

… subsequent HTTP requests, which improves performance. This is disabled by default in CivetWeb as it puts additional compliance constraints on the request handlers. However, Pi-hole's API already fully complies with these additional constraints so no other changes than enabling this are needed.

Signed-off-by: DL6ER <[email protected]>
Copy link
Member

@PromoFaux PromoFaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played with this option when I was trying to hunt down slowness issues after initial release - I never got around to investigating it fully, though. LGTM

Copy link
Member

@PromoFaux PromoFaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played with this option when I was trying to hunt down slowness issues after initial release - I never got around to investigating it fully, though. LGTM

@XhmikosR
Copy link

XhmikosR commented Mar 8, 2025

Confirmed! I was right to think something was missing :)

@DL6ER DL6ER merged commit 52a0ac7 into development Mar 8, 2025
18 checks passed
@DL6ER DL6ER deleted the new/keep-alive branch March 8, 2025 18:17
@PromoFaux PromoFaux mentioned this pull request Mar 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants