🤖 answer a contended thread delete 204, not 500
Browse files at this commitc998c0d62b34e04e637a3781ed9045b925ae3effParent: 2351107- fix
- 🤖 answer a contended thread delete 204, not 500
1 changed file +30 -13
- src/domain/chatbot_thread.rs +30 -13
src/domain/chatbot_thread.rsmodified+30 -13
| @@ -289,20 +289,37 @@ impl ChatbotThread { | |||
| 289 | /// Delete the thread and every turn in it — one transaction, so a crash | 289 | /// Delete the thread and every turn in it — one transaction, so a crash |
| 290 | /// can't orphan messages under a vanished thread. The owner's slot comes | 290 | /// can't orphan messages under a vanished thread. The owner's slot comes |
| 291 | /// back in that same transaction, or the cap would ratchet shut. | 291 | /// back in that same transaction, or the cap would ratchet shut. |
| 292 | + /// | ||
| 293 | + /// A turn, a rename and this delete all write the thread row (see | ||
| 294 | + /// [`touch_and_write`]), so the store aborting one of them is ordinary | ||
| 295 | + /// here — that collision is exactly what keeps a turn from outliving the | ||
| 296 | + /// thread — and a lost round is re-sent rather than reported as the `500` | ||
| 297 | + /// it used to be. Re-sending is sound: every statement is a `DELETE` or a | ||
| 298 | + /// field-scoped `UPDATE`, none of which can ever answer "already exists" | ||
| 299 | + /// (see [`transaction_with_retry`]). On the re-sent round the thread is | ||
| 300 | + /// already gone, so the `RETURN` is empty and the caller gets the `404` | ||
| 301 | + /// that is the truth. | ||
| 292 | pub async fn delete(self, db: &Database) -> Result<ChatbotThread, AppError> { | 302 | pub async fn delete(self, db: &Database) -> Result<ChatbotThread, AppError> { |
| 293 | - let mut result = db | 303 | + let (mut result, mut errors) = transaction_with_retry( |
| 294 | - .query( | 304 | + db, |
| 295 | - "BEGIN TRANSACTION; | 305 | + "BEGIN TRANSACTION; |
| 296 | - DELETE chatbot_message WHERE thread_id = $conv; | 306 | + DELETE chatbot_message WHERE thread_id = $conv; |
| 297 | - LET $gone = (DELETE $conv RETURN BEFORE); | 307 | + LET $gone = (DELETE $conv RETURN BEFORE); |
| 298 | - UPDATE $usr SET chatbot_thread_count = math::max([(chatbot_thread_count ?? 0) - array::len($gone), 0]); | 308 | + UPDATE $usr SET chatbot_thread_count = math::max([(chatbot_thread_count ?? 0) - array::len($gone), 0]); |
| 299 | - RETURN $gone; | 309 | + RETURN $gone; |
| 300 | - COMMIT TRANSACTION;", | 310 | + COMMIT TRANSACTION;", |
| 301 | - ) | 311 | + &[ |
| 302 | - .bind(("conv", self.id.record())) | 312 | + ("conv".into(), self.id.record().into_value()), |
| 303 | - .bind(("usr", self.user_id.record())) | 313 | + ("usr".into(), self.user_id.record().into_value()), |
| 304 | - .await? | 314 | + ], |
| 305 | - .check()?; | 315 | + &[], |
| 316 | + ) | ||
| 317 | + .await?; | ||
| 318 | + if let Some(error) = errors.drain().map(|(_, error)| error).next() { | ||
| 319 | + return Err(error.into()); | ||
| 320 | + } | ||
| 321 | + // Slots count BEGIN, the cascade, the LET and the UPDATE: the RETURN | ||
| 322 | + // is slot 4. | ||
| 306 | let deleted: Option<ChatbotThread> = | 323 | let deleted: Option<ChatbotThread> = |
| 307 | result.take::<Vec<ChatbotThread>>(4)?.into_iter().next(); | 324 | result.take::<Vec<ChatbotThread>>(4)?.into_iter().next(); |
| 308 | deleted.ok_or(AppError::NotFound) | 325 | deleted.ok_or(AppError::NotFound) |