Skip to content

Commit

Permalink
feat: NodeConnectionManager.getConnectionWithAddresses is now separ…
Browse files Browse the repository at this point in the history
…ated into multiple stages so that if connecting with `local` `NodeAddress`s succeeds, `external` `NodeAddress`s will be ignored

fix: `NodeConnectionManager.getConnectionWithAddresses` now attempts connections with `local` scoped addresses before `external` scoped addresses

fix: correct typing on `establishMultiConnection` for `ctx` param

[ci-skip]
  • Loading branch information
amydevs committed Oct 25, 2023
1 parent 66cf31b commit 5c4e258
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,16 +771,44 @@ class NodeConnectionManager {
const existingConnection = await this.getExistingConnection(targetNodeId);
if (existingConnection != null) return existingConnection;
const targetNodeIdEncoded = nodesUtils.encodeNodeId(targetNodeId);
let timeoutDivisions = 0;
const addressGroups: { local: Array<NodeAddress>, external: Array<NodeAddress> } = { local: [], external: [] };
for (const address of addresses) {
let scope = address.scopes.includes('local') ? 'local' : 'external';
// If this is the first time an addressGroup has had an address added, the timeout divisions must be incremented.
if (addressGroups[scope].length === 0) {
timeoutDivisions++;
}
addressGroups[scope].push(address);
}
this.logger.debug(`Getting NodeConnection for ${targetNodeIdEncoded}`);
return await this.connectionLocks
.withF([targetNodeIdString, Lock, ctx], async () => {
this.logger.debug(`acquired lock for ${targetNodeIdEncoded}`);
// Attempting a multi-connection for the target node
const results = await this.establishMultiConnection(
[targetNodeId],
addresses,
ctx,
);
// Attempting a multi-connection for the target node using local addresses
const timeout = ctx.timer.getTimeout() / timeoutDivisions;
let results: Map<NodeIdString, ConnectionAndTimer> | undefined;
if (addressGroups.local.length !== 0) {
results = await this.establishMultiConnection(
[targetNodeId],
addressGroups.local,
{
signal: ctx.signal,
timer: timeout
},
);
}
// If there are no results from the attempted local connections, attempt a multi-connection for the target node using external addresses
if (results == null || results.size === 0) {
results = await this.establishMultiConnection(
[targetNodeId],
addressGroups.external,
{
signal: ctx.signal,
timer: timeout
},
);
}
// Should be a single result.
for (const [, connAndTimer] of results) {
return connAndTimer;
Expand All @@ -801,10 +829,20 @@ class NodeConnectionManager {
* @param ctx
* @protected
*/
protected establishMultiConnection(
nodeIds: Array<NodeId>,
addresses: Array<NodeAddress>,
ctx?: Partial<ContextTimedInput>,
): PromiseCancellable<Map<NodeIdString, ConnectionAndTimer>>;
@timedCancellable(
true,
(nodeConnectionManager: NodeConnectionManager) =>
nodeConnectionManager.connectionConnectTimeoutTime,
)
protected async establishMultiConnection(
nodeIds: Array<NodeId>,
addresses: Array<NodeAddress>,
ctx: ContextTimed,
@context ctx: ContextTimed,
): Promise<Map<NodeIdString, ConnectionAndTimer>> {
const nodesEncoded = nodeIds.map((v) => nodesUtils.encodeNodeId(v));
this.logger.debug(`getting multi-connection for ${nodesEncoded}`);
Expand Down

0 comments on commit 5c4e258

Please sign in to comment.