| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use sqlx::PgPool; |
| 6 |
|
| 7 |
use crate::db::models::DbSyncBlob; |
| 8 |
use crate::db::{SyncAppId, UserId}; |
| 9 |
use crate::error::Result; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#[tracing::instrument(skip_all)] |
| 15 |
pub async fn get_sync_blob_by_hash( |
| 16 |
pool: &PgPool, |
| 17 |
app_id: SyncAppId, |
| 18 |
user_id: UserId, |
| 19 |
hash: &str, |
| 20 |
) -> Result<Option<DbSyncBlob>> { |
| 21 |
let blob = sqlx::query_as::<_, DbSyncBlob>( |
| 22 |
"SELECT * FROM sync_blobs WHERE app_id = $1 AND user_id = $2 AND hash = $3", |
| 23 |
) |
| 24 |
.bind(app_id) |
| 25 |
.bind(user_id) |
| 26 |
.bind(hash) |
| 27 |
.fetch_optional(pool) |
| 28 |
.await?; |
| 29 |
|
| 30 |
Ok(blob) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
async fn sum_blob_bytes<'e, E>(executor: E, app_id: SyncAppId, user_id: UserId) -> Result<i64> |
| 39 |
where |
| 40 |
E: sqlx::PgExecutor<'e>, |
| 41 |
{ |
| 42 |
let used: i64 = sqlx::query_scalar( |
| 43 |
"SELECT COALESCE(SUM(size_bytes), 0)::BIGINT FROM sync_blobs |
| 44 |
WHERE app_id = $1 AND user_id = $2", |
| 45 |
) |
| 46 |
.bind(app_id) |
| 47 |
.bind(user_id) |
| 48 |
.fetch_one(executor) |
| 49 |
.await?; |
| 50 |
Ok(used) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[tracing::instrument(skip_all)] |
| 57 |
pub async fn storage_used_bytes(pool: &PgPool, app_id: SyncAppId, user_id: UserId) -> Result<i64> { |
| 58 |
sum_blob_bytes(pool, app_id, user_id).await |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 64 |
pub enum BlobConfirm { |
| 65 |
|
| 66 |
Stored, |
| 67 |
|
| 68 |
|
| 69 |
AlreadyStored, |
| 70 |
|
| 71 |
NoSubscription, |
| 72 |
|
| 73 |
QuotaExceeded { |
| 74 |
dimension: &'static str, |
| 75 |
used: i64, |
| 76 |
limit: i64, |
| 77 |
key: Option<String>, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
async fn insert_blob_tx( |
| 85 |
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>, |
| 86 |
app_id: SyncAppId, |
| 87 |
user_id: UserId, |
| 88 |
hash: &str, |
| 89 |
size_bytes: i64, |
| 90 |
s3_key: &str, |
| 91 |
key: &str, |
| 92 |
) -> Result<()> { |
| 93 |
sqlx::query( |
| 94 |
r" |
| 95 |
INSERT INTO sync_blobs (app_id, user_id, hash, size_bytes, s3_key, key) |
| 96 |
VALUES ($1, $2, $3, $4, $5, $6) |
| 97 |
ON CONFLICT (app_id, user_id, hash) |
| 98 |
DO UPDATE SET size_bytes = EXCLUDED.size_bytes |
| 99 |
", |
| 100 |
) |
| 101 |
.bind(app_id) |
| 102 |
.bind(user_id) |
| 103 |
.bind(hash) |
| 104 |
.bind(size_bytes) |
| 105 |
.bind(s3_key) |
| 106 |
.bind(key) |
| 107 |
.execute(&mut **tx) |
| 108 |
.await?; |
| 109 |
Ok(()) |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
#[tracing::instrument(skip_all)] |
| 119 |
pub async fn confirm_internal_blob( |
| 120 |
pool: &PgPool, |
| 121 |
app_id: SyncAppId, |
| 122 |
user_id: UserId, |
| 123 |
hash: &str, |
| 124 |
size_bytes: i64, |
| 125 |
s3_key: &str, |
| 126 |
key: &str, |
| 127 |
) -> Result<BlobConfirm> { |
| 128 |
let mut tx = pool.begin().await?; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
let sub: Option<(String, Option<i64>)> = sqlx::query_as( |
| 133 |
"SELECT status, storage_limit_bytes FROM app_sync_subscriptions |
| 134 |
WHERE app_id = $1 AND user_id = $2 FOR UPDATE", |
| 135 |
) |
| 136 |
.bind(app_id) |
| 137 |
.bind(user_id) |
| 138 |
.fetch_optional(&mut *tx) |
| 139 |
.await?; |
| 140 |
let Some((status, limit)) = sub else { |
| 141 |
return Ok(BlobConfirm::NoSubscription); |
| 142 |
}; |
| 143 |
if status != "active" { |
| 144 |
return Ok(BlobConfirm::NoSubscription); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
let exists: Option<i32> = sqlx::query_scalar( |
| 149 |
"SELECT 1 FROM sync_blobs WHERE app_id = $1 AND user_id = $2 AND hash = $3", |
| 150 |
) |
| 151 |
.bind(app_id) |
| 152 |
.bind(user_id) |
| 153 |
.bind(hash) |
| 154 |
.fetch_optional(&mut *tx) |
| 155 |
.await?; |
| 156 |
if exists.is_some() { |
| 157 |
tx.commit().await?; |
| 158 |
return Ok(BlobConfirm::AlreadyStored); |
| 159 |
} |
| 160 |
|
| 161 |
let used = sum_blob_bytes(&mut *tx, app_id, user_id).await?; |
| 162 |
let limit = limit.unwrap_or(0); |
| 163 |
if used.saturating_add(size_bytes) > limit { |
| 164 |
return Ok(BlobConfirm::QuotaExceeded { |
| 165 |
dimension: "storage", |
| 166 |
used, |
| 167 |
limit, |
| 168 |
key: None, |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
insert_blob_tx(&mut tx, app_id, user_id, hash, size_bytes, s3_key, key).await?; |
| 173 |
tx.commit().await?; |
| 174 |
Ok(BlobConfirm::Stored) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
#[tracing::instrument(skip_all)] |
| 184 |
#[allow(clippy::too_many_arguments)] |
| 185 |
pub async fn confirm_developer_blob( |
| 186 |
pool: &PgPool, |
| 187 |
app_id: SyncAppId, |
| 188 |
user_id: UserId, |
| 189 |
hash: &str, |
| 190 |
size_bytes: i64, |
| 191 |
s3_key: &str, |
| 192 |
key: &str, |
| 193 |
enforcement_mode: crate::db::SyncEnforcementMode, |
| 194 |
storage_gb_cap: Option<i32>, |
| 195 |
key_cap: Option<i32>, |
| 196 |
gb_per_key: Option<i32>, |
| 197 |
) -> Result<BlobConfirm> { |
| 198 |
let mut tx = pool.begin().await?; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
let app_used: i64 = sqlx::query_scalar( |
| 203 |
"SELECT bytes_stored FROM sync_app_usage_current WHERE app_id = $1 FOR UPDATE", |
| 204 |
) |
| 205 |
.bind(app_id) |
| 206 |
.fetch_one(&mut *tx) |
| 207 |
.await?; |
| 208 |
|
| 209 |
|
| 210 |
let exists: Option<i32> = sqlx::query_scalar( |
| 211 |
"SELECT 1 FROM sync_blobs WHERE app_id = $1 AND user_id = $2 AND hash = $3", |
| 212 |
) |
| 213 |
.bind(app_id) |
| 214 |
.bind(user_id) |
| 215 |
.bind(hash) |
| 216 |
.fetch_optional(&mut *tx) |
| 217 |
.await?; |
| 218 |
if exists.is_some() { |
| 219 |
tx.commit().await?; |
| 220 |
return Ok(BlobConfirm::AlreadyStored); |
| 221 |
} |
| 222 |
|
| 223 |
match enforcement_mode { |
| 224 |
crate::db::SyncEnforcementMode::Bulk => { |
| 225 |
if let Some(gb) = storage_gb_cap { |
| 226 |
let limit = crate::synckit_billing::storage_cap_bytes(gb as u32); |
| 227 |
if app_used.saturating_add(size_bytes) > limit { |
| 228 |
return Ok(BlobConfirm::QuotaExceeded { |
| 229 |
dimension: "storage", |
| 230 |
used: app_used, |
| 231 |
limit, |
| 232 |
key: None, |
| 233 |
}); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
crate::db::SyncEnforcementMode::PerKey => { |
| 238 |
if let (Some(kc), Some(g)) = (key_cap, gb_per_key) { |
| 239 |
let per_key_limit = crate::synckit_billing::storage_cap_bytes(g as u32); |
| 240 |
let app_limit = |
| 241 |
crate::synckit_billing::storage_cap_bytes(kc.saturating_mul(g) as u32); |
| 242 |
let key_used: i64 = sqlx::query_scalar( |
| 243 |
"SELECT bytes_stored FROM sync_key_usage_current |
| 244 |
WHERE app_id = $1 AND key = $2 FOR UPDATE", |
| 245 |
) |
| 246 |
.bind(app_id) |
| 247 |
.bind(key) |
| 248 |
.fetch_optional(&mut *tx) |
| 249 |
.await? |
| 250 |
.unwrap_or(0); |
| 251 |
if key_used.saturating_add(size_bytes) > per_key_limit { |
| 252 |
return Ok(BlobConfirm::QuotaExceeded { |
| 253 |
dimension: "storage_per_key", |
| 254 |
used: key_used, |
| 255 |
limit: per_key_limit, |
| 256 |
key: Some(key.to_string()), |
| 257 |
}); |
| 258 |
} |
| 259 |
|
| 260 |
if app_used.saturating_add(size_bytes) > app_limit { |
| 261 |
return Ok(BlobConfirm::QuotaExceeded { |
| 262 |
dimension: "storage", |
| 263 |
used: app_used, |
| 264 |
limit: app_limit, |
| 265 |
key: None, |
| 266 |
}); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
insert_blob_tx(&mut tx, app_id, user_id, hash, size_bytes, s3_key, key).await?; |
| 273 |
|
| 274 |
|
| 275 |
sqlx::query( |
| 276 |
"UPDATE sync_app_usage_current |
| 277 |
SET bytes_stored = GREATEST(bytes_stored + $2, 0), updated_at = NOW() |
| 278 |
WHERE app_id = $1", |
| 279 |
) |
| 280 |
.bind(app_id) |
| 281 |
.bind(size_bytes) |
| 282 |
.execute(&mut *tx) |
| 283 |
.await?; |
| 284 |
sqlx::query( |
| 285 |
"INSERT INTO sync_key_usage_current (app_id, key, bytes_stored) |
| 286 |
VALUES ($1, $2, GREATEST($3, 0)) |
| 287 |
ON CONFLICT (app_id, key) |
| 288 |
DO UPDATE SET bytes_stored = GREATEST(sync_key_usage_current.bytes_stored + $3, 0), |
| 289 |
updated_at = NOW()", |
| 290 |
) |
| 291 |
.bind(app_id) |
| 292 |
.bind(key) |
| 293 |
.bind(size_bytes) |
| 294 |
.execute(&mut *tx) |
| 295 |
.await?; |
| 296 |
|
| 297 |
tx.commit().await?; |
| 298 |
Ok(BlobConfirm::Stored) |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 303 |
pub enum BlobDelete { |
| 304 |
|
| 305 |
Deleted { size_bytes: i64 }, |
| 306 |
|
| 307 |
NotFound, |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
#[tracing::instrument(skip_all)] |
| 323 |
pub async fn delete_sync_blob( |
| 324 |
pool: &PgPool, |
| 325 |
app_id: SyncAppId, |
| 326 |
user_id: UserId, |
| 327 |
hash: &str, |
| 328 |
) -> Result<BlobDelete> { |
| 329 |
let mut tx = pool.begin().await?; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
let row: Option<(i64, String, String)> = sqlx::query_as( |
| 335 |
"DELETE FROM sync_blobs WHERE app_id = $1 AND user_id = $2 AND hash = $3 |
| 336 |
RETURNING size_bytes, s3_key, key", |
| 337 |
) |
| 338 |
.bind(app_id) |
| 339 |
.bind(user_id) |
| 340 |
.bind(hash) |
| 341 |
.fetch_optional(&mut *tx) |
| 342 |
.await?; |
| 343 |
|
| 344 |
let Some((size_bytes, s3_key, key)) = row else { |
| 345 |
tx.commit().await?; |
| 346 |
return Ok(BlobDelete::NotFound); |
| 347 |
}; |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
sqlx::query( |
| 353 |
"UPDATE sync_app_usage_current |
| 354 |
SET bytes_stored = GREATEST(bytes_stored - $2, 0), updated_at = NOW() |
| 355 |
WHERE app_id = $1", |
| 356 |
) |
| 357 |
.bind(app_id) |
| 358 |
.bind(size_bytes) |
| 359 |
.execute(&mut *tx) |
| 360 |
.await?; |
| 361 |
sqlx::query( |
| 362 |
"UPDATE sync_key_usage_current |
| 363 |
SET bytes_stored = GREATEST(bytes_stored - $3, 0), updated_at = NOW() |
| 364 |
WHERE app_id = $1 AND key = $2", |
| 365 |
) |
| 366 |
.bind(app_id) |
| 367 |
.bind(&key) |
| 368 |
.bind(size_bytes) |
| 369 |
.execute(&mut *tx) |
| 370 |
.await?; |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
crate::db::pending_s3_deletions::enqueue_deletions( |
| 377 |
&mut *tx, |
| 378 |
&[(s3_key, "synckit".to_string())], |
| 379 |
"synckit_blob_delete", |
| 380 |
) |
| 381 |
.await?; |
| 382 |
|
| 383 |
tx.commit().await?; |
| 384 |
Ok(BlobDelete::Deleted { size_bytes }) |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
#[tracing::instrument(skip_all)] |
| 389 |
pub async fn count_sync_devices(pool: &PgPool, app_id: SyncAppId, user_id: UserId) -> Result<i64> { |
| 390 |
let count: i64 = |
| 391 |
sqlx::query_scalar("SELECT COUNT(*) FROM sync_devices WHERE app_id = $1 AND user_id = $2") |
| 392 |
.bind(app_id) |
| 393 |
.bind(user_id) |
| 394 |
.fetch_one(pool) |
| 395 |
.await?; |
| 396 |
|
| 397 |
Ok(count) |
| 398 |
} |
| 399 |
|