-
Notifications
You must be signed in to change notification settings - Fork 111
fix(selector): expire token locks by the consuming transaction #2244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -117,3 +117,45 @@ func IsExpiredToken(tokenRequests, tokenLocks common3.Table, leaseExpiry time.Du | |||||||||||||
| cond.OlderThan(tokenLocks.Field("created_at"), leaseExpiry), | ||||||||||||||
| ) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // IsStaleLock matches the lock rows whose lease has aged out, or whose consuming | ||||||||||||||
| // transaction is Deleted or Orphan. The correlation is on consumer_tx_id, the | ||||||||||||||
| // transaction that is trying to spend the token: (tx_id, idx) identifies the locked | ||||||||||||||
| // token, i.e. the transaction that created it, whose status says nothing about | ||||||||||||||
| // whether the lock is still live. The condition is correlated rather than a | ||||||||||||||
| // partial-key IN on tx_id, so cleanup removes only the matching (tx_id, idx) rows | ||||||||||||||
| // and leaves the other indices of the same transaction locked. See #2018. | ||||||||||||||
| func IsStaleLock(tokenLocks, tokenRequests common3.Table, leaseExpiry time.Duration) cond.Condition { | ||||||||||||||
|
HayimShaul marked this conversation as resolved.
Outdated
|
||||||||||||||
| return cond.Or( | ||||||||||||||
| cond.OlderThan(tokenLocks.Field("created_at"), leaseExpiry), | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should fix here. Timezone mismatch between the two halves of this comparison on Postgres.
For a lock created at 12:00 UTC with a 5s lease:
SQLite is unaffected because Pre-existing rather than introduced here, but this PR consolidates the query into one place and documents the behaviour as uniform, so it is the natural moment. |
||||||||||||||
| cond.Exists( | ||||||||||||||
| q.Select(). | ||||||||||||||
| Fields(common3.FieldName("1")). | ||||||||||||||
| From(tokenRequests). | ||||||||||||||
| Where(cond.And( | ||||||||||||||
| cond.Cmp(tokenRequests.Field("tx_id"), "=", tokenLocks.Field("consumer_tx_id")), | ||||||||||||||
| cond.FieldIn(tokenRequests.Field("status"), driver.Deleted, driver.Orphan), | ||||||||||||||
| )), | ||||||||||||||
| ), | ||||||||||||||
| ) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Cleanup releases the stale token locks: those whose consuming transaction is | ||||||||||||||
| // Deleted or Orphan, and those whose lease is older than leaseExpiry. Only the | ||||||||||||||
| // affected (tx_id, idx) rows are deleted. The same statement is used by every SQL | ||||||||||||||
| // backend, so lock lifetime does not depend on the driver in use. | ||||||||||||||
| func (db *TokenLockStore) Cleanup(ctx context.Context, leaseExpiry time.Duration) error { | ||||||||||||||
| tokenLocks, tokenRequests := q.Table(db.Table.TokenLocks), q.Table(db.Table.Requests) | ||||||||||||||
|
|
||||||||||||||
| query, args := q.DeleteFrom(db.Table.TokenLocks). | ||||||||||||||
| Where(IsStaleLock(tokenLocks, tokenRequests, leaseExpiry)). | ||||||||||||||
| Format(db.ci) | ||||||||||||||
|
|
||||||||||||||
| db.Logger.Debug(query, args) | ||||||||||||||
| _, err := db.WriteDB.ExecContext(ctx, query, args...) | ||||||||||||||
| if err != nil { | ||||||||||||||
| db.Logger.Errorf("query failed: %s", query) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.