Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
- Fix: サーバーメトリクスが90度傾いている
- Fix: sparkle内にリンクを入れるとクリック不能になる問題の修正

### Server
- JSON.parse の回数を削減することで、ストリーミングのパフォーマンスを向上しました

## 13.13.2

### General
Expand Down
16 changes: 11 additions & 5 deletions packages/backend/src/server/api/StreamingApiServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
stream: MainStreamConnection,
user: LocalUser | null;
Expand All @@ -112,12 +119,11 @@ export class StreamingApiServerService {

const ev = new EventEmitter();

async function onRedisMessage(_: string, data: string): Promise<void> {
const parsed = JSON.parse(data);
ev.emit(parsed.channel, parsed.message);
function onRedisMessage(data: any): void {
ev.emit(data.channel, data.message);
}

this.redisForSub.on('message', onRedisMessage);
globalEv.on('message', onRedisMessage);

await stream.listen(ev, connection);

Expand All @@ -137,7 +143,7 @@ export class StreamingApiServerService {
connection.once('close', () => {
ev.removeAllListeners();
stream.dispose();
this.redisForSub.off('message', onRedisMessage);
globalEv.off('message', onRedisMessage);
this.#connections.delete(connection);
if (userUpdateIntervalId) clearInterval(userUpdateIntervalId);
});
Expand Down