1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-11-27 14:28:49 +09:00

perf(backend): JSON.parse の呼び出しを削減する (#11091)

* perf(backend): JSON.parse の呼び出しを削減する

Co-authored-by: Hidekazu Kobayashi <kobahide789@gmail.com>

* Update CHANGELOG.md

---------

Co-authored-by: Hidekazu Kobayashi <kobahide789@gmail.com>
This commit is contained in:
riku6460 2023-07-04 07:49:13 +09:00 committed by GitHub
parent 84d3a06637
commit 61e7eb8ff1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -22,6 +22,9 @@
- Fix: サーバーメトリクスが90度傾いている - Fix: サーバーメトリクスが90度傾いている
- Fix: sparkle内にリンクを入れるとクリック不能になる問題の修正 - Fix: sparkle内にリンクを入れるとクリック不能になる問題の修正
### Server
- JSON.parse の回数を削減することで、ストリーミングのパフォーマンスを向上しました
## 13.13.2 ## 13.13.2
### General ### General

View File

@ -103,6 +103,13 @@ export class StreamingApiServerService {
}); });
}); });
const globalEv = new EventEmitter();
this.redisForSub.on('message', (_: string, data: string) => {
const parsed = JSON.parse(data);
globalEv.emit('message', parsed);
});
this.#wss.on('connection', async (connection: WebSocket.WebSocket, request: http.IncomingMessage, ctx: { this.#wss.on('connection', async (connection: WebSocket.WebSocket, request: http.IncomingMessage, ctx: {
stream: MainStreamConnection, stream: MainStreamConnection,
user: LocalUser | null; user: LocalUser | null;
@ -112,12 +119,11 @@ export class StreamingApiServerService {
const ev = new EventEmitter(); const ev = new EventEmitter();
async function onRedisMessage(_: string, data: string): Promise<void> { function onRedisMessage(data: any): void {
const parsed = JSON.parse(data); ev.emit(data.channel, data.message);
ev.emit(parsed.channel, parsed.message);
} }
this.redisForSub.on('message', onRedisMessage); globalEv.on('message', onRedisMessage);
await stream.listen(ev, connection); await stream.listen(ev, connection);
@ -137,7 +143,7 @@ export class StreamingApiServerService {
connection.once('close', () => { connection.once('close', () => {
ev.removeAllListeners(); ev.removeAllListeners();
stream.dispose(); stream.dispose();
this.redisForSub.off('message', onRedisMessage); globalEv.off('message', onRedisMessage);
this.#connections.delete(connection); this.#connections.delete(connection);
if (userUpdateIntervalId) clearInterval(userUpdateIntervalId); if (userUpdateIntervalId) clearInterval(userUpdateIntervalId);
}); });