sync/src/backend/iobackend.js

46 lines
1.6 KiB
JavaScript
Raw Normal View History

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';
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';
const BACKEND_POOL = 'backend-hosts';
2015-12-24 16:24:07 -08:00
export default class IOBackend {
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;
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();
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() {
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
}
initBackendPoolUpdater() {
2016-01-23 12:46:04 -08:00
const hostname = this.proxyListenerConfig.getHost();
const port = this.proxyListenerConfig.getPort();
const entry = {
2016-01-23 12:46:04 -08:00
address: formatProxyAddress(hostname, port)
}
this.poolEntryUpdater = new PoolEntryUpdater(
this.poolRedisClient,
BACKEND_POOL,
uuid.v4(),
entry
);
this.poolEntryUpdater.start();
}
2015-12-24 16:24:07 -08:00
}