Repository navigation
feat: reset balance events - #2750
Conversation
📝 WalkthroughWalkthroughThis change introduces explicit support for a new "reset" operation type in entitlement event handling. It adds the Changes
Possibly related PRs
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (11)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
73522d4 to
8578b82
Compare
8578b82 to
6feaf90
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (4)
openmeter/entitlement/snapshot/event.go (2)
118-121: Remove redundantOperation.Validate()call
Operation.Validate()is invoked twice inSnapshotEvent.Validate()(lines 94–96 and again here).
The second invocation is superfluous and slightly inflates the hot-path of every snapshot validation.- if err := e.Operation.Validate(); err != nil { - return err - }Removing these four lines keeps behaviour unchanged while shaving a small amount of CPU cycles and allocations during event validation.
25-31: Micro-optimiseValues()(optional)
Values()allocates a fresh slice on every call. As it is only used byValidate(), consider:-var _allValueOperations = []ValueOperationType{ - ValueOperationReset, - ValueOperationUpdate, - ValueOperationDelete, -} - -func (o ValueOperationType) Values() []ValueOperationType { - return _allValueOperations -} +var _allValueOperations = [...]ValueOperationType{ + ValueOperationReset, + ValueOperationUpdate, + ValueOperationDelete, +} + +func (o ValueOperationType) Values() []ValueOperationType { + // slice over the array – zero alloc + return _allValueOperations[:] +}This is entirely optional, but brings
Validate()down to a single allocation-free slice header construction.openmeter/entitlement/balanceworker/entitlementhandler.go (2)
51-55: Avoid storing a pointer to a captured variable (minor)
WithSourceOperationkeeps the address of the function-parametersourceOperation.
Although the variable is heap-escaped and safe, using a value copy avoids subtle lifetime surprises:-func WithSourceOperation(sourceOperation snapshot.ValueOperationType) handleOption { - return func(o *handleEntitlementEventOptions) { - o.sourceOperation = &sourceOperation - } +func WithSourceOperation(op snapshot.ValueOperationType) handleOption { + return func(o *handleEntitlementEventOptions) { + tmp := op // guarantees unique address per call + o.sourceOperation = &tmp + } }Purely optional; existing code works, but the rewrite removes an indirection to a shared variable.
80-104: High-watermark cache bypass for reset events looks correct but skips cache update
checkHighWatermarkCachealways returnsrecalculateActionfor reset events, yethandleEntitlementEventnever adds a cache entry along the reset fast-path (lines 179-192).
That means identical reset events (same entitlement + time) will trigger full recalculation on every delivery attempt/replay.Consider either:
- updating the cache after emitting the reset snapshot, or
- including the operation type in the cache key so that non-reset events are unaffected.
This would prevent unnecessary recalculations while keeping semantics intact.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
openmeter/entitlement/balanceworker/entitlementhandler.go(10 hunks)openmeter/entitlement/balanceworker/worker.go(2 hunks)openmeter/entitlement/snapshot/event.go(4 hunks)openmeter/notification/consumer/balancetreshold.go(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
openmeter/entitlement/balanceworker/worker.go (2)
openmeter/entitlement/balanceworker/entitlementhandler.go (2)
WithEventAt(45-49)WithSourceOperation(51-55)openmeter/entitlement/snapshot/event.go (1)
ValueOperationReset(20-20)
openmeter/notification/consumer/balancetreshold.go (1)
openmeter/entitlement/snapshot/event.go (3)
ValueOperationType(17-17)ValueOperationUpdate(21-21)ValueOperationReset(20-20)
⏰ Context from checks skipped due to timeout of 90000ms (12)
- GitHub Check: Artifacts / Container image
- GitHub Check: Quickstart
- GitHub Check: E2E
- GitHub Check: CI
- GitHub Check: Test
- GitHub Check: Migration Checks
- GitHub Check: Lint
- GitHub Check: Developer environment
- GitHub Check: Commit hooks
- GitHub Check: Build
- GitHub Check: Analyze (javascript)
- GitHub Check: Analyze (go)
🔇 Additional comments (3)
openmeter/notification/consumer/balancetreshold.go (1)
216-218: LGTM – reset events now correctly pass balance-threshold filterAccepting both
updateandresetoperations makes the consumer consistent with the new snapshot semantics.openmeter/entitlement/balanceworker/entitlementhandler.go (2)
179-192: Good: snapshot taken at period start for reset eventsReset snapshots use
CurrentUsagePeriod.FromascalculatedAt, guaranteeing an accurate “period-initial” balance.
238-243: Validation guard is solidNice touch performing validation on
OverrideOperation; catches typos at call-site early.
Overview
Make sure that we are generating an internal event on resets, that contains the snapshot exactly at the period's start. The operation is set to reset in such cases.
Example event:
{ "specversion": "1.0", "id": "01JT3AECGVGZ7CTHNRTE5FD0ZR", "source": "//openmeter.io/namespace/org_2l3uuzkgTdvCyom82y11jeZO2u5/entitlement/01JT3987GH49S9CBYYKX0BGNS1", "type": "io.openmeter.entitlement.v2.entitlement.snapshot", "subject": "//openmeter.io/namespace/org_2l3uuzkgTdvCyom82y11jeZO2u5/subjectKey/test", "datacontenttype": "application/json", "time": "2025-04-30T12:23:46.459268Z", "data": { "entitlement": { "namespace": "org_2l3uuzkgTdvCyom82y11jeZO2u5", "createdAt": "2025-04-30T12:02:56.145752Z", "updatedAt": "2025-04-30T12:23:46.427412Z", "Annotations": null, "activeFrom": "2025-04-30T12:02:56.142814Z", "id": "01JT3987GH49S9CBYYKX0BGNS1", "featureId": "01JT38V68VS69NCRAZR6KK9FTD", "featureKey": "api_requests_total", "subjectKey": "test", "type": "metered", "usagePeriod": { "period": "P1D", "anchor": "2025-04-30T12:23:00Z" }, "currentUsagePeriod": { "from": "2025-04-30T12:23:00Z", "to": "2025-05-01T12:23:00Z" }, "originalUsagePeriodAnchor": "2025-04-30T12:02:00Z", "measureUsageFrom": "2025-04-30T14:02:00+02:00", "issueAfterReset": 123, "issueAfterResetPriority": 1, "isSoftLimit": true, "lastReset": "2025-04-30T12:23:00Z" }, "namespace": { "id": "org_2l3uuzkgTdvCyom82y11jeZO2u5" }, "subject": { "id": "01JT38VH8H8SA86G0TWN0Z8HVZ", "key": "test", "metadata": {} }, "feature": { "namespace": "org_2l3uuzkgTdvCyom82y11jeZO2u5", "id": "01JT38V68VS69NCRAZR6KK9FTD", "name": "api_requests_total", "key": "api_requests_total", "meterSlug": "api_requests_total", "createdAt": "2025-04-30T11:55:48.891454Z", "updatedAt": "2025-04-30T11:55:48.891454Z" }, "operation": "reset", "calculatedAt": "2025-04-30T12:23:00Z", "value": { "balance": 123, "hasAccess": true, "overage": 0, "usage": 0 }, "currentUsagePeriod": { "from": "2025-04-30T12:23:00Z", "to": "2025-05-01T12:23:00Z" } } }Summary by CodeRabbit