Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pipeToRouter(): Use static map of PipeTransport pairs #697

Merged
merged 8 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions node/lib/Router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export declare type PipeToRouterResult = {
};
export declare class Router extends EnhancedEventEmitter {
#private;
private static readonly mapRouterPairPipeTransportPair;
private static readonly pipeToRouterQueue;
private static getPipeTransportPairKey;
/**
* @private
* @emits workerclose
Expand Down
2 changes: 1 addition & 1 deletion node/lib/Router.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 26 additions & 22 deletions node/lib/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
}
return privateMap.get(receiver);
};
var _internal, _data, _channel, _payloadChannel, _closed, _appData, _transports, _producers, _rtpObservers, _dataProducers, _mapRouterPipeTransports, _pipeToRouterQueue, _observer;
var _internal, _data, _channel, _payloadChannel, _closed, _appData, _transports, _producers, _rtpObservers, _dataProducers, _observer;
Object.defineProperty(exports, "__esModule", { value: true });
const uuid_1 = require("uuid");
const awaitqueue_1 = require("awaitqueue");
Expand Down Expand Up @@ -55,10 +55,6 @@ class Router extends EnhancedEventEmitter_1.EnhancedEventEmitter {
_rtpObservers.set(this, new Map());
// DataProducers map.
_dataProducers.set(this, new Map());
// Router to PipeTransport map.
_mapRouterPipeTransports.set(this, new Map());
// AwaitQueue instance to make pipeToRouter tasks happen sequentially.
_pipeToRouterQueue.set(this, new awaitqueue_1.AwaitQueue({ ClosedErrorClass: errors_1.InvalidStateError }));
// Observer instance.
_observer.set(this, new EnhancedEventEmitter_1.EnhancedEventEmitter());
logger.debug('constructor()');
Expand All @@ -68,6 +64,11 @@ class Router extends EnhancedEventEmitter_1.EnhancedEventEmitter {
__classPrivateFieldSet(this, _payloadChannel, payloadChannel);
__classPrivateFieldSet(this, _appData, appData);
}
static getPipeTransportPairKey(router1, router2) {
return router1.id < router2.id
? `${router1.id}_${router2.id}`
: `${router2.id}_${router1.id}`;
}
/**
* Router id.
*/
Expand Down Expand Up @@ -132,10 +133,6 @@ class Router extends EnhancedEventEmitter_1.EnhancedEventEmitter {
__classPrivateFieldGet(this, _rtpObservers).clear();
// Clear the DataProducers map.
__classPrivateFieldGet(this, _dataProducers).clear();
// Clear map of Router/PipeTransports.
__classPrivateFieldGet(this, _mapRouterPipeTransports).clear();
// Close the pipeToRouter AwaitQueue instance.
__classPrivateFieldGet(this, _pipeToRouterQueue).close();
this.emit('@close');
// Emit observer event.
__classPrivateFieldGet(this, _observer).safeEmit('close');
Expand Down Expand Up @@ -164,8 +161,6 @@ class Router extends EnhancedEventEmitter_1.EnhancedEventEmitter {
__classPrivateFieldGet(this, _rtpObservers).clear();
// Clear the DataProducers map.
__classPrivateFieldGet(this, _dataProducers).clear();
// Clear map of Router/PipeTransports.
__classPrivateFieldGet(this, _mapRouterPipeTransports).clear();
this.safeEmit('workerclose');
// Emit observer event.
__classPrivateFieldGet(this, _observer).safeEmit('close');
Expand Down Expand Up @@ -417,20 +412,21 @@ class Router extends EnhancedEventEmitter_1.EnhancedEventEmitter {
// use an async queue.
let localPipeTransport;
let remotePipeTransport;
await __classPrivateFieldGet(this, _pipeToRouterQueue).push(async () => {
let pipeTransportPair = __classPrivateFieldGet(this, _mapRouterPipeTransports).get(router);
await Router.pipeToRouterQueue.push(async () => {
const key = Router.getPipeTransportPairKey(this, router);
const pipeTransportPair = Router.mapRouterPairPipeTransportPair.get(key);
if (pipeTransportPair) {
localPipeTransport = pipeTransportPair[0];
remotePipeTransport = pipeTransportPair[1];
localPipeTransport = pipeTransportPair[this.id];
remotePipeTransport = pipeTransportPair[router.id];
}
else {
try {
pipeTransportPair = await Promise.all([
const pipeTransports = await Promise.all([
this.createPipeTransport({ listenIp, enableSctp, numSctpStreams, enableRtx, enableSrtp }),
router.createPipeTransport({ listenIp, enableSctp, numSctpStreams, enableRtx, enableSrtp })
]);
localPipeTransport = pipeTransportPair[0];
remotePipeTransport = pipeTransportPair[1];
localPipeTransport = pipeTransports[0];
remotePipeTransport = pipeTransports[1];
await Promise.all([
localPipeTransport.connect({
ip: remotePipeTransport.tuple.localIp,
Expand All @@ -445,13 +441,16 @@ class Router extends EnhancedEventEmitter_1.EnhancedEventEmitter {
]);
localPipeTransport.observer.on('close', () => {
remotePipeTransport.close();
__classPrivateFieldGet(this, _mapRouterPipeTransports).delete(router);
Router.mapRouterPairPipeTransportPair.delete(key);
});
remotePipeTransport.observer.on('close', () => {
localPipeTransport.close();
__classPrivateFieldGet(this, _mapRouterPipeTransports).delete(router);
Router.mapRouterPairPipeTransportPair.delete(key);
});
Router.mapRouterPairPipeTransportPair.set(key, {
[this.id]: localPipeTransport,
[router.id]: remotePipeTransport
});
__classPrivateFieldGet(this, _mapRouterPipeTransports).set(router, [localPipeTransport, remotePipeTransport]);
}
catch (error) {
logger.error('pipeToRouter() | error creating PipeTransport pair:%o', error);
Expand Down Expand Up @@ -610,4 +609,9 @@ class Router extends EnhancedEventEmitter_1.EnhancedEventEmitter {
}
}
exports.Router = Router;
_internal = new WeakMap(), _data = new WeakMap(), _channel = new WeakMap(), _payloadChannel = new WeakMap(), _closed = new WeakMap(), _appData = new WeakMap(), _transports = new WeakMap(), _producers = new WeakMap(), _rtpObservers = new WeakMap(), _dataProducers = new WeakMap(), _mapRouterPipeTransports = new WeakMap(), _pipeToRouterQueue = new WeakMap(), _observer = new WeakMap();
_internal = new WeakMap(), _data = new WeakMap(), _channel = new WeakMap(), _payloadChannel = new WeakMap(), _closed = new WeakMap(), _appData = new WeakMap(), _transports = new WeakMap(), _producers = new WeakMap(), _rtpObservers = new WeakMap(), _dataProducers = new WeakMap(), _observer = new WeakMap();
// Map of PipeTransport pairs indexed by a string computed with the ids of
// both involved Routers.
Router.mapRouterPairPipeTransportPair = new Map();
// AwaitQueue instance to make pipeToRouter tasks happen sequentially.
Router.pipeToRouterQueue = new awaitqueue_1.AwaitQueue({ ClosedErrorClass: errors_1.InvalidStateError });
65 changes: 38 additions & 27 deletions node/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,22 @@ export type PipeToRouterResult =

const logger = new Logger('Router');

type PipeTransportPair =
{
[key: string]: PipeTransport;
};

export class Router extends EnhancedEventEmitter
{
// Map of PipeTransport pairs indexed by a string computed with the ids of
// both involved Routers.
private static readonly mapRouterPairPipeTransportPair: Map<string, PipeTransportPair> =
new Map();

// AwaitQueue instance to make pipeToRouter tasks happen sequentially.
private static readonly pipeToRouterQueue =
new AwaitQueue({ ClosedErrorClass: InvalidStateError });

// Internal data.
readonly #internal:
{
Expand Down Expand Up @@ -140,16 +154,16 @@ export class Router extends EnhancedEventEmitter
// DataProducers map.
readonly #dataProducers: Map<string, DataProducer> = new Map();

// Router to PipeTransport map.
readonly #mapRouterPipeTransports: Map<Router, PipeTransport[]> = new Map();

// AwaitQueue instance to make pipeToRouter tasks happen sequentially.
readonly #pipeToRouterQueue =
new AwaitQueue({ ClosedErrorClass: InvalidStateError });

// Observer instance.
readonly #observer = new EnhancedEventEmitter();

private static getPipeTransportPairKey(router1: Router, router2: Router): string
{
return router1.id < router2.id
? `${router1.id}_${router2.id}`
: `${router2.id}_${router1.id}`;
}

/**
* @private
* @emits workerclose
Expand Down Expand Up @@ -270,12 +284,6 @@ export class Router extends EnhancedEventEmitter
// Clear the DataProducers map.
this.#dataProducers.clear();

// Clear map of Router/PipeTransports.
this.#mapRouterPipeTransports.clear();

// Close the pipeToRouter AwaitQueue instance.
this.#pipeToRouterQueue.close();

this.emit('@close');

// Emit observer event.
Expand Down Expand Up @@ -316,9 +324,6 @@ export class Router extends EnhancedEventEmitter
// Clear the DataProducers map.
this.#dataProducers.clear();

// Clear map of Router/PipeTransports.
this.#mapRouterPipeTransports.clear();

this.safeEmit('workerclose');

// Emit observer event.
Expand Down Expand Up @@ -737,20 +742,21 @@ export class Router extends EnhancedEventEmitter
let localPipeTransport: PipeTransport | undefined;
let remotePipeTransport: PipeTransport | undefined;

await this.#pipeToRouterQueue.push(async () =>
await Router.pipeToRouterQueue.push(async () =>
{
let pipeTransportPair = this.#mapRouterPipeTransports.get(router);
const key = Router.getPipeTransportPairKey(this, router);
const pipeTransportPair = Router.mapRouterPairPipeTransportPair.get(key);

if (pipeTransportPair)
{
localPipeTransport = pipeTransportPair[0];
remotePipeTransport = pipeTransportPair[1];
localPipeTransport = pipeTransportPair[this.id];
remotePipeTransport = pipeTransportPair[router.id];
}
else
{
try
{
pipeTransportPair = await Promise.all(
const pipeTransports = await Promise.all(
[
this.createPipeTransport(
{ listenIp, enableSctp, numSctpStreams, enableRtx, enableSrtp }),
Expand All @@ -759,8 +765,8 @@ export class Router extends EnhancedEventEmitter
{ listenIp, enableSctp, numSctpStreams, enableRtx, enableSrtp })
]);

localPipeTransport = pipeTransportPair[0];
remotePipeTransport = pipeTransportPair[1];
localPipeTransport = pipeTransports[0];
remotePipeTransport = pipeTransports[1];

await Promise.all(
[
Expand All @@ -782,17 +788,22 @@ export class Router extends EnhancedEventEmitter
localPipeTransport.observer.on('close', () =>
{
remotePipeTransport!.close();
this.#mapRouterPipeTransports.delete(router);
Router.mapRouterPairPipeTransportPair.delete(key);
});

remotePipeTransport.observer.on('close', () =>
{
localPipeTransport!.close();
this.#mapRouterPipeTransports.delete(router);
Router.mapRouterPairPipeTransportPair.delete(key);
});

this.#mapRouterPipeTransports.set(
router, [ localPipeTransport, remotePipeTransport ]);
Router.mapRouterPairPipeTransportPair.set(
key,
{
[this.id] : localPipeTransport,
[router.id] : remotePipeTransport
}
);
}
catch (error)
{
Expand Down
Loading