Skip to content

Conversation

@dustinbyrne
Copy link
Contributor

@dustinbyrne dustinbyrne commented Dec 9, 2025

Adds enable_keep_alive() and set_socket_options() to configure TCP keepalive on HTTP connections, preventing idle connections from being dropped by network infrastructure.

Additionally adds disable_connection_reuse() to prevent connection pooling altogether.

Usage

import posthog

posthog.enable_keep_alive()
posthog.api_key = '...'

Or with custom socket options:

import posthog

posthog.set_socket_options([...])
posthog.api_key = '...'

Testing

If you'd like to try this branch, install it to your project.

requirements.txt

posthog @ git+https://github.com/PostHog/posthog-python.git@feat/custom-socket-options

pyproject.toml (Poetry):

[tool.poetry.dependencies]
posthog = { git = "https://github.com/PostHog/posthog-python.git", branch = "feat/custom-socket-options" }

pyproject.toml (pip/setuptools):

[project]
dependencies = [
	"posthog @ git+https://github.com/PostHog/posthog-python.git@feat/custom-socket-options",
]

This allows clients to configure (e.g.) socket keep alive probes
@dustinbyrne dustinbyrne force-pushed the feat/custom-socket-options branch from 2ffcf66 to 3eb1610 Compare December 9, 2025 04:34
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Copy link
Contributor

@dmarticus dmarticus left a comment

Choose a reason for hiding this comment

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

Great job finding this; I'm impressed that you tracked this down.

In addition to my comments, 2 more questions:

  1. Given that the specific use case around this is AWS NAT timeouts (and I imagine this might also apply to azure/gcp load balancers as well), can we document this in the options as well?
  2. Should enable_keep_alive() be idempotent? Currently calling it twice rebuilds the session unnecessarily.

@dustinbyrne dustinbyrne merged commit d72e89a into master Dec 9, 2025
15 checks passed
@dustinbyrne dustinbyrne deleted the feat/custom-socket-options branch December 9, 2025 06:52
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

Successfully merging this pull request may close these issues.

3 participants