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

fix: add advisory locking to the client create #239

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/stores/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ pub trait ClientStore {
impl ClientStore for sqlx::PgPool {
async fn create_client(&self, tenant_id: &str, id: &str, client: Client) -> stores::Result<()> {
info!(
"ClientStore::create_client tenant_id={tenant_id} id={id} token={}",
"ClientStore::create_client tenant_id={tenant_id} id={id} token={} with locking",
client.token
);

let mut transaction = self.begin().await?;

// Statement for locking the row. Issue #230
sqlx::query("SELECT * FROM public.clients WHERE id = $1 OR device_token = $2 FOR UPDATE")
// Statement for locking to prevent an issue #230
sqlx::query("SELECT pg_advisory_xact_lock(abs(hashtext($1::text)))")
.bind(id)
.bind(client.token.clone())
.execute(&mut transaction)
.await?;

Expand Down
Loading