A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
perf(api): run the legacy ttl passes once per database
Hampuscff6f403e40c
diff --git a/fluxer_api/src/api/database/PostgresKvDefaultTtlExpiry.ts b/fluxer_api/src/api/database/PostgresKvDefaultTtlExpiry.tsindex b70ff2882..3c806ba64 100644--- a/fluxer_api/src/api/database/PostgresKvDefaultTtlExpiry.ts+++ b/fluxer_api/src/api/database/PostgresKvDefaultTtlExpiry.ts@@ -2,7 +2,7 @@import {POSTGRES_KV_MIGRATION_TABLE,- postgresKvPassIsFresh,+ postgresKvPassRecorded,recordPostgresKvCleanPass,} from '@app/api/database/PostgresKvQueryExecutor';import * as DonationTables from '@app/api/donation/DonationTables';@@ -95,7 +95,7 @@ export async function expireLegacyDefaultTtlRows(client: IPostgresClient,deadlineMs: number,): Promise<LegacyDefaultTtlExpiryResult | null> {- if (await postgresKvPassIsFresh(client, DEFAULT_TTL_EXPIRY_MARKER, CLEAN_PASS_INTERVAL_MS)) {+ if (await postgresKvPassRecorded(client, DEFAULT_TTL_EXPIRY_MARKER)) {return null;}const kvTable = quoteIdentifier(client.kvTable());diff --git a/fluxer_api/src/api/database/PostgresKvQueryExecutor.ts b/fluxer_api/src/api/database/PostgresKvQueryExecutor.tsindex 963cfe0bc..4a1094d8c 100644--- a/fluxer_api/src/api/database/PostgresKvQueryExecutor.ts+++ b/fluxer_api/src/api/database/PostgresKvQueryExecutor.ts@@ -121,9 +121,10 @@ const NON_NUMERIC_ROW_KEY_RANGES = [`kv.row_key COLLATE "C" >= '${BIGINT_ROW_KEY_PREFIX}:'`,];+export async function postgresKvPassRecorded(client: IPostgresClient, marker: string): Promise<boolean> {const result = await client.query(- `SELECT 1 FROM ${quoteIdentifier(client.kvTable())} WHERE table_name = $1 AND row_key = $2 AND (row_data ->> 'applied_at')::timestamptz > now() - make_interval(secs => $3::double precision)`,- [POSTGRES_KV_MIGRATION_TABLE, marker, maxAgeMs / 1000],+ `SELECT 1 FROM ${quoteIdentifier(client.kvTable())} WHERE table_name = $1 AND row_key = $2`,+ [POSTGRES_KV_MIGRATION_TABLE, marker],);return result.rows.length > 0;}diff --git a/fluxer_api/src/api/jobs/PostgresJobLedgerExpiry.ts b/fluxer_api/src/api/jobs/PostgresJobLedgerExpiry.tsindex 322bf9ec8..70e5468ba 100644--- a/fluxer_api/src/api/jobs/PostgresJobLedgerExpiry.ts+++ b/fluxer_api/src/api/jobs/PostgresJobLedgerExpiry.ts@@ -1,15 +1,13 @@// SPDX-License-Identifier: AGPL-3.0-or-later-import {postgresKvPassIsFresh, recordPostgresKvCleanPass} from '@app/api/database/PostgresKvQueryExecutor';+import {postgresKvPassRecorded, recordPostgresKvCleanPass} from '@app/api/database/PostgresKvQueryExecutor';import {EXPIRED_JOB_ERROR, JOB_LEDGER_TTL_SECONDS, JOB_STALE_AFTER_MS} from '@app/api/jobs/JobLedgerRepository';import {FLUXER_EPOCH} from '@fluxer/constants/src/Core';import {TIMESTAMP_SHIFT} from '@fluxer/snowflake/src/Snowflake';import {type IPostgresClient, quoteIdentifier} from '@pkgs/postgres/src/Client';-import {ms} from 'itty-time';const LEGACY_JOB_LEDGER_MARKER = 'job_ledger_expiry_v1';const PAGE_SIZE = 2000;-const CLEAN_PASS_INTERVAL_MS = ms('1 day');const BIGINT_KEY_PREFIX = '{"__fluxer_type":"bigint","value":"';const BIGINT_KEY_SUFFIX = '"}';@@ -98,7 +96,7 @@ export async function expireLegacyJobLedgerRows(deadlineMs: number,): Promise<LegacyJobLedgerExpiryResult | null> {const table = quoteIdentifier(client.kvTable());- if (await postgresKvPassIsFresh(client, LEGACY_JOB_LEDGER_MARKER, CLEAN_PASS_INTERVAL_MS)) {+ if (await postgresKvPassRecorded(client, LEGACY_JOB_LEDGER_MARKER)) {return null;}const now = Date.now();