perf(backend): use websockets/ws instead of theturtle32/WebSocket-Node (#10884)

* perf(backend): use websockets/ws instead of theturtle32/WebSocket-Node

Resolve #10883

* refactor

* Update StreamingApiServerService.ts

* Update StreamingApiServerService.ts

* ✌️

* Update StreamingApiServerService.ts

* fix main stream init

* fix timing 2

* setIntervalの重複を避ける(気休め)

* add comment

* ✌️

---------

Co-authored-by: tamaina <tamaina@hotmail.co.jp>
This commit is contained in:
syuilo 2023-05-29 13:32:19 +09:00 committed by GitHub
parent b35b9bc27f
commit f930eaee02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 107 additions and 108 deletions

View file

@ -35,6 +35,7 @@
"@swc/core-win32-x64-msvc": "1.3.56",
"@tensorflow/tfjs": "4.4.0",
"@tensorflow/tfjs-node": "4.4.0",
"bufferutil": "^4.0.7",
"slacc-android-arm-eabi": "0.0.9",
"slacc-android-arm64": "0.0.9",
"slacc-darwin-arm64": "0.0.9",
@ -46,7 +47,8 @@
"slacc-linux-arm64-musl": "0.0.9",
"slacc-linux-x64-gnu": "0.0.9",
"slacc-win32-arm64-msvc": "0.0.9",
"slacc-win32-x64-msvc": "0.0.9"
"slacc-win32-x64-msvc": "0.0.9",
"utf-8-validate": "^6.0.3"
},
"dependencies": {
"@aws-sdk/client-s3": "3.321.1",
@ -157,7 +159,6 @@
"uuid": "9.0.0",
"vary": "1.1.2",
"web-push": "3.6.1",
"websocket": "1.0.34",
"ws": "8.13.0",
"xev": "3.0.2"
},

View file

@ -194,7 +194,7 @@ export class ServerService implements OnApplicationShutdown {
fastify.register(this.clientServerService.createServer);
this.streamingApiServerService.attachStreamingApi(fastify.server);
this.streamingApiServerService.attach(fastify.server);
fastify.server.on('error', err => {
switch ((err as any).code) {
@ -224,6 +224,7 @@ export class ServerService implements OnApplicationShutdown {
@bindThis
public async dispose(): Promise<void> {
await this.streamingApiServerService.detach();
await this.#fastify.close();
}

View file

@ -36,7 +36,7 @@ export class AuthenticateService {
}
@bindThis
public async authenticate(token: string | null | undefined): Promise<[LocalUser | null | undefined, AccessToken | null | undefined]> {
public async authenticate(token: string | null | undefined): Promise<[LocalUser | null, AccessToken | null]> {
if (token == null) {
return [null, null];
}

View file

@ -1,23 +1,25 @@
import { EventEmitter } from 'events';
import { Inject, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis';
import * as websocket from 'websocket';
import * as WebSocket from 'ws';
import { DI } from '@/di-symbols.js';
import type { UsersRepository, BlockingsRepository, ChannelFollowingsRepository, FollowingsRepository, MutingsRepository, UserProfilesRepository, RenoteMutingsRepository } from '@/models/index.js';
import type { UsersRepository, AccessToken } from '@/models/index.js';
import type { Config } from '@/config.js';
import { NoteReadService } from '@/core/NoteReadService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { NotificationService } from '@/core/NotificationService.js';
import { bindThis } from '@/decorators.js';
import { CacheService } from '@/core/CacheService.js';
import { AuthenticateService } from './AuthenticateService.js';
import { LocalUser } from '@/models/entities/User';
import { AuthenticateService, AuthenticationError } from './AuthenticateService.js';
import MainStreamConnection from './stream/index.js';
import { ChannelsService } from './stream/ChannelsService.js';
import type { ParsedUrlQuery } from 'querystring';
import type * as http from 'node:http';
@Injectable()
export class StreamingApiServerService {
#wss: WebSocket.WebSocketServer;
constructor(
@Inject(DI.config)
private config: Config,
@ -28,24 +30,6 @@ export class StreamingApiServerService {
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.followingsRepository)
private followingsRepository: FollowingsRepository,
@Inject(DI.mutingsRepository)
private mutingsRepository: MutingsRepository,
@Inject(DI.renoteMutingsRepository)
private renoteMutingsRepository: RenoteMutingsRepository,
@Inject(DI.blockingsRepository)
private blockingsRepository: BlockingsRepository,
@Inject(DI.channelFollowingsRepository)
private channelFollowingsRepository: ChannelFollowingsRepository,
@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,
private cacheService: CacheService,
private noteReadService: NoteReadService,
private authenticateService: AuthenticateService,
@ -55,25 +39,65 @@ export class StreamingApiServerService {
}
@bindThis
public attachStreamingApi(server: http.Server) {
// Init websocket server
const ws = new websocket.server({
httpServer: server,
public attach(server: http.Server): void {
this.#wss = new WebSocket.WebSocketServer({
noServer: true,
});
ws.on('request', async (request) => {
const q = request.resourceURL.query as ParsedUrlQuery;
// TODO: トークンが間違ってるなどしてauthenticateに失敗したら
// コネクション切断するなりエラーメッセージ返すなりする
// (現状はエラーがキャッチされておらずサーバーのログに流れて邪魔なので)
const [user, miapp] = await this.authenticateService.authenticate(q.i as string);
if (user?.isSuspended) {
request.reject(400);
server.on('upgrade', async (request, socket, head) => {
if (request.url == null) {
socket.write('HTTP/1.1 400 Bad Request\r\n\r\n');
socket.destroy();
return;
}
const q = new URL(request.url, `http://${request.headers.host}`).searchParams;
let user: LocalUser | null = null;
let app: AccessToken | null = null;
try {
[user, app] = await this.authenticateService.authenticate(q.get('i'));
} catch (e) {
if (e instanceof AuthenticationError) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
} else {
socket.write('HTTP/1.1 500 Internal Server Error\r\n\r\n');
}
socket.destroy();
return;
}
if (user?.isSuspended) {
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
socket.destroy();
return;
}
const stream = new MainStreamConnection(
this.channelsService,
this.noteReadService,
this.notificationService,
this.cacheService,
user, app,
);
await stream.init();
this.#wss.handleUpgrade(request, socket, head, (ws) => {
this.#wss.emit('connection', ws, request, {
stream, user, app,
});
});
});
this.#wss.on('connection', async (connection: WebSocket.WebSocket, request: http.IncomingMessage, ctx: {
stream: MainStreamConnection,
user: LocalUser | null;
app: AccessToken | null
}) => {
const { stream, user, app } = ctx;
const ev = new EventEmitter();
async function onRedisMessage(_: string, data: string): Promise<void> {
@ -83,19 +107,7 @@ export class StreamingApiServerService {
this.redisForSub.on('message', onRedisMessage);
const main = new MainStreamConnection(
this.channelsService,
this.noteReadService,
this.notificationService,
this.cacheService,
ev, user, miapp,
);
await main.init();
const connection = request.accept();
main.init2(connection);
await stream.listen(ev, connection);
const intervalId = user ? setInterval(() => {
this.usersRepository.update(user.id, {
@ -110,16 +122,23 @@ export class StreamingApiServerService {
connection.once('close', () => {
ev.removeAllListeners();
main.dispose();
stream.dispose();
this.redisForSub.off('message', onRedisMessage);
if (intervalId) clearInterval(intervalId);
});
connection.on('message', async (data) => {
if (data.type === 'utf8' && data.utf8Data === 'ping') {
if (data.toString() === 'ping') {
connection.send('pong');
}
});
});
}
@bindThis
public detach(): Promise<void> {
return new Promise((resolve) => {
this.#wss.close(() => resolve());
});
}
}

View file

@ -1,3 +1,4 @@
import * as WebSocket from 'ws';
import type { User } from '@/models/entities/User.js';
import type { AccessToken } from '@/models/entities/AccessToken.js';
import type { Packed } from '@/misc/json-schema.js';
@ -7,7 +8,6 @@ import { bindThis } from '@/decorators.js';
import { CacheService } from '@/core/CacheService.js';
import { UserProfile } from '@/models/index.js';
import type { ChannelsService } from './ChannelsService.js';
import type * as websocket from 'websocket';
import type { EventEmitter } from 'events';
import type Channel from './channel.js';
import type { StreamEventEmitter, StreamMessages } from './types.js';
@ -18,7 +18,7 @@ import type { StreamEventEmitter, StreamMessages } from './types.js';
export default class Connection {
public user?: User;
public token?: AccessToken;
private wsConnection: websocket.connection;
private wsConnection: WebSocket.WebSocket;
public subscriber: StreamEventEmitter;
private channels: Channel[] = [];
private subscribingNotes: any = {};
@ -37,11 +37,9 @@ export default class Connection {
private notificationService: NotificationService,
private cacheService: CacheService,
subscriber: EventEmitter,
user: User | null | undefined,
token: AccessToken | null | undefined,
) {
this.subscriber = subscriber;
if (user) this.user = user;
if (token) this.token = token;
}
@ -70,12 +68,16 @@ export default class Connection {
if (this.user != null) {
await this.fetch();
this.fetchIntervalId = setInterval(this.fetch, 1000 * 10);
if (!this.fetchIntervalId) {
this.fetchIntervalId = setInterval(this.fetch, 1000 * 10);
}
}
}
@bindThis
public async init2(wsConnection: websocket.connection) {
public async listen(subscriber: EventEmitter, wsConnection: WebSocket.WebSocket) {
this.subscriber = subscriber;
this.wsConnection = wsConnection;
this.wsConnection.on('message', this.onWsConnectionMessage);
@ -88,14 +90,11 @@ export default class Connection {
*
*/
@bindThis
private async onWsConnectionMessage(data: websocket.Message) {
if (data.type !== 'utf8') return;
if (data.utf8Data == null) return;
private async onWsConnectionMessage(data: WebSocket.RawData) {
let obj: Record<string, any>;
try {
obj = JSON.parse(data.utf8Data);
obj = JSON.parse(data.toString());
} catch (e) {
return;
}