Skip to content

Commit

Permalink
fix: add advisory locking to the client create (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Oct 6, 2023
1 parent 066bd18 commit 46d1797
Showing 1 changed file with 3 additions and 4 deletions.
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

0 comments on commit 46d1797

Please sign in to comment.