Skip to content

Commit 453cef1

Browse files
refactor(db): move push store ownership
Signed-off-by: tornquist <[email protected]>
1 parent d6abdf9 commit 453cef1

3 files changed

Lines changed: 252 additions & 169 deletions

File tree

‎crates/buzz-db/src/lib.rs‎

Lines changed: 0 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,175 +1222,6 @@ impl Db {
12221222
Ok(result)
12231223
}
12241224

1225-
/// Exclusively claim a batch of due matcher jobs from one community.
1226-
#[datastore_span(name = "claim_due_push_match_batch", system = "postgresql")]
1227-
pub async fn claim_due_push_match_batch(
1228-
&self,
1229-
limit: i64,
1230-
lease_until: DateTime<Utc>,
1231-
) -> Result<Option<push::ClaimedMatchBatch>> {
1232-
push::claim_due_match_batch(&self.pool, limit, lease_until).await
1233-
}
1234-
1235-
/// Load active endpoint-enabled leases eligible for push matching.
1236-
#[datastore_span(name = "active_push_match_leases", system = "postgresql")]
1237-
pub async fn active_push_match_leases(
1238-
&self,
1239-
community: CommunityId,
1240-
) -> Result<Vec<push::MatchLease>> {
1241-
push::active_match_leases(&self.pool, community).await
1242-
}
1243-
1244-
/// Complete matcher jobs from one claimed batch while the fence holds.
1245-
#[datastore_span(name = "complete_push_match_batch", system = "postgresql")]
1246-
pub async fn complete_push_match_batch(
1247-
&self,
1248-
community: CommunityId,
1249-
claim_id: uuid::Uuid,
1250-
event_ids: &[Vec<u8>],
1251-
) -> Result<u64> {
1252-
push::complete_match_batch(&self.pool, community, claim_id, event_ids).await
1253-
}
1254-
1255-
/// Release fenced matcher claims from one batch for retry.
1256-
#[datastore_span(name = "retry_push_match_batch", system = "postgresql")]
1257-
pub async fn retry_push_match_batch(
1258-
&self,
1259-
community: CommunityId,
1260-
claim_id: uuid::Uuid,
1261-
event_ids: &[Vec<u8>],
1262-
next: DateTime<Utc>,
1263-
) -> Result<u64> {
1264-
push::retry_match_batch(&self.pool, community, claim_id, event_ids, next).await
1265-
}
1266-
1267-
/// Delete exhausted matcher jobs (periodic sweep, off the claim path).
1268-
#[datastore_span(name = "reap_exhausted_push_matches", system = "postgresql")]
1269-
pub async fn reap_exhausted_push_matches(&self) -> Result<u64> {
1270-
push::reap_exhausted_matches(&self.pool).await
1271-
}
1272-
1273-
/// Idempotently enqueue a wake for a matched lease and event.
1274-
#[datastore_span(name = "enqueue_push_wake", system = "postgresql")]
1275-
pub async fn enqueue_push_wake(
1276-
&self,
1277-
community: CommunityId,
1278-
author: &[u8],
1279-
installation_id: &str,
1280-
wake: push::NewWake<'_>,
1281-
) -> Result<push::EnqueueWakeOutcome> {
1282-
push::enqueue_wake(&self.pool, community, author, installation_id, wake).await
1283-
}
1284-
1285-
/// Set-wise [`Self::enqueue_push_wake`]: one transaction per batch.
1286-
#[datastore_span(name = "enqueue_push_wakes", system = "postgresql")]
1287-
pub async fn enqueue_push_wakes(
1288-
&self,
1289-
community: CommunityId,
1290-
requests: &[push::WakeRequest],
1291-
) -> Result<Vec<push::EnqueueWakeOutcome>> {
1292-
push::enqueue_wakes(&self.pool, community, requests).await
1293-
}
1294-
1295-
/// Exclusively claim due wake jobs for one community.
1296-
#[datastore_span(name = "claim_due_push_wakes", system = "postgresql")]
1297-
pub async fn claim_due_push_wakes(
1298-
&self,
1299-
community: CommunityId,
1300-
limit: i64,
1301-
lease_until: DateTime<Utc>,
1302-
) -> Result<Vec<push::ClaimedWake>> {
1303-
push::claim_due_wakes(&self.pool, community, limit, lease_until).await
1304-
}
1305-
1306-
/// Revalidate a wake's claim, source event, and current lease before send.
1307-
#[datastore_span(name = "revalidate_push_wake", system = "postgresql")]
1308-
pub async fn revalidate_push_wake(
1309-
&self,
1310-
community: CommunityId,
1311-
id: Uuid,
1312-
claim_id: Uuid,
1313-
) -> Result<push::RevalidateWakeOutcome> {
1314-
push::revalidate_wake_for_send(&self.pool, community, id, claim_id).await
1315-
}
1316-
1317-
/// Mark a fenced wake claim delivered.
1318-
#[datastore_span(name = "complete_push_wake", system = "postgresql")]
1319-
pub async fn complete_push_wake(
1320-
&self,
1321-
community: CommunityId,
1322-
id: Uuid,
1323-
claim_id: Uuid,
1324-
) -> Result<bool> {
1325-
push::complete_wake(&self.pool, community, id, claim_id).await
1326-
}
1327-
1328-
/// Release a fenced wake claim for retry at the supplied time.
1329-
#[datastore_span(name = "retry_push_wake", system = "postgresql")]
1330-
pub async fn retry_push_wake(
1331-
&self,
1332-
community: CommunityId,
1333-
id: Uuid,
1334-
claim_id: Uuid,
1335-
next: DateTime<Utc>,
1336-
) -> Result<bool> {
1337-
push::retry_wake(&self.pool, community, id, claim_id, next).await
1338-
}
1339-
1340-
/// Mark a fenced wake claim terminally failed.
1341-
#[datastore_span(name = "fail_push_wake", system = "postgresql")]
1342-
pub async fn fail_push_wake(
1343-
&self,
1344-
community: CommunityId,
1345-
id: Uuid,
1346-
claim_id: Uuid,
1347-
) -> Result<bool> {
1348-
push::fail_wake(&self.pool, community, id, claim_id).await
1349-
}
1350-
1351-
/// Disable an endpoint only if the specified lease generation is current.
1352-
#[datastore_span(name = "disable_push_endpoint", system = "postgresql")]
1353-
pub async fn disable_push_endpoint(
1354-
&self,
1355-
community: CommunityId,
1356-
author: &[u8],
1357-
installation_id: &str,
1358-
generation: i64,
1359-
) -> Result<bool> {
1360-
push::disable_endpoint_generation(
1361-
&self.pool,
1362-
community,
1363-
author,
1364-
installation_id,
1365-
generation,
1366-
)
1367-
.await
1368-
}
1369-
1370-
/// Atomically persist a validated kind:30350 event and its effective lease.
1371-
#[allow(clippy::too_many_arguments)]
1372-
#[datastore_span(name = "accept_push_lease_event", system = "postgresql")]
1373-
pub async fn accept_push_lease_event(
1374-
&self,
1375-
community: CommunityId,
1376-
event: &nostr::Event,
1377-
installation_id: &str,
1378-
version: push::LeaseVersion<'_>,
1379-
active: Option<push::ActiveLease<'_>>,
1380-
max_active_leases: i64,
1381-
) -> Result<push::AcceptLeaseOutcome> {
1382-
push::accept_lease_event(
1383-
&self.pool,
1384-
community,
1385-
event,
1386-
installation_id,
1387-
version,
1388-
active,
1389-
max_active_leases,
1390-
)
1391-
.await
1392-
}
1393-
13941225
/// Shared route decision for one read: evaluate the predicate against a
13951226
/// proved reader session and record the decision. Fail closed to the
13961227
/// writer everywhere.

