Skip to content
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

SQLite worker queue implementation #969

Merged
merged 3 commits into from
Nov 7, 2024

Conversation

isaacdonaldson
Copy link
Contributor

This is an implementation of an SQLite background worker queue.

Since SQLite does not have FOR UPDATE SKIP LOCKED and sqlx does not support BEGIN IMMEDIATE transactions (from what I could tell), I opted to use another table aquire_queue_write_lock as concurrency control.

When reading for dequeue, it will attempt to update a field in the table, on success it will turn the transaction into a write transaction (from the starting point of read transaction). This will prevent the other readers from reading a queued task until the current one is done selecting one. On failure it will not select a task as there is another transaction selecting one, making sure that no task is selected twice.

@jondot
Copy link
Contributor

jondot commented Nov 7, 2024

wow i was hoping that someone with sqlite expertise come in and implement this one! thank you!!
i'm not an expert but I've just spent some time reading about other sqlite implementations, and it looks like people say that sqlite locks the entire DB on writes. would that obsolete the locking table/issue?

@isaacdonaldson
Copy link
Contributor Author

It does lock on writes, but the dequeue method is a read transaction until it finds a task, then afterwords when claiming it, it gets upgraded to a write transaction. The write into the lock_queue will upgrade the transaction to a write queue before it fetches a task, so it will be locked for the read and remove the chance a task is claimed twice.

@jondot
Copy link
Contributor

jondot commented Nov 7, 2024

Makes sense. Fantastic contribution ❤️

@jondot jondot merged commit 9879c2c into loco-rs:master Nov 7, 2024
16 checks passed
@jondot jondot added this to the 0.13.0 milestone Nov 9, 2024
jondot added a commit that referenced this pull request Nov 9, 2024
* add sqlite worker_queue implementation

* add backticks for clippy

---------

Co-authored-by: Dotan J. Nahum <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants