2015-12-28 23:52:39 -08:00
|
|
|
import Server from 'cytube-common/lib/proxy/server';
|
2016-02-04 21:43:20 -08:00
|
|
|
import ProxyInterceptor from './proxyinterceptor';
|
2016-01-20 23:11:55 -08:00
|
|
|
import uuid from 'uuid';
|
|
|
|
|
import PoolEntryUpdater from 'cytube-common/lib/redis/poolentryupdater';
|
|
|
|
|
import JSONProtocol from 'cytube-common/lib/proxy/protocol';
|
2016-01-23 12:46:04 -08:00
|
|
|
import { formatProxyAddress } from 'cytube-common/lib/util/addressutil';
|
2016-01-20 23:11:55 -08:00
|
|
|
|
|
|
|
|
const BACKEND_POOL = 'backend-hosts';
|
2015-12-24 16:24:07 -08:00
|
|
|
|
|
|
|
|
export default class IOBackend {
|
2016-01-20 23:11:55 -08:00
|
|
|
constructor(proxyListenerConfig, socketEmitter, poolRedisClient) {
|
2015-12-24 16:24:07 -08:00
|
|
|
this.proxyListenerConfig = proxyListenerConfig;
|
2015-12-26 15:07:03 -08:00
|
|
|
this.socketEmitter = socketEmitter;
|
2016-01-20 23:11:55 -08:00
|
|
|
this.poolRedisClient = poolRedisClient;
|
|
|
|
|
this.protocol = new JSONProtocol();
|
2016-02-04 21:43:20 -08:00
|
|
|
this.initProxyInterceptor();
|
2015-12-24 16:24:07 -08:00
|
|
|
this.initProxyListener();
|
2016-01-20 23:11:55 -08:00
|
|
|
this.initBackendPoolUpdater();
|
2015-12-24 16:24:07 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-04 21:43:20 -08:00
|
|
|
initProxyInterceptor() {
|
|
|
|
|
this.proxyInterceptor = new ProxyInterceptor(this.socketEmitter);
|
2015-12-24 16:24:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initProxyListener() {
|
2016-01-20 23:11:55 -08:00
|
|
|
this.proxyListener = new Server(this.proxyListenerConfig, this.protocol);
|
2015-12-24 16:24:07 -08:00
|
|
|
this.proxyListener.on('connection',
|
2016-02-04 21:43:20 -08:00
|
|
|
this.proxyInterceptor.onConnection.bind(this.proxyInterceptor));
|
2015-12-24 16:24:07 -08:00
|
|
|
}
|
2016-01-20 23:11:55 -08:00
|
|
|
|
|
|
|
|
initBackendPoolUpdater() {
|
2016-01-23 12:46:04 -08:00
|
|
|
const hostname = this.proxyListenerConfig.getHost();
|
|
|
|
|
const port = this.proxyListenerConfig.getPort();
|
2016-01-20 23:11:55 -08:00
|
|
|
const entry = {
|
2016-01-23 12:46:04 -08:00
|
|
|
address: formatProxyAddress(hostname, port)
|
2016-01-20 23:11:55 -08:00
|
|
|
}
|
|
|
|
|
this.poolEntryUpdater = new PoolEntryUpdater(
|
|
|
|
|
this.poolRedisClient,
|
|
|
|
|
BACKEND_POOL,
|
|
|
|
|
uuid.v4(),
|
|
|
|
|
entry
|
|
|
|
|
);
|
|
|
|
|
this.poolEntryUpdater.start();
|
|
|
|
|
}
|
2015-12-24 16:24:07 -08:00
|
|
|
}
|