‎crates/buzz-db/src/push.rs‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use sqlx::{PgPool, Row as _};
1111
use uuid::Uuid;
1212

1313
use crate::error::Result;
14+
use crate::Db;
15+
use buzz_datastore_tracing::datastore_span;
1416

1517
/// Namespace for the per-community push-gate advisory lock. Must match the
1618
/// key built inside the `enqueue_push_match_job` trigger (migration 0023):
@@ -1278,6 +1280,177 @@ fn row_to_claimed_wake(row: sqlx::postgres::PgRow) -> Result<ClaimedWake> {
12781280
})
12791281
}
12801282

1283+
impl Db {
1284+
/// Exclusively claim a batch of due matcher jobs from one community.
1285+
#[datastore_span(name = "claim_due_push_match_batch", system = "postgresql")]
1286+
pub async fn claim_due_push_match_batch(
1287+
&self,
1288+
limit: i64,
1289+
lease_until: DateTime<Utc>,
1290+
) -> Result<Option<crate::push::ClaimedMatchBatch>> {
1291+
crate::push::claim_due_match_batch(&self.pool, limit, lease_until).await
1292+
}
1293+
1294+
/// Load active endpoint-enabled leases eligible for push matching.
1295+
#[datastore_span(name = "active_push_match_leases", system = "postgresql")]
1296+
pub async fn active_push_match_leases(
1297+
&self,
1298+
community: CommunityId,
1299+
) -> Result<Vec<crate::push::MatchLease>> {
1300+
crate::push::active_match_leases(&self.pool, community).await
1301+
}
1302+
1303+
/// Complete matcher jobs from one claimed batch while the fence holds.
1304+
#[datastore_span(name = "complete_push_match_batch", system = "postgresql")]
1305+
pub async fn complete_push_match_batch(
1306+
&self,
1307+
community: CommunityId,
1308+
claim_id: uuid::Uuid,
1309+
event_ids: &[Vec<u8>],
1310+
) -> Result<u64> {
1311+
crate::push::complete_match_batch(&self.pool, community, claim_id, event_ids).await
1312+
}
1313+
1314+
/// Release fenced matcher claims from one batch for retry.
1315+
#[datastore_span(name = "retry_push_match_batch", system = "postgresql")]
1316+
pub async fn retry_push_match_batch(
1317+
&self,
1318+
community: CommunityId,
1319+
claim_id: uuid::Uuid,
1320+
event_ids: &[Vec<u8>],
1321+
next: DateTime<Utc>,
1322+
) -> Result<u64> {
1323+
crate::push::retry_match_batch(&self.pool, community, claim_id, event_ids, next).await
1324+
}
1325+
1326+
/// Delete exhausted matcher jobs (periodic sweep, off the claim path).
1327+
#[datastore_span(name = "reap_exhausted_push_matches", system = "postgresql")]
1328+
pub async fn reap_exhausted_push_matches(&self) -> Result<u64> {
1329+
crate::push::reap_exhausted_matches(&self.pool).await
1330+
}
1331+
1332+
/// Idempotently enqueue a wake for a matched lease and event.
1333+
#[datastore_span(name = "enqueue_push_wake", system = "postgresql")]
1334+
pub async fn enqueue_push_wake(
1335+
&self,
1336+
community: CommunityId,
1337+
author: &[u8],
1338+
installation_id: &str,
1339+
wake: crate::push::NewWake<'_>,
1340+
) -> Result<crate::push::EnqueueWakeOutcome> {
1341+
crate::push::enqueue_wake(&self.pool, community, author, installation_id, wake).await
1342+
}
1343+
1344+
/// Set-wise [`Self::enqueue_push_wake`]: one transaction per batch.
1345+
#[datastore_span(name = "enqueue_push_wakes", system = "postgresql")]
1346+
pub async fn enqueue_push_wakes(
1347+
&self,
1348+
community: CommunityId,
1349+
requests: &[crate::push::WakeRequest],
1350+
) -> Result<Vec<crate::push::EnqueueWakeOutcome>> {
1351+
crate::push::enqueue_wakes(&self.pool, community, requests).await
1352+
}
1353+
1354+
/// Exclusively claim due wake jobs for one community.
1355+
#[datastore_span(name = "claim_due_push_wakes", system = "postgresql")]
1356+
pub async fn claim_due_push_wakes(
1357+
&self,
1358+
community: CommunityId,
1359+
limit: i64,
1360+
lease_until: DateTime<Utc>,
1361+
) -> Result<Vec<crate::push::ClaimedWake>> {
1362+
crate::push::claim_due_wakes(&self.pool, community, limit, lease_until).await
1363+
}
1364+
1365+
/// Revalidate a wake's claim, source event, and current lease before send.
1366+
#[datastore_span(name = "revalidate_push_wake", system = "postgresql")]
1367+
pub async fn revalidate_push_wake(
1368+
&self,
1369+
community: CommunityId,
1370+
id: Uuid,
1371+
claim_id: Uuid,
1372+
) -> Result<crate::push::RevalidateWakeOutcome> {
1373+
crate::push::revalidate_wake_for_send(&self.pool, community, id, claim_id).await
1374+
}
1375+
1376+
/// Mark a fenced wake claim delivered.
1377+
#[datastore_span(name = "complete_push_wake", system = "postgresql")]
1378+
pub async fn complete_push_wake(
1379+
&self,
1380+
community: CommunityId,
1381+
id: Uuid,
1382+
claim_id: Uuid,
1383+
) -> Result<bool> {
1384+
crate::push::complete_wake(&self.pool, community, id, claim_id).await
1385+
}
1386+
1387+
/// Release a fenced wake claim for retry at the supplied time.
1388+
#[datastore_span(name = "retry_push_wake", system = "postgresql")]
1389+
pub async fn retry_push_wake(
1390+
&self,
1391+
community: CommunityId,
1392+
id: Uuid,
1393+
claim_id: Uuid,
1394+
next: DateTime<Utc>,
1395+
) -> Result<bool> {
1396+
crate::push::retry_wake(&self.pool, community, id, claim_id, next).await
1397+
}
1398+
1399+
/// Mark a fenced wake claim terminally failed.
1400+
#[datastore_span(name = "fail_push_wake", system = "postgresql")]
1401+
pub async fn fail_push_wake(
1402+
&self,
1403+
community: CommunityId,
1404+
id: Uuid,
1405+
claim_id: Uuid,
1406+
) -> Result<bool> {
1407+
crate::push::fail_wake(&self.pool, community, id, claim_id).await
1408+
}
1409+
1410+
/// Disable an endpoint only if the specified lease generation is current.
1411+
#[datastore_span(name = "disable_push_endpoint", system = "postgresql")]
1412+
pub async fn disable_push_endpoint(
1413+
&self,
1414+
community: CommunityId,
1415+
author: &[u8],
1416+
installation_id: &str,
1417+
generation: i64,
1418+
) -> Result<bool> {
1419+
crate::push::disable_endpoint_generation(
1420+
&self.pool,
1421+
community,
1422+
author,
1423+
installation_id,
1424+
generation,
1425+
)
1426+
.await
1427+
}
1428+
1429+
/// Atomically persist a validated kind:30350 event and its effective lease.
1430+
#[allow(clippy::too_many_arguments)]
1431+
#[datastore_span(name = "accept_push_lease_event", system = "postgresql")]
1432+
pub async fn accept_push_lease_event(
1433+
&self,
1434+
community: CommunityId,
1435+
event: &nostr::Event,
1436+
installation_id: &str,
1437+
version: crate::push::LeaseVersion<'_>,
1438+
active: Option<crate::push::ActiveLease<'_>>,
1439+
max_active_leases: i64,
1440+
) -> Result<crate::push::AcceptLeaseOutcome> {
1441+
crate::push::accept_lease_event(
1442+
&self.pool,
1443+
community,
1444+
event,
1445+
installation_id,
1446+
version,
1447+
active,
1448+
max_active_leases,
1449+
)
1450+
.await
1451+
}
1452+
}
1453+
12811454
#[cfg(test)]
12821455
mod tests {
12831456
use super::*;

0 commit comments

Comments
 (0)