Commit attachment retry reservations before network delivery

This commit is contained in:
evgeny committed 2026-10-27 03:00:30 +01:00
1 parent 5a6a209436
commit 74fccbc5da
2 files changed
+122 -29

No files matched your search

+72 -23
View File
@@ -37,21 +37,16 @@ struct ak_file {
/* Все ошибки SQLite содержат конкретную операцию. */
static sqlite3_stmt* ak_prepare(struct UTUN_INSTANCE* inst, const char* sql) {
sqlite3_stmt* st = NULL;
if (!CC(inst) || sqlite3_prepare_v2(CC(inst)->db, sql, -1, &st, NULL) != SQLITE_OK)
if (!inst || !CC(inst) || sqlite3_prepare_v2(CC(inst)->db, sql, -1, &st, NULL) != SQLITE_OK) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment keys: prepare failed sql=%s error=%s", sql,
CC(inst) ? sqlite3_errmsg(CC(inst)->db) : "stopped");
inst && CC(inst) ? sqlite3_errmsg(CC(inst)->db) : "stopped");
int rc = sqlite3_finalize(st);
if (rc != SQLITE_OK) DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment keys: prepare cleanup finalize rc=%d", rc);
return NULL;
}
return st;
}
/* Statement всегда освобождается. */
static int ak_step(struct UTUN_INSTANCE* inst, sqlite3_stmt* st) {
if (!st) return -1;
int rc = sqlite3_step(st);
if (rc != SQLITE_DONE) DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment keys: write failed rc=%d: %s", rc, sqlite3_errmsg(CC(inst)->db));
sqlite3_finalize(st);
return rc == SQLITE_DONE ? 0 : -1;
}
/* Управление транзакциями без открытых statement. */
static int ak_exec(struct UTUN_INSTANCE* inst, const char* sql) {
int rc = sqlite3_exec(CC(inst)->db, sql, NULL, NULL, NULL);
@@ -1053,21 +1048,72 @@ fail:
ak_rollback(inst, "downloaded", channel); return -1;
}
/* Выбрать команду и зафиксировать следующую попытку до сети; 1 — команда, 0 — нет, -1 — ошибка. */
static int ak_next_command(struct UTUN_INSTANCE* inst, char channel[32], uint8_t message[32], uint64_t* peer, int* cmd) {
sqlite3* db = CC(inst)->db; sqlite3_stmt* st = NULL;
if (!sqlite3_get_autocommit(db)) {
DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "attachment keys: outbox postponed inside external transaction"); return -1;
}
const char* stage = "begin outbox";
int rc = sqlite3_exec(db, "BEGIN IMMEDIATE /* attachment outbox */", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto fail;
stage = "prepare outbox";
rc = sqlite3_prepare_v2(db, "SELECT channel,message_id,peer,command FROM attachment_key_outbox"
" WHERE retry_at<=unixepoch() ORDER BY retry_at,rowid LIMIT 1", -1, &st, NULL);
if (rc != SQLITE_OK) goto fail;
stage = "read outbox"; rc = sqlite3_step(st);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) goto fail;
int found = rc == SQLITE_ROW;
if (found) {
const char* name = (const char*)sqlite3_column_text(st, 0);
if (!ak_channel(name) || sqlite3_column_bytes(st, 1) != 32 || !sqlite3_column_blob(st, 1)) {
stage = "outbox identity"; rc = SQLITE_MISMATCH; goto fail;
}
snprintf(channel, 32, "%s", name); memcpy(message, sqlite3_column_blob(st, 1), 32);
*peer = (uint64_t)sqlite3_column_int64(st, 2); *cmd = sqlite3_column_int(st, 3);
if (!*peer || *peer == inst->node_id || (*cmd != AK_REQUEST && *cmd != AK_GRANT && *cmd != AK_DENY)) {
stage = "outbox peer/command"; rc = SQLITE_MISMATCH; goto fail;
}
}
stage = "finalize outbox"; rc = sqlite3_finalize(st); st = NULL;
if (rc != SQLITE_OK) goto fail;
if (found) {
stage = "prepare retry";
rc = sqlite3_prepare_v2(db, "UPDATE attachment_key_outbox SET retry_at=unixepoch()+5 WHERE channel=? AND message_id=? AND peer=?",
-1, &st, NULL);
if (rc != SQLITE_OK) goto fail;
stage = "bind channel"; rc = sqlite3_bind_text(st, 1, channel, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) goto fail;
stage = "bind message"; rc = sqlite3_bind_blob(st, 2, message, 32, SQLITE_STATIC);
if (rc != SQLITE_OK) goto fail;
stage = "bind peer"; rc = sqlite3_bind_int64(st, 3, (sqlite3_int64)*peer);
if (rc != SQLITE_OK) goto fail;
stage = "write retry"; rc = sqlite3_step(st);
if (rc != SQLITE_DONE) goto fail;
int changed = sqlite3_changes(db);
stage = "finalize retry"; rc = sqlite3_finalize(st); st = NULL;
if (rc != SQLITE_OK) goto fail;
if (changed != 1) { stage = "row count"; rc = SQLITE_CONSTRAINT; goto fail; }
}
stage = "commit outbox"; rc = sqlite3_exec(db, "COMMIT /* attachment outbox */", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto fail;
if (found) DEBUG_DEBUG(DEBUG_CATEGORY_MEDIA, "attachment keys: retry committed channel=%s peer=%llu command=%d",
channel, (unsigned long long)*peer, *cmd);
return found;
fail:
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment keys: outbox channel=%s stage=%s rc=%d (%s) db=%s",
channel, stage, rc, sqlite3_errstr(rc), sqlite3_errmsg(db));
int final_rc = sqlite3_finalize(st);
if (final_rc != SQLITE_OK) DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment keys: outbox cleanup finalize rc=%d", final_rc);
ak_rollback(inst, "outbox", channel); return -1;
}
/* Один проход ограничен; retry_at даёт всем задачам очередь без голодания. */
static void ak_pump(struct UTUN_INSTANCE* inst) {
for (unsigned i = 0; i < 16; i++) {
sqlite3_stmt* st = ak_prepare(inst, "SELECT channel,message_id,peer,command FROM attachment_key_outbox"
" WHERE retry_at<=unixepoch() ORDER BY retry_at,rowid LIMIT 1");
if (!st) return;
int rc = sqlite3_step(st);
if (rc != SQLITE_ROW) { sqlite3_finalize(st); if (rc != SQLITE_DONE) DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment keys: outbox read failed rc=%d", rc); return; }
char channel[32]; uint8_t message[32], wire[AK_WIRE];
snprintf(channel, sizeof(channel), "%s", sqlite3_column_text(st, 0)); memcpy(message, sqlite3_column_blob(st, 1), 32);
uint64_t peer = (uint64_t)sqlite3_column_int64(st, 2); int cmd = sqlite3_column_int(st, 3); sqlite3_finalize(st);
st = ak_prepare(inst, "UPDATE attachment_key_outbox SET retry_at=unixepoch()+5 WHERE channel=? AND message_id=? AND peer=?");
if (!st) return;
sqlite3_bind_text(st, 1, channel, -1, SQLITE_STATIC); sqlite3_bind_blob(st, 2, message, 32, SQLITE_STATIC);
sqlite3_bind_int64(st, 3, (sqlite3_int64)peer); if (ak_step(inst, st)) return;
char channel[32] = {0}; uint8_t message[32], wire[AK_WIRE]; uint64_t peer = 0; int cmd = 0;
int rc = ak_next_command(inst, channel, message, &peer, &cmd);
if (rc <= 0) return;
if (cmd == AK_GRANT && !ak_authorized(inst, channel, message, peer)) continue;
int n = ak_body(inst, channel, message, (uint8_t)cmd, wire);
if (n > 0) {
@@ -1145,6 +1191,9 @@ fail:
static int ak_claim_decryption(struct ak_file* job) {
struct UTUN_INSTANCE* inst = job->inst;
sqlite3* db = CC(inst)->db; sqlite3_stmt* st = NULL;
if (!sqlite3_get_autocommit(db)) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment keys: claim rejected inside external transaction channel=%s", job->channel); return -1;
}
const char* stage = "begin claim";
int rc = sqlite3_exec(db, "BEGIN IMMEDIATE", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto fail;