Refactor backend initialization
This commit is contained in:
parent
86abebf9bf
commit
50124c8a45
7 changed files with 148 additions and 37 deletions
|
|
@ -1,70 +0,0 @@
|
|||
import logger from 'cytube-common/lib/logger';
|
||||
import ioServer from '../ioserver';
|
||||
import ProxiedSocket from './proxiedsocket';
|
||||
|
||||
export default class FrontendManager {
|
||||
constructor(socketEmitter) {
|
||||
this.socketEmitter = socketEmitter;
|
||||
this.frontendConnections = {};
|
||||
this.frontendProxiedSockets = {};
|
||||
}
|
||||
|
||||
onConnection(socket) {
|
||||
if (this.frontendConnections.hasOwnProperty(socket.endpoint)) {
|
||||
// TODO: do some validation, maybe check if the socket is still connected?
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
this.frontendConnections[socket.endpoint] = socket;
|
||||
socket.on('close', this.onFrontendDisconnect.bind(this, socket));
|
||||
socket.on('SocketConnectEvent', this.onSocketConnect.bind(this, socket));
|
||||
socket.on('SocketFrameEvent', this.onSocketFrame.bind(this, socket));
|
||||
}
|
||||
|
||||
onFrontendDisconnect(socket) {
|
||||
const endpoint = socket.endpoint;
|
||||
if (this.frontendConnections.hasOwnProperty(endpoint)) {
|
||||
if (this.frontendProxiedSockets.hasOwnProperty(endpoint)) {
|
||||
logger.warn(`Frontend ${endpoint} disconnected`);
|
||||
for (const key in this.frontendProxiedSockets[endpoint]) {
|
||||
const proxySocket = this.frontendProxiedSockets[endpoint][key];
|
||||
proxySocket.onProxiedEventReceived('disconnect');
|
||||
}
|
||||
delete this.frontendProxiedSockets[endpoint];
|
||||
}
|
||||
delete this.frontendConnections[endpoint];
|
||||
}
|
||||
}
|
||||
|
||||
onSocketConnect(frontendConnection, socketID, socketIP, socketUser) {
|
||||
const mapKey = frontendConnection.endpoint;
|
||||
const proxiedSocket = new ProxiedSocket(
|
||||
socketID,
|
||||
socketIP,
|
||||
socketUser,
|
||||
this.socketEmitter,
|
||||
frontendConnection);
|
||||
|
||||
if (!this.frontendProxiedSockets.hasOwnProperty(mapKey)) {
|
||||
this.frontendProxiedSockets[mapKey] = {};
|
||||
} else if (this.frontendProxiedSockets[mapKey].hasOwnProperty(socketID)) {
|
||||
// TODO: Handle this gracefully
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
this.frontendProxiedSockets[mapKey][socketID] = proxiedSocket;
|
||||
ioServer.handleConnection(proxiedSocket);
|
||||
}
|
||||
|
||||
onSocketFrame(frontendConnection, socketID, event, args) {
|
||||
const mapKey = frontendConnection.endpoint;
|
||||
const socketMap = this.frontendProxiedSockets[mapKey];
|
||||
if (!socketMap || !socketMap.hasOwnProperty(socketID)) {
|
||||
// TODO
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
const socket = socketMap[socketID];
|
||||
socket.onProxiedEventReceived.apply(socket, [event].concat(args));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
import Server from 'cytube-common/lib/proxy/server';
|
||||
import FrontendManager from './frontendmanager';
|
||||
import uuid from 'uuid';
|
||||
import PoolEntryUpdater from 'cytube-common/lib/redis/poolentryupdater';
|
||||
import JSONProtocol from 'cytube-common/lib/proxy/protocol';
|
||||
import { formatProxyAddress } from 'cytube-common/lib/util/addressutil';
|
||||
|
||||
const BACKEND_POOL = 'backend-hosts';
|
||||
|
||||
export default class IOBackend {
|
||||
constructor(proxyListenerConfig, socketEmitter, poolRedisClient) {
|
||||
this.proxyListenerConfig = proxyListenerConfig;
|
||||
this.socketEmitter = socketEmitter;
|
||||
this.poolRedisClient = poolRedisClient;
|
||||
this.protocol = new JSONProtocol();
|
||||
this.initFrontendManager();
|
||||
this.initProxyListener();
|
||||
this.initBackendPoolUpdater();
|
||||
}
|
||||
|
||||
initFrontendManager() {
|
||||
this.frontendManager = new FrontendManager(this.socketEmitter);
|
||||
}
|
||||
|
||||
initProxyListener() {
|
||||
this.proxyListener = new Server(this.proxyListenerConfig, this.protocol);
|
||||
this.proxyListener.on('connection',
|
||||
this.frontendManager.onConnection.bind(this.frontendManager));
|
||||
}
|
||||
|
||||
initBackendPoolUpdater() {
|
||||
const hostname = this.proxyListenerConfig.getHost();
|
||||
const port = this.proxyListenerConfig.getPort();
|
||||
const entry = {
|
||||
address: formatProxyAddress(hostname, port)
|
||||
}
|
||||
this.poolEntryUpdater = new PoolEntryUpdater(
|
||||
this.poolRedisClient,
|
||||
BACKEND_POOL,
|
||||
uuid.v4(),
|
||||
entry
|
||||
);
|
||||
this.poolEntryUpdater.start();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
import { EventEmitter } from 'events';
|
||||
|
||||
export default class ProxiedSocket extends EventEmitter {
|
||||
constructor(socketID, socketIP, socketUser, socketEmitter, frontendConnection) {
|
||||
super();
|
||||
this.id = socketID;
|
||||
this.ip = socketIP;
|
||||
this._realip = socketIP;
|
||||
if (socketUser) {
|
||||
this.user = {
|
||||
name: socketUser.name,
|
||||
global_rank: socketUser.globalRank
|
||||
};
|
||||
}
|
||||
this.socketEmitter = socketEmitter;
|
||||
this.frontendConnection = frontendConnection;
|
||||
}
|
||||
|
||||
emit() {
|
||||
const target = this.socketEmitter.to(this.id);
|
||||
target.emit.apply(target, arguments);
|
||||
}
|
||||
|
||||
onProxiedEventReceived() {
|
||||
EventEmitter.prototype.emit.apply(this, arguments);
|
||||
}
|
||||
|
||||
join(channel) {
|
||||
this.frontendConnection.write(
|
||||
this.frontendConnection.protocol.newSocketJoinRoomsEvent(
|
||||
this.id, [channel]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.frontendConnection.write(
|
||||
this.frontendConnection.protocol.newSocketKickEvent(this.id)
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue