From 19ac295fdaf30e6fc4b160640813ecd1cfb64361 Mon Sep 17 00:00:00 2001 From: Max Kalashnikoff Date: Fri, 6 Oct 2023 15:00:40 +0300 Subject: [PATCH] fix: add advisory locking to client create --- src/stores/client.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/stores/client.rs b/src/stores/client.rs index 5ea78e25..3fa5e64f 100644 --- a/src/stores/client.rs +++ b/src/stores/client.rs @@ -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?;