Skip to content

Commit

Permalink
feat: added nodesConnectionFindLocalTimeoutTime config option and f…
Browse files Browse the repository at this point in the history
…ixed `nodes add` command from setting nodes with `scopes`

fix: correct scope has been added to `nodes add` command

[ci-skip]
  • Loading branch information
amydevs committed Nov 9, 2023
1 parent 8857d60 commit 09b291f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class PolykeyAgent {
config.defaultsSystem.nodesConnectionIdleTimeoutTime,
connectionFindConcurrencyLimit:
config.defaultsSystem.nodesConnectionFindConcurrencyLimit,
connectionFindLocalTimeoutTime:
config.defaultsSystem.nodesConnectionFindLocalTimeoutTime,
connectionConnectTimeoutTime:
config.defaultsSystem.nodesConnectionConnectTimeoutTime,
connectionKeepAliveTimeoutTime:
Expand Down Expand Up @@ -346,6 +348,8 @@ class PolykeyAgent {
seedNodes: optionsDefaulted.seedNodes,
connectionFindConcurrencyLimit:
optionsDefaulted.nodes.connectionFindConcurrencyLimit,
connectionFindLocalTimeoutTime:
optionsDefaulted.nodes.connectionFindLocalTimeoutTime,
connectionIdleTimeoutTime:
optionsDefaulted.nodes.connectionIdleTimeoutTime,
connectionConnectTimeoutTime:
Expand Down
4 changes: 2 additions & 2 deletions src/client/handlers/NodesAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -68,7 +67,8 @@ class NodesAdd extends UnaryHandler<
{
host,
port,
} as NodeAddress,
scopes: ['global'],
},
true,
input.force ?? false,
1500,
Expand Down
16 changes: 16 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 11 additions & 2 deletions src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -345,6 +350,8 @@ class NodeConnectionManager {
seedNodes = {},
connectionFindConcurrencyLimit = config.defaultsSystem
.nodesConnectionFindConcurrencyLimit,
connectionFindLocalTimeoutTime = config.defaultsSystem
.nodesConnectionFindLocalTimeoutTime,
connectionIdleTimeoutTime = config.defaultsSystem
.nodesConnectionIdleTimeoutTime,
connectionConnectTimeoutTime = config.defaultsSystem
Expand All @@ -365,6 +372,7 @@ class NodeConnectionManager {
tlsConfig: TLSConfig;
seedNodes?: SeedNodes;
connectionFindConcurrencyLimit?: number;
connectionFindLocalTimeoutTime?: number;
connectionIdleTimeoutTime?: number;
connectionConnectTimeoutTime?: number;
connectionKeepAliveTimeoutTime?: number;
Expand All @@ -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;
Expand Down Expand Up @@ -1393,7 +1402,7 @@ class NodeConnectionManager {
@timedCancellable(
true,
(nodeConnectionManager: NodeConnectionManager) =>
nodeConnectionManager.connectionConnectTimeoutTime,
nodeConnectionManager.connectionFindLocalTimeoutTime,
)
public async findNodeLocal(
targetNodeId: NodeId,
Expand Down Expand Up @@ -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),
]);
Expand Down
6 changes: 5 additions & 1 deletion tests/client/handlers/nodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 09b291f

Please sign in to comment.