Skip to content

Commit

Permalink
fix: use client_id lock when inserting notification
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Jan 25, 2024
1 parent d77fa50 commit 715db35
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/stores/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ impl NotificationStore for sqlx::PgPool {
client_id: &str,
payload: &PushMessageBody,
) -> stores::Result<Notification> {
let mut transaction = self.begin().await?;

sqlx::query("SELECT pg_advisory_xact_lock(abs(hashtext($1::text)))")
.bind(client_id)
.execute(&mut transaction)
.await?;

let res = sqlx::query_as::<sqlx::postgres::Postgres, Notification>(
"
INSERT INTO public.notifications (id, tenant_id, client_id, last_payload)
Expand All @@ -62,9 +69,11 @@ impl NotificationStore for sqlx::PgPool {
.bind(tenant_id)
.bind(client_id)
.bind(Json(payload))
.fetch_one(self)
.fetch_one(&mut transaction)
.await;

transaction.commit().await?;

match res {
Err(e) => Err(e.into()),
Ok(row) => Ok(row),
Expand Down

0 comments on commit 715db35

Please sign in to comment.