You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(credit): reuse the caller transaction for metered grant reads - #5595
Metered entitlement balance calculations can run inside a transaction that has just issued a grant. Looking up active grants on a separate connection misses that grant and can time out if the transaction already holds the last available connection.
Use the caller’s transaction for active-grant reads in balance, history, and reset calculations, including the starting snapshot. Standalone reads continue to use the ordinary grant repository.
The PostgreSQL regression test issues 10 units, reads the balance before commit with a one-connection pool, and checks that 3 units of usage leave 7 available.
Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.
Summary
Active-grant reads now reuse the caller’s transaction across balance, history, reset, and starting-snapshot calculations.
Credit balance reads now see grants created in the caller’s transaction.
Balance calculations now consistently include grants created within the current transaction, so entitlement balances reflect those grants before the transaction commits.
No actionable comments were generated in the recent review.
Walkthrough
Balance grant lookups now use a transaction-aware helper. A new test verifies that an entitlement balance read inside a transaction includes a grant created in that transaction.
listActiveGrantsBetween runs grant listing through entutils.TransactingRepo. Balance calculations and snapshot lookup use this helper. The test reads the balance before the transaction commits and asserts a result of 7.
Balance calculations inside a transaction now include grants created in that same transaction. They also avoid waiting for a second database connection when the pool has only one. No concrete merge-blocking risk was found.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the main change: metered grant reads reuse the caller's transaction. The raw summary shows listActiveGrantsBetween now runs through entutils.TransactingRepo, and the balance calculatio…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the main change: metered grant reads reuse the caller's transaction. The raw summary shows listActiveGrantsBetween now runs through entutils.TransactingRepo, and the balance calculations call that helper. The added regression test covers the one-connection, uncommitted-grant case. The title is specific and matches the changeset and the PR objectives. It uses the conventional fix(scope) format and is 65 characters long, within a typical limit.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Reviewing files that changed from the base of the PR and between 6856c74 and c372d87.
Files selected for processing: 3
openmeter/credit/balance.go
openmeter/credit/helper.go
openmeter/entitlement/metered/balance_test.go
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Metered entitlement balance calculations can run inside a transaction that has just issued a grant. Looking up active grants on a separate connection misses that grant and can time out if the transaction already holds the last available connection.
Use the caller’s transaction for active-grant reads in balance, history, and reset calculations, including the starting snapshot. Standalone reads continue to use the ordinary grant repository.
The PostgreSQL regression test issues 10 units, reads the balance before commit with a one-connection pool, and checks that 3 units of usage leave 7 available.


Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.Summary
Active-grant reads now reuse the caller’s transaction across balance, history, reset, and starting-snapshot calculations.
No actionable issues found. Tests were not run.
Reviews (1) · Last reviewed commit: "fix(credit): reuse the caller transactio..." · Reviewed by Greptile
Summary by CodeRabbit