diff options
| author | Marie <github@yuugi.dev> | 2025-03-19 17:40:49 +0000 |
|---|---|---|
| committer | Marie <github@yuugi.dev> | 2025-03-19 17:40:49 +0000 |
| commit | 65d5f1e081895ec4698cb30d8201a7675d1f8708 (patch) | |
| tree | 8c2d4e19d5e71aca6b41d33a2a7582b0c7f7e5c9 | |
| parent | merge: fix notification dot (!946) (diff) | |
| parent | adjust compression fallback (diff) | |
| download | sharkey-65d5f1e081895ec4698cb30d8201a7675d1f8708.tar.gz sharkey-65d5f1e081895ec4698cb30d8201a7675d1f8708.tar.bz2 sharkey-65d5f1e081895ec4698cb30d8201a7675d1f8708.zip | |
merge: Optionally enable websocket compression (!921)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/921
Approved-by: dakkar <dakkar@thenautilus.net>
Approved-by: Marie <github@yuugi.dev>
| -rw-r--r-- | .config/example.yml | 6 | ||||
| -rw-r--r-- | packages/backend/src/config.ts | 7 | ||||
| -rw-r--r-- | packages/backend/src/server/api/StreamingApiServerService.ts | 5 |
3 files changed, 17 insertions, 1 deletions
diff --git a/.config/example.yml b/.config/example.yml index e18afd615b..f3caa0d676 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -419,3 +419,9 @@ attachLdSignatureForRelays: true # How long to save each log entry before deleting it. # Default: 2592000000 (1 week) #maxAge: 2592000000 + +# Transparently compress every websocket message on clients that support it. +# Trades server CPU usage for reduced bandwidth usage and a faster frontend on the client. +# If you're not using jemalloc, this may cause memory fragmentation and performance issues! (https://www.npmjs.com/package/ws#websocket-compression) +# jemalloc is used by default in the Sharkey Docker image and may be set up manually otherwise: https://github.com/jemalloc/jemalloc/wiki/getting-started +websocketCompression: false diff --git a/packages/backend/src/config.ts b/packages/backend/src/config.ts index 61c7fcb6c7..067672fd79 100644 --- a/packages/backend/src/config.ts +++ b/packages/backend/src/config.ts @@ -136,6 +136,8 @@ type Source = { preSave?: boolean; maxAge?: number; }; + + websocketCompression?: boolean; }; export type Config = { @@ -253,6 +255,8 @@ export type Config = { preSave: boolean; maxAge: number; }; + + websocketCompression?: boolean; }; export type FulltextSearchProvider = 'sqlLike' | 'sqlPgroonga' | 'meilisearch' | 'sqlTsvector'; @@ -401,6 +405,7 @@ export function loadConfig(): Config { preSave: config.activityLogging?.preSave ?? false, maxAge: config.activityLogging?.maxAge ?? (1000 * 60 * 60 * 24 * 30), }, + websocketCompression: config.websocketCompression ?? false, }; } @@ -535,7 +540,7 @@ function applyEnvOverrides(config: Source) { // these are all the settings that can be overridden - _apply_top([['url', 'port', 'address', 'socket', 'chmodSocket', 'disableHsts', 'id', 'dbReplications']]); + _apply_top([['url', 'port', 'address', 'socket', 'chmodSocket', 'disableHsts', 'id', 'dbReplications', 'websocketCompression']]); _apply_top(['db', ['host', 'port', 'db', 'user', 'pass', 'disableCache']]); _apply_top(['dbSlaves', Array.from((config.dbSlaves ?? []).keys()), ['host', 'port', 'db', 'user', 'pass']]); _apply_top([ diff --git a/packages/backend/src/server/api/StreamingApiServerService.ts b/packages/backend/src/server/api/StreamingApiServerService.ts index 6e7abcfae6..0954744f81 100644 --- a/packages/backend/src/server/api/StreamingApiServerService.ts +++ b/packages/backend/src/server/api/StreamingApiServerService.ts @@ -26,6 +26,7 @@ import MainStreamConnection from './stream/Connection.js'; import { ChannelsService } from './stream/ChannelsService.js'; import type * as http from 'node:http'; import type { IEndpointMeta } from './endpoints.js'; +import type { Config } from "@/config.js"; @Injectable() export class StreamingApiServerService { @@ -49,6 +50,9 @@ export class StreamingApiServerService { private channelFollowingService: ChannelFollowingService, private rateLimiterService: SkRateLimiterService, private loggerService: LoggerService, + + @Inject(DI.config) + private config: Config, ) { } @@ -74,6 +78,7 @@ export class StreamingApiServerService { public attach(server: http.Server): void { this.#wss = new WebSocket.WebSocketServer({ noServer: true, + perMessageDeflate: this.config.websocketCompression, }); server.on('upgrade', async (request, socket, head) => { |