Skip to content

ref(apigateway): use a threadlocal session for proxy requests#116054

Merged
JoshFerge merged 4 commits into
masterfrom
fix/apigateway-pooling-session
May 28, 2026
Merged

ref(apigateway): use a threadlocal session for proxy requests#116054
JoshFerge merged 4 commits into
masterfrom
fix/apigateway-pooling-session

Conversation

@JoshFerge
Copy link
Copy Markdown
Member

@JoshFerge JoshFerge commented May 21, 2026

Use a rollout-gated, thread-local requests.Session for synchronous API gateway proxy requests so the gateway can reuse HTTP connections without sharing a session object across request-handler threads. Same as #115827 which we saw helped quite a bit with RPC latency, but on our api gateway cross silo requests.

Slack Context: https://sentry.slack.com/archives/C0B40RRUCT0/p1779391427679709

Rollout Control

Pooling now uses hybridcloud.apigateway.use_pooling.rate as a sampled rollout rate, matching the RPC pooling rollout behavior instead of treating any non-zero value as full enablement.

Cookie Handling

The pooled session uses a stateless cookie jar so backend Set-Cookie headers are still returned to the client, incoming request Cookie headers are still forwarded to the cell, and response cookies are not remembered for later requests on the same thread.

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label May 21, 2026
@JoshFerge JoshFerge force-pushed the fix/apigateway-pooling-session branch 2 times, most recently from 66090ab to 3cf4267 Compare May 21, 2026 21:19
@JoshFerge JoshFerge marked this pull request as ready for review May 21, 2026 21:22
@JoshFerge JoshFerge requested review from a team as code owners May 21, 2026 21:22
Comment thread src/sentry/hybridcloud/apigateway/proxy.py Outdated
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 3cf4267. Configure here.

Comment thread src/sentry/hybridcloud/apigateway/proxy.py
Comment thread src/sentry/hybridcloud/apigateway/proxy.py
@JoshFerge JoshFerge marked this pull request as draft May 21, 2026 21:29
@JoshFerge JoshFerge force-pushed the fix/apigateway-pooling-session branch from 3cf4267 to d76fe99 Compare May 21, 2026 21:30
Create the pooled API gateway requests session on first use in each thread so rollout-enabled proxy requests do not fail in worker threads.

Register the API gateway pooling rollout option and apply it through the existing random rollout helper.

Co-Authored-By: Codex <noreply@openai.com>
@JoshFerge JoshFerge force-pushed the fix/apigateway-pooling-session branch from d76fe99 to 46383d6 Compare May 21, 2026 21:39
@JoshFerge JoshFerge changed the title fix(apigateway): Lazily initialize pooled sessions ref(apigateway): use a threadlocal session for proxy requests May 21, 2026
Copy link
Copy Markdown
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

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

Looks good to me. The cookie jar implementation you have will prevent cookies from proxied requests from being stored. I read through requests' docs and didn't see any other attributes that would be retained across requests.

Release streamed upstream responses after Django finishes proxying so pooled sessions do not hold sockets or file descriptors after the response lifecycle ends.

Add test cleanup for the thread-local sync proxy session to keep pooled connection state isolated between tests.

Co-Authored-By: Codex <codex@openai.com>
@JoshFerge JoshFerge marked this pull request as ready for review May 28, 2026 20:42
… tests

Select the request callable once (pooled session vs. one-off request)
instead of duplicating the full request call across both branches.

Drop the production-only close_connection() helper, which was only ever
used for test cleanup, and inline that teardown into the test fixture so
the proxy module no longer carries test-only API.

https://claude.ai/code/session_01WKg4JcWqqoM19sJ7BSqC98
@JoshFerge JoshFerge merged commit 9537b8a into master May 28, 2026
84 checks passed
@JoshFerge JoshFerge deleted the fix/apigateway-pooling-session branch May 28, 2026 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants