Skip to content

fix: stop destroying connection pool after every sync request#270

Merged
SoulPancake merged 5 commits intomainfrom
fix/conn-pool-reuse-bug
Apr 15, 2026
Merged

fix: stop destroying connection pool after every sync request#270
SoulPancake merged 5 commits intomainfrom
fix/conn-pool-reuse-bug

Conversation

@SoulPancake
Copy link
Copy Markdown
Member

Description

The sync REST client called self.close() at the end of every request() method (sync/rest.py, line 501). This invokes pool_manager.clear(), which per urllib3's implementation "empties the store of pools and directs them all to close" so every pooled TCP+TLS connection is destroyed after a single use. Each subsequent request had to establish a brand new connection from scratch.

The async client (openfga_sdk/rest.py) does not call self.close() after requests, so this bug was specific to the sync client path.

What problem is being solved?

How is it being solved?

What changes are made to solve it?

References

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

Copilot AI review requested due to automatic review settings April 14, 2026 12:00
@SoulPancake SoulPancake requested a review from a team as a code owner April 14, 2026 12:00
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 14, 2026

Warning

Rate limit exceeded

@SoulPancake has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 35 minutes and 5 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 35 minutes and 5 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9bf9d14b-4435-4530-a7e0-b940c24bd307

📥 Commits

Reviewing files that changed from the base of the PR and between c4dab0b and c03417e.

📒 Files selected for processing (7)
  • openfga_sdk/rest.py
  • openfga_sdk/sync/api_client.py
  • openfga_sdk/sync/rest.py
  • openfga_sdk/telemetry/attributes.py
  • openfga_sdk/validation.py
  • test/rest_test.py
  • test/sync/rest_test.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/conn-pool-reuse-bug

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.85%. Comparing base (c4dab0b) to head (c03417e).

❌ Your project status has failed because the head coverage (69.85%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #270      +/-   ##
==========================================
+ Coverage   69.84%   69.85%   +0.01%     
==========================================
  Files         140      140              
  Lines       10743    10748       +5     
==========================================
+ Hits         7503     7508       +5     
  Misses       3240     3240              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an issue in the synchronous REST client where the connection pool was effectively being torn down after every request, preventing TCP/TLS connection reuse and hurting performance.

Changes:

  • Stop clearing the sync client’s urllib3 pool manager at the end of every request() call.
  • Minor request-building optimization: simplify JSON content-type detection (avoid regex) in both sync and async clients.
  • Micro-optimizations: precompile ULID regex; avoid parsing URLs twice when deriving telemetry attributes.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
openfga_sdk/sync/rest.py Removes per-request pool teardown and simplifies JSON content-type detection.
openfga_sdk/rest.py Matches sync behavior for JSON content-type detection (regex → lowercase substring check).
openfga_sdk/validation.py Precompiles ULID regex at module scope to avoid recompiling per call.
openfga_sdk/telemetry/attributes.py Parses URL once when extracting hostname/scheme telemetry attributes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread openfga_sdk/sync/rest.py Outdated
Comment thread openfga_sdk/sync/rest.py Outdated
@SoulPancake
Copy link
Copy Markdown
Member Author

Reference: regarding the auto release back to the pool https://urllib3.readthedocs.io/en/stable/advanced-usage.html#streaming-and-i-o

@SoulPancake SoulPancake added this pull request to the merge queue Apr 15, 2026
Merged via the queue into main with commit 9d39016 Apr 15, 2026
29 checks passed
@SoulPancake SoulPancake deleted the fix/conn-pool-reuse-bug branch April 15, 2026 06:21
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.

4 participants