From 09b291f697aa886ff197b17f4070c31676eaeb31 Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Thu, 9 Nov 2023 14:58:27 +1100 Subject: [PATCH] feat: added `nodesConnectionFindLocalTimeoutTime` config option and fixed `nodes add` command from setting nodes with `scopes` fix: correct scope has been added to `nodes add` command [ci-skip] --- src/PolykeyAgent.ts | 4 ++++ src/client/handlers/NodesAdd.ts | 4 ++-- src/config.ts | 16 ++++++++++++++++ src/nodes/NodeConnectionManager.ts | 13 +++++++++++-- tests/client/handlers/nodes.test.ts | 6 +++++- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/PolykeyAgent.ts b/src/PolykeyAgent.ts index 60a93cdf2..d95fbe408 100644 --- a/src/PolykeyAgent.ts +++ b/src/PolykeyAgent.ts @@ -163,6 +163,8 @@ class PolykeyAgent { config.defaultsSystem.nodesConnectionIdleTimeoutTime, connectionFindConcurrencyLimit: config.defaultsSystem.nodesConnectionFindConcurrencyLimit, + connectionFindLocalTimeoutTime: + config.defaultsSystem.nodesConnectionFindLocalTimeoutTime, connectionConnectTimeoutTime: config.defaultsSystem.nodesConnectionConnectTimeoutTime, connectionKeepAliveTimeoutTime: @@ -346,6 +348,8 @@ class PolykeyAgent { seedNodes: optionsDefaulted.seedNodes, connectionFindConcurrencyLimit: optionsDefaulted.nodes.connectionFindConcurrencyLimit, + connectionFindLocalTimeoutTime: + optionsDefaulted.nodes.connectionFindLocalTimeoutTime, connectionIdleTimeoutTime: optionsDefaulted.nodes.connectionIdleTimeoutTime, connectionConnectTimeoutTime: diff --git a/src/client/handlers/NodesAdd.ts b/src/client/handlers/NodesAdd.ts index a384624d2..09fe84bc8 100644 --- a/src/client/handlers/NodesAdd.ts +++ b/src/client/handlers/NodesAdd.ts @@ -6,7 +6,6 @@ import type { } from '../types'; import type { NodeId } from '../../ids'; import type { Host, Hostname, Port } from '../../network/types'; -import type { NodeAddress } from '../../nodes/types'; import type NodeManager from '../../nodes/NodeManager'; import { UnaryHandler } from '@matrixai/rpc'; import * as ids from '../../ids'; @@ -68,7 +67,8 @@ class NodesAdd extends UnaryHandler< { host, port, - } as NodeAddress, + scopes: ['global'], + }, true, input.force ?? false, 1500, diff --git a/src/config.ts b/src/config.ts index 3d9c1152e..401c2021e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -195,6 +195,22 @@ const config = { * the network. */ nodesConnectionFindConcurrencyLimit: 3, + /** + * Timeout for finding local nodes via MDNS. + * + * When `NodeConnectionManager.findNodeLocal` is executed, `js-mdns` will + * start up a query for any Polykey agent services. The timeout determines + * how long the query should sit before it is cancelled. + * + * During this time, if `js-mdns` manages to find a service relating to the + * nodeId that `NodeConnectionManager.findNodeLocal` is looking for, the + * timeout will be disregarded. + * + * The default value should allow some leeway for at least 2 query packets + * to be sent out, and for `js-mdns` to wait some time before receiving + * the corresponding answer packets from devices on the network. + */ + nodesConnectionFindLocalTimeoutTime: 1_500, // 1.5 seconds /** * Timeout for idle node connections. * diff --git a/src/nodes/NodeConnectionManager.ts b/src/nodes/NodeConnectionManager.ts index b01a01629..568ce58e9 100644 --- a/src/nodes/NodeConnectionManager.ts +++ b/src/nodes/NodeConnectionManager.ts @@ -97,6 +97,11 @@ class NodeConnectionManager { */ public readonly connectionFindConcurrencyLimit: number; + /** + * Time used to find a node using `findNodeLocal`. + */ + public readonly connectionFindLocalTimeoutTime: number; + /** * Time to wait to garbage collect un-used node connections. */ @@ -345,6 +350,8 @@ class NodeConnectionManager { seedNodes = {}, connectionFindConcurrencyLimit = config.defaultsSystem .nodesConnectionFindConcurrencyLimit, + connectionFindLocalTimeoutTime = config.defaultsSystem + .nodesConnectionFindLocalTimeoutTime, connectionIdleTimeoutTime = config.defaultsSystem .nodesConnectionIdleTimeoutTime, connectionConnectTimeoutTime = config.defaultsSystem @@ -365,6 +372,7 @@ class NodeConnectionManager { tlsConfig: TLSConfig; seedNodes?: SeedNodes; connectionFindConcurrencyLimit?: number; + connectionFindLocalTimeoutTime?: number; connectionIdleTimeoutTime?: number; connectionConnectTimeoutTime?: number; connectionKeepAliveTimeoutTime?: number; @@ -384,6 +392,7 @@ class NodeConnectionManager { return k !== nodeIdEncodedOwn; }) as SeedNodes; this.connectionFindConcurrencyLimit = connectionFindConcurrencyLimit; + this.connectionFindLocalTimeoutTime = connectionFindLocalTimeoutTime; this.connectionIdleTimeoutTime = connectionIdleTimeoutTime; this.connectionConnectTimeoutTime = connectionConnectTimeoutTime; this.connectionKeepAliveTimeoutTime = connectionKeepAliveTimeoutTime; @@ -1393,7 +1402,7 @@ class NodeConnectionManager { @timedCancellable( true, (nodeConnectionManager: NodeConnectionManager) => - nodeConnectionManager.connectionConnectTimeoutTime, + nodeConnectionManager.connectionFindLocalTimeoutTime, ) public async findNodeLocal( targetNodeId: NodeId, @@ -1504,7 +1513,7 @@ class NodeConnectionManager { const [localAddresses, kademliaAddress] = await Promise.allSettled([ this.findNodeLocal(targetNodeId, { signal: ctx.signal, - timer: pingTimeoutTime ?? this.connectionConnectTimeoutTime, + timer: pingTimeoutTime ?? this.connectionFindLocalTimeoutTime, }), this.findNode(targetNodeId, pingTimeoutTime, ctx), ]); diff --git a/tests/client/handlers/nodes.test.ts b/tests/client/handlers/nodes.test.ts index d91a75374..2d482a5ba 100644 --- a/tests/client/handlers/nodes.test.ts +++ b/tests/client/handlers/nodes.test.ts @@ -183,7 +183,11 @@ describe('nodesAdd', () => { )!, ); expect(result).toBeDefined(); - expect(result!.address).toEqual({ host: '127.0.0.1', port: 11111 }); + expect(result!.address).toEqual({ + host: '127.0.0.1', + port: 11111, + scopes: ['global'], + }); }); test('cannot add invalid node', async () => { // Invalid host