Commit gateway usage records and token-budget updates atomically

Open
#394 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
django, python
Domain
backend, databases

Research direction

Read record_usage in secondshift/gateway/metering.py and the DAO operations in secondshift/gateway/dao.py to trace the two writes and their async adapter. Add a test that makes the token increment fail after the usage insert; done means neither the usage record nor the budget update commits, while the F() increment remains in use.

Written by the indexing model from the issue text.

Description

code-review secondshift

Priority: Important

record_usage in secondshift/gateway/metering.py awaits separate writes to ModelUsage and RunToken.used_tokens in secondshift/gateway/dao.py. If the second write fails or the task is cancelled between writes, the usage row can remain committed while the budget counter stays unchanged. Future requests check the counter and can continue with a stale balance.

Execute the two writes in one synchronous transaction.atomic() DAO operation called through the async adapter, retaining the F() increment for concurrent requests. Add a test that forces the increment to fail after the insert and verifies that neither change commits.


Opened by SecondShift from run #279 at ef292eae0f91.

Dominant language
Python
Stars
1
Forks
1
Avg merge
1h 9m
Merged PRs (30d)
322

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from piotrekno1/second-shift

All issues in piotrekno1/second-shift

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.