A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
perf(api): cap the sharp cache and log host sizing at startup
Hampus00ad838179e6
diff --git a/fluxer_api/src/App.ts b/fluxer_api/src/App.tsindex 4d997fd5b..c0585e472 100644--- a/fluxer_api/src/App.ts+++ b/fluxer_api/src/App.ts@@ -4,6 +4,7 @@ import {createAPIApp} from '@app/api/App';import {OPERATOR_SOCKET_PATH} from '@app/api/admin/operator/OperatorRequests';import {listenOperatorSocket} from '@app/api/admin/operator/OperatorSocket';import {buildAPIServerOptions, initializeConfig} from '@app/api/Config';+import {sizeProcessForHost} from '@app/api/infrastructure/HostResources';import {initializeLogger} from '@app/api/Logger';import {Config} from '@app/Config';import {Logger} from '@app/Logger';@@ -24,6 +25,7 @@ async function closeHttpServer(server: {close: (callback: (error?: Error) => voiasync function main(): Promise<void> {initializeConfig(Config);initializeLogger(Logger);+ sizeProcessForHost(Logger);const {app, initialize, shutdown} = await createAPIApp({config: Config,logger: Logger,diff --git a/fluxer_api/src/api/infrastructure/HostResources.ts b/fluxer_api/src/api/infrastructure/HostResources.tsnew file mode 100644index 000000000..d9eb69d11--- /dev/null+++ b/fluxer_api/src/api/infrastructure/HostResources.ts@@ -0,0 +1,41 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {availableParallelism, totalmem} from 'node:os';+import {getHeapStatistics} from 'node:v8';+import type {LoggerInterface} from '@fluxer/logger/src/LoggerInterface';+import sharp from 'sharp';++const MIB = 1024 * 1024;+const SHARP_CACHE_MEMORY_MB = 8;+const SHARP_CACHE_ITEMS = 20;++export interface HostResources {+ cores: number;+ memoryBytes: number;+}++function detectHostResources(): HostResources {+ const total = totalmem();+ const constrained = process.constrainedMemory();+ return {+ cores: availableParallelism(),+ memoryBytes: constrained > 0 ? Math.min(constrained, total) : total,+ };+}++export function sizeProcessForHost(logger: Pick<LoggerInterface, 'info'>): HostResources {+ const resources = detectHostResources();+ sharp.cache({memory: SHARP_CACHE_MEMORY_MB, items: SHARP_CACHE_ITEMS, files: 0});+ const sharpConcurrency = sharp.concurrency(Math.min(sharp.concurrency(), resources.cores));+ logger.info(+ {+ cores: resources.cores,+ memoryMb: Math.round(resources.memoryBytes / MIB),+ heapLimitMb: Math.round(getHeapStatistics().heap_size_limit / MIB),+ sharpConcurrency,+ sharpCacheMb: SHARP_CACHE_MEMORY_MB,+ },+ 'Sized the process for the host',+ );+ return resources;+}diff --git a/fluxer_api/src/api/worker/WorkerMain.ts b/fluxer_api/src/api/worker/WorkerMain.tsindex 5fd51d1a1..2d5ddc58d 100644--- a/fluxer_api/src/api/worker/WorkerMain.ts+++ b/fluxer_api/src/api/worker/WorkerMain.ts@@ -12,6 +12,7 @@ import {} from '@app/api/infrastructure/activity/ActivityEvents';import {setActivityProcessChannel} from '@app/api/infrastructure/activity/ActivityMeta';import {startSharedListWatch, stopSharedListWatch} from '@app/api/infrastructure/activity/SharedLists';+import {sizeProcessForHost} from '@app/api/infrastructure/HostResources';import type {ISnowflakeService} from '@app/api/infrastructure/ISnowflakeService';import {shutdownStorageChangeFeed} from '@app/api/infrastructure/StorageServiceFactory';import type {InstanceConfigRepository} from '@app/api/instance/InstanceConfigRepository';@@ -157,6 +158,7 @@ function registerCronJobs(cron: CronScheduler, jobsStreamMaxAgeMs: number, kvCliexport async function startWorkerMain(): Promise<void> {Logger.info('Starting worker backend...');+ sizeProcessForHost(Logger);let cassandraInitialized = false;let postgresInitialized = false;let jsConnectionManager: JetStreamConnectionManager | null = null;