Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

perf(api): probe missing activity streams slowly on self-host

Hampus5444834e81de
diff --git a/fluxer_api/src/api/app/APILifecycle.ts b/fluxer_api/src/api/app/APILifecycle.ts
index 91f760cb0..73c645e14 100644
--- a/fluxer_api/src/api/app/APILifecycle.ts
+++ b/fluxer_api/src/api/app/APILifecycle.ts
@@ -204,7 +204,7 @@ export function createInitializer(
spoolWhileMissing: !config.instance.selfHosted,
});
startRequestErrorTelemetry();
- startSharedListWatch(connection.getJetStreamClient());
+ startSharedListWatch(connection.getJetStreamClient(), {selfHosted: config.instance.selfHosted});
unregisterMetricsSections.push(
registerMetricsSection(renderActivityMetrics),
registerMetricsSection(renderSharedListMetrics),
diff --git a/fluxer_api/src/api/infrastructure/activity/ActivityEvents.ts b/fluxer_api/src/api/infrastructure/activity/ActivityEvents.ts
index 9b1296ee8..ee5920b52 100644
--- a/fluxer_api/src/api/infrastructure/activity/ActivityEvents.ts
+++ b/fluxer_api/src/api/infrastructure/activity/ActivityEvents.ts
@@ -35,6 +35,7 @@ const RETRY_DELAYS_MS = [100, 400];
const QUEUE_CAPACITY = 20_000;
const QUEUE_CONCURRENCY = 64;
const STREAM_PROBE_INTERVAL_MS = 15_000;
+const STREAM_IDLE_PROBE_INTERVAL_MS = 300_000;
const STREAM_REPROBE_INTERVAL_MS = 2_000;
const STREAM_NOT_FOUND = 10059;
const SPOOL_DEPTH_INTERVAL_MS = 15_000;
@@ -227,6 +228,10 @@ export async function emitActivity<K extends EventKind>(
}
}
+export function activityEventsPublishing(): boolean {
+ return runtime?.mode() === 'publish';
+}
+
export function idleActivityEvents(): Promise<void> {
if (inFlight === 0 && queue.length === 0) return Promise.resolve();
return new Promise((resolve) => {
@@ -275,10 +280,12 @@ export async function startActivityEvents(options: ActivityEventsOptions): Promi
let state: StreamState = jsm ? await probeStream(jsm) : 'present';
const timers: Array<ReturnType<typeof setTimeout>> = [];
const whenMissing: EmitMode = options.spoolWhileMissing ? 'spool' : 'off';
+ const missingProbeMs = options.spoolWhileMissing ? STREAM_PROBE_INTERVAL_MS : STREAM_IDLE_PROBE_INTERVAL_MS;
let stopped = false;
+ const mode = (): EmitMode => (state === 'missing' ? whenMissing : 'publish');
const schedule = (probe: Pick<JetStreamManager, 'streams'>) => {
if (stopped || state === 'present') return;
- const wait = state === 'unknown' ? STREAM_REPROBE_INTERVAL_MS : STREAM_PROBE_INTERVAL_MS;
+ const wait = state === 'unknown' ? STREAM_REPROBE_INTERVAL_MS : missingProbeMs;
const timer = setTimeout(() => {
void probeStream(probe).then((next) => {
if (next !== state) Logger.info({stream: EVENTS_STREAM, state: next}, 'Activity stream state changed');
@@ -294,6 +301,7 @@ export async function startActivityEvents(options: ActivityEventsOptions): Promi
schedule(jsm);
}
const depth = setInterval(() => {
+ if (mode() === 'off') return;
void options.kv
.llen(ACTIVITY_SPOOL_KEY)
.then((value) => {
@@ -306,7 +314,7 @@ export async function startActivityEvents(options: ActivityEventsOptions): Promi
runtime = {
publisher: options.publisher,
kv: options.kv,
- mode: () => (state === 'missing' ? whenMissing : 'publish'),
+ mode,
stop: () => {
stopped = true;
for (const timer of timers) {
diff --git a/fluxer_api/src/api/infrastructure/activity/SharedLists.ts b/fluxer_api/src/api/infrastructure/activity/SharedLists.ts
index c094d311d..5ed54082d 100644
--- a/fluxer_api/src/api/infrastructure/activity/SharedLists.ts
+++ b/fluxer_api/src/api/infrastructure/activity/SharedLists.ts
@@ -7,6 +7,7 @@ import {type ConsumerMessages, DeliverPolicy, type JetStreamClient} from '@nats-
export type SharedListName = (typeof SHARED_LISTS)[number];
const RETRY_DELAY_MS = 30_000;
+const SELF_HOSTED_RETRY_DELAY_MS = 300_000;
const STREAM_NAME = `KV_${LISTS_BUCKET}`;
const SUBJECT_PREFIX = `$KV.${LISTS_BUCKET}.`;
const KV_OPERATION_HEADER = 'KV-Operation';
@@ -14,6 +15,7 @@ const KV_OPERATION_HEADER = 'KV-Operation';
const lists = new Map<string, ReadonlySet<string>>();
interface WatchState {
stopped: boolean;
+ retryDelayMs: number;
messages: ConsumerMessages | null;
retry: ReturnType<typeof setTimeout> | null;
}
@@ -105,9 +107,14 @@ function scheduleWatch(js: JetStreamClient, state: WatchState, delayMs: number):
state.retry.unref?.();
}
-export function startSharedListWatch(js: JetStreamClient): void {
+export function startSharedListWatch(js: JetStreamClient, options: {selfHosted: boolean}): void {
stopSharedListWatch();
- const state: WatchState = {stopped: false, messages: null, retry: null};
+ const state: WatchState = {
+ stopped: false,
+ retryDelayMs: options.selfHosted ? SELF_HOSTED_RETRY_DELAY_MS : RETRY_DELAY_MS,
+ messages: null,
+ retry: null,
+ };
watcher = state;
scheduleWatch(js, state, 0);
}
diff --git a/fluxer_api/src/api/worker/WorkerMain.ts b/fluxer_api/src/api/worker/WorkerMain.ts
index 1261c0f06..d77d9ae73 100644
--- a/fluxer_api/src/api/worker/WorkerMain.ts
+++ b/fluxer_api/src/api/worker/WorkerMain.ts
@@ -311,7 +311,7 @@ export async function startWorkerMain(): Promise<void> {
jsm: await jsConnectionManager.getJetStreamManager(),
spoolWhileMissing: !Config.instance.selfHosted,
});
- startSharedListWatch(jsConnectionManager.getJetStreamClient());
+ startSharedListWatch(jsConnectionManager.getJetStreamClient(), {selfHosted: Config.instance.selfHosted});
if (activeWorkerLanes.some((lane) => lane.name === 'lifecycle')) {
const apiContext = createApiContext();
startAccountActionConsumer({