forked from FoundKeyGang/FoundKey
perf(server): Redis接続をストリーミング接続ごとに作らず、プロセス全体で共有するように
This commit is contained in:
parent
6b753b05d6
commit
48ea805999
2 changed files with 13 additions and 26 deletions
|
@ -1,7 +1,7 @@
|
||||||
import * as redis from 'redis';
|
import * as redis from 'redis';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
|
|
||||||
export default redis.createClient(
|
const client = redis.createClient(
|
||||||
config.redis.port,
|
config.redis.port,
|
||||||
config.redis.host,
|
config.redis.host,
|
||||||
{
|
{
|
||||||
|
@ -10,3 +10,7 @@ export default redis.createClient(
|
||||||
db: config.redis.db || 0
|
db: config.redis.db || 0
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
client.subscribe(config.host);
|
||||||
|
|
||||||
|
export default client;
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import * as websocket from 'websocket';
|
import * as websocket from 'websocket';
|
||||||
import * as redis from 'redis';
|
|
||||||
|
|
||||||
import MainStreamConnection from './stream';
|
import MainStreamConnection from './stream';
|
||||||
import { ParsedUrlQuery } from 'querystring';
|
import { ParsedUrlQuery } from 'querystring';
|
||||||
import authenticate from './authenticate';
|
import authenticate from './authenticate';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import config from '../../config';
|
import redisClient from '../../db/redis';
|
||||||
|
|
||||||
module.exports = (server: http.Server) => {
|
module.exports = (server: http.Server) => {
|
||||||
// Init websocket server
|
// Init websocket server
|
||||||
|
@ -24,37 +23,21 @@ module.exports = (server: http.Server) => {
|
||||||
|
|
||||||
const connection = request.accept();
|
const connection = request.accept();
|
||||||
|
|
||||||
let ev: EventEmitter;
|
const ev = new EventEmitter();
|
||||||
|
|
||||||
// Connect to Redis
|
async function onRedisMessage(_: string, data: string) {
|
||||||
const subscriber = redis.createClient(
|
const parsed = JSON.parse(data);
|
||||||
config.redis.port,
|
ev.emit(parsed.channel, parsed.message);
|
||||||
config.redis.host,
|
}
|
||||||
{
|
|
||||||
password: config.redis.pass
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
subscriber.subscribe(config.host);
|
redisClient.on('message', onRedisMessage);
|
||||||
|
|
||||||
ev = new EventEmitter();
|
|
||||||
|
|
||||||
subscriber.on('message', async (_, data) => {
|
|
||||||
const obj = JSON.parse(data);
|
|
||||||
|
|
||||||
ev.emit(obj.channel, obj.message);
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.once('close', () => {
|
|
||||||
subscriber.unsubscribe();
|
|
||||||
subscriber.quit();
|
|
||||||
});
|
|
||||||
|
|
||||||
const main = new MainStreamConnection(connection, ev, user, app);
|
const main = new MainStreamConnection(connection, ev, user, app);
|
||||||
|
|
||||||
connection.once('close', () => {
|
connection.once('close', () => {
|
||||||
ev.removeAllListeners();
|
ev.removeAllListeners();
|
||||||
main.dispose();
|
main.dispose();
|
||||||
|
redisClient.off('message', onRedisMessage);
|
||||||
});
|
});
|
||||||
|
|
||||||
connection.on('message', async (data) => {
|
connection.on('message', async (data) => {
|
||||||
|
|
Loading…
Reference in a new issue