Skip to content

fix: reuse DynamoEnvironmentWrapper in DynamoIdentityWrapper#6813

Open
gagantrivedi wants to merge 4 commits intomainfrom
fix/reuse-dynamo-environment-wrapper-singleton
Open

fix: reuse DynamoEnvironmentWrapper in DynamoIdentityWrapper#6813
gagantrivedi wants to merge 4 commits intomainfrom
fix/reuse-dynamo-environment-wrapper-singleton

Conversation

@gagantrivedi
Copy link
Member

@gagantrivedi gagantrivedi commented Mar 2, 2026

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

DynamoIdentityWrapper.get_segment_ids() was creating a new DynamoEnvironmentWrapper() on every call. Each instantiation triggers a fresh boto3.resource("dynamodb", ...) session with its own HTTP connection pool and botocore parser chain, causing excessive memory churn (~145MB of botocore parsing overhead per profiling run).

This PR:

  • Adds constructor injection to DynamoIdentityWrapper — it now accepts an optional environment_wrapper parameter, defaulting to creating one at init time rather than per get_segment_ids call.
  • Updates production call sites (EdgeIdentity, environments/tasks.py, migrator.py) to pass existing DynamoEnvironmentWrapper instances instead of letting each wrapper create its own.

How did you test this code?

  • Added a regression test (test_get_segment_ids__called_multiple_times__reuses_environment_wrapper) that calls get_segment_ids three times and asserts DynamoEnvironmentWrapper is instantiated only once. This test fails before the fix (call count = 3) and passes after (call count = 1).

get_segment_ids was creating a new DynamoEnvironmentWrapper on every
call, each triggering a fresh boto3.resource() session. This caused
excessive botocore parsing overhead and memory churn.

Add constructor injection so DynamoIdentityWrapper accepts an optional
DynamoEnvironmentWrapper, defaulting to creating one at init time.
Update call sites to pass existing wrapper instances where available.
@gagantrivedi gagantrivedi requested a review from a team as a code owner March 2, 2026 07:26
@gagantrivedi gagantrivedi requested review from khvn26 and removed request for a team March 2, 2026 07:26
@vercel
Copy link

vercel bot commented Mar 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs Ignored Ignored Preview Mar 2, 2026 8:08am
flagsmith-frontend-preview Ignored Ignored Preview Mar 2, 2026 8:08am
flagsmith-frontend-staging Ignored Ignored Preview Mar 2, 2026 8:08am

Request Review

@github-actions github-actions bot added the api Issue related to the REST API label Mar 2, 2026
@github-actions github-actions bot added the fix label Mar 2, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-6813 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api-test:pr-6813 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-6813 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-6813 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-6813 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-6813 Finished ✅ Results

Cast identity_uuid to str to satisfy mypy strict mode, since
identity_document dict values are typed as DocumentValue (union type).
@github-actions github-actions bot added fix and removed fix labels Mar 2, 2026
Revert import-time DI in EdgeIdentity — class-level DynamoIdentityWrapper
already creates one internal DynamoEnvironmentWrapper via the default,
which is sufficient since it's a singleton.

Update migrator test assertion to expect environment_wrapper kwarg.
@github-actions github-actions bot added fix and removed fix labels Mar 2, 2026
@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.30%. Comparing base (380fecd) to head (cd319b2).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6813   +/-   ##
=======================================
  Coverage   98.30%   98.30%           
=======================================
  Files        1331     1331           
  Lines       49332    49346   +14     
=======================================
+ Hits        48494    48508   +14     
  Misses        838      838           

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant