core/hub: use Map to store relays

This commit is contained in:
Micooz 2017-11-28 09:00:59 +08:00
parent fe0f397f8b
commit 9fd276a565
No known key found for this signature in database
GPG Key ID: B100C9A159B1EBA0

@ -19,7 +19,7 @@ export class Hub {
_udpServer = null;
_relays = [];
_relays = new Map(/* id: <relay> */);
_udpRelays = null; // LRU cache
@ -39,6 +39,7 @@ export class Hub {
// relays
this._udpRelays.reset();
this._relays.forEach((relay) => relay.destroy());
this._relays.clear();
MiddlewareManager.cleanup();
// balancer
if (__IS_CLIENT__) {
@ -219,9 +220,9 @@ export class Hub {
logger.verbose(`[hub] [${context.remoteAddress}:${context.remotePort}] connected`);
const relay = createRelay(__TRANSPORT__, context, proxyRequest);
relay.on('close', () => {
this._relays = this._relays.filter((r) => r.id !== relay.id);
this._relays.delete(relay.id);
});
this._relays.push(relay);
this._relays.set(relay.id, relay);
}
_switchServer() {