Skip to content

Tune httpx keep alives for polling integrations#95782

Merged
balloob merged 4 commits into
devfrom
keep_alive_polling
Jul 3, 2023
Merged

Tune httpx keep alives for polling integrations#95782
balloob merged 4 commits into
devfrom
keep_alive_polling

Conversation

@bdraco
Copy link
Copy Markdown
Member

@bdraco bdraco commented Jul 3, 2023

Proposed change

aiohttp closes the connection after 15s by default, and httpx closes the connection after 5s by default.

We have a lot of integrations that poll every 10-30s which create and tear down connections over and over.

Set the keep alive time to 15s for httpx to match aiohttp to maximize connection reuse and avoid tls negotiation overhead

The server side default for nginx is 75s https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout which means we likely close a lot of connections that could be reused with the defaults

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

aiohttp closes the connection after 15s by default,
and httpx closes the connection after 5s by default.

We have a lot of integrations that poll every 10-60s
which create and tear down connections over and over.

Set the keep alive time to 65s to maximize connection
reuse and avoid tls negotiation overhead
@home-assistant home-assistant Bot added cla-signed core small-pr PRs with less than 30 lines. labels Jul 3, 2023
@bdraco
Copy link
Copy Markdown
Member Author

bdraco commented Jul 3, 2023

Still testing with this, but I think we can go as low as 31s and still be good for 90% of the cases

Comment thread homeassistant/helpers/aiohttp_client.py Outdated
Comment thread homeassistant/helpers/aiohttp_client.py Outdated
Comment thread homeassistant/helpers/httpx_client.py Outdated
Comment thread homeassistant/helpers/httpx_client.py Outdated
@bdraco
Copy link
Copy Markdown
Member Author

bdraco commented Jul 3, 2023

At 32 there is still a nice drop in connection churn and less risk of side effects from non-compliant webservers

@bdraco bdraco changed the title Tune keep alives for polling integrations Tune httpx keep alives for polling integrations Jul 3, 2023
@bdraco bdraco marked this pull request as ready for review July 3, 2023 16:29
@bdraco bdraco requested a review from a team as a code owner July 3, 2023 16:29
Copy link
Copy Markdown
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

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

Should we follow the 75s too like nginx has ?

@balloob balloob merged commit 4d3662d into dev Jul 3, 2023
@balloob balloob deleted the keep_alive_polling branch July 3, 2023 18:22
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla-signed core small-pr PRs with less than 30 lines.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants