Skip to content

Commit

Permalink
perf(server): Redis接続をストリーミング接続ごとに作らず、プロセス全体で共有するように
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Mar 23, 2021
1 parent 6b753b0 commit 48ea805
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
6 changes: 5 additions & 1 deletion src/db/redis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as redis from 'redis';
import config from '../config';

export default redis.createClient(
const client = redis.createClient(
config.redis.port,
config.redis.host,
{
Expand All @@ -10,3 +10,7 @@ export default redis.createClient(
db: config.redis.db || 0
}
);

client.subscribe(config.host);

export default client;
33 changes: 8 additions & 25 deletions src/server/api/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as http from 'http';
import * as websocket from 'websocket';
import * as redis from 'redis';

import MainStreamConnection from './stream';
import { ParsedUrlQuery } from 'querystring';
import authenticate from './authenticate';
import { EventEmitter } from 'events';
import config from '../../config';
import redisClient from '../../db/redis';

module.exports = (server: http.Server) => {
// Init websocket server
Expand All @@ -24,37 +23,21 @@ module.exports = (server: http.Server) => {

const connection = request.accept();

let ev: EventEmitter;
const ev = new EventEmitter();

// Connect to Redis
const subscriber = redis.createClient(
config.redis.port,
config.redis.host,
{
password: config.redis.pass
}
);

subscriber.subscribe(config.host);

ev = new EventEmitter();

subscriber.on('message', async (_, data) => {
const obj = JSON.parse(data);
async function onRedisMessage(_: string, data: string) {
const parsed = JSON.parse(data);
ev.emit(parsed.channel, parsed.message);
}

ev.emit(obj.channel, obj.message);
});

connection.once('close', () => {
subscriber.unsubscribe();
subscriber.quit();
});
redisClient.on('message', onRedisMessage);

const main = new MainStreamConnection(connection, ev, user, app);

connection.once('close', () => {
ev.removeAllListeners();
main.dispose();
redisClient.off('message', onRedisMessage);
});

connection.on('message', async (data) => {
Expand Down

8 comments on commit 48ea805

@syuilo
Copy link
Member Author

@syuilo syuilo commented on 48ea805 Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これこうして良いんだっけ?というのが自信ない(コネクション使いまわすというのは普通にやるはずなので、あえてそうしていなかったのってなんか理由あったっけ?みたいな)ので見ていただけると🙏
cc: Collaborators (@mei23 @rinsuki @acid-chicken あたり?)

@syuilo
Copy link
Member Author

@syuilo syuilo commented on 48ea805 Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テスト失敗したのでなんか問題ありそう

@rinsuki
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subscribeは使い回すと死にそう publishは使い回してもたぶん大丈夫 (適当)

@rinsuki
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

って思ったけどなんか普通に動きそうな気がするな とりあえず re-run してみた

@syuilo
Copy link
Member Author

@syuilo syuilo commented on 48ea805 Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ローカルでERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this contextが出た

@syuilo
Copy link
Member Author

@syuilo syuilo commented on 48ea805 Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ひとつのコネクションで subscribe と publish は同時にできないのか

@syuilo
Copy link
Member Author

@syuilo syuilo commented on 48ea805 Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@syuilo
Copy link
Member Author

@syuilo syuilo commented on 48ea805 Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テストは通った

Please sign in to comment.