Skip to content

Commit 6f9d7d8

Browse files
CMCDragonkaitegefaulkes
authored andcommitted
wip: refactoring NodeManager
[ci skip]
1 parent f6f45f5 commit 6f9d7d8

13 files changed

+1316
-948
lines changed

jest.config.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
const os = require('os');
21
const path = require('path');
3-
const fs = require('fs');
4-
const process = require('process');
52
const { pathsToModuleNameMapper } = require('ts-jest');
63
const { compilerOptions } = require('./tsconfig');
74

@@ -16,10 +13,6 @@ const globals = {
1613
projectDir: __dirname,
1714
// Absolute directory to the test root
1815
testDir: path.join(__dirname, 'tests'),
19-
// Default global data directory
20-
dataDir: fs.mkdtempSync(
21-
path.join(os.tmpdir(), 'polykey-test-global-'),
22-
),
2316
// Default asynchronous test timeout
2417
defaultTimeout: 20000,
2518
failedConnectionTimeout: 50000,
@@ -29,8 +22,8 @@ const globals = {
2922

3023
// The `globalSetup` and `globalTeardown` cannot access the `globals`
3124
// They run in their own process context
32-
// They can receive process environment
33-
process.env['GLOBAL_DATA_DIR'] = globals.dataDir;
25+
// They can however receive the process environment
26+
// Use `process.env` to set variables
3427

3528
module.exports = {
3629
testEnvironment: 'node',

src/config.ts

+41
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,47 @@ const config = {
238238
* Interval for hole punching reverse node connections.
239239
*/
240240
nodesConnectionHolePunchIntervalTime: 1_000, // 1 second
241+
/**
242+
* Interval for refreshing buckets.
243+
*
244+
* A bucket that hasn't had any lookups for this amount of time will be
245+
* refreshed. Lookups can be successful or unsuccessful. A look up will
246+
* generally result in updates to the node graph.
247+
*/
248+
nodesRefreshBucketIntervalTime: 3_600_000, // 1 hour
249+
/**
250+
* Interval time jitter multiplier for refreshing buckets.
251+
*
252+
* For example, if the interval is 60 seconds, and the jitter is configured
253+
* as 0.5 (50%), the actual interval could vary between 30 seconds
254+
* (60 * 0.5) and 90 seconds (60 * 1.5).
255+
*/
256+
nodesRefreshBucketIntervalTimeJitter: 0.5,
257+
/**
258+
* Node graph bucket limit. The node graph keeps record of all node
259+
* addresses of the network.
260+
*
261+
* A smaller limit reduces how many node addresses each node needs to
262+
* keep track of. This can increase stability and fault toelrance
263+
* because it can be kept up to date more quickly, and when nodes
264+
* leave the network, it has a smaller impact on the network. However,
265+
* it may increase the number hops required to find a node.
266+
*
267+
* A larger limit increases how many node addresses each node needs to
268+
* keep track of. This can decrease stability and fault tolerance
269+
* because it can take longer to keep it up to date, and when nodes
270+
* leave the network, it has a larger impact on the network. However,
271+
* it may decrease the number hops required to find a node.
272+
*
273+
* This must be balannced between an efficient number of hops to look up
274+
* a node and resource usage per node and across the network.
275+
*/
276+
nodesGraphBucketLimit: 20,
277+
278+
279+
280+
281+
241282
/**
242283
* Multicast group addresses that the MDNS stack will operate on.
243284
*

src/nodes/NodeConnection.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@ import * as networkUtils from '../network/utils';
3333
import * as nodesUtils from '../nodes/utils';
3434
import { never } from '../utils';
3535
import config from '../config';
36+
import agentClientManifest from './agent/callers';
37+
38+
type AgentClientManifest = typeof agentClientManifest;
3639

3740
/**
3841
* Encapsulates the unidirectional client-side connection of one node to another.
3942
*/
4043
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- False positive for M
41-
interface NodeConnection<M extends ClientManifest> extends CreateDestroy {}
44+
interface NodeConnection extends CreateDestroy {}
4245
@CreateDestroy({
4346
eventDestroy: nodesEvents.EventNodeConnectionDestroy,
4447
eventDestroyed: nodesEvents.EventNodeConnectionDestroyed,
4548
})
46-
class NodeConnection<M extends ClientManifest> {
49+
class NodeConnection {
4750
/**
4851
* Hostname is defined if the target's host was resolved from this hostname
4952
* Undefined if a Host was directly provided
@@ -66,7 +69,7 @@ class NodeConnection<M extends ClientManifest> {
6669
protected logger: Logger;
6770
public readonly quicClient: QUICClient | undefined;
6871
public readonly quicConnection: QUICConnection;
69-
public readonly rpcClient: RPCClient<M>;
72+
public readonly rpcClient: RPCClient<AgentClientManifest>;
7073

7174
/**
7275
* Dispatches a `EventNodeConnectionClose` in response to any `NodeConnection`
@@ -167,7 +170,7 @@ class NodeConnection<M extends ClientManifest> {
167170
}
168171
};
169172

170-
static createNodeConnection<M extends ClientManifest>(
173+
static createNodeConnection(
171174
{
172175
targetNodeIds,
173176
targetHost,
@@ -190,16 +193,16 @@ class NodeConnection<M extends ClientManifest> {
190193
connectionKeepAliveIntervalTime?: number;
191194
connectionKeepAliveTimeoutTime?: number;
192195
quicSocket?: QUICSocket;
193-
manifest: M;
196+
manifest: AgentClientManifest;
194197
logger?: Logger;
195198
},
196199
ctx?: Partial<ContextTimedInput>,
197-
): PromiseCancellable<NodeConnection<M>>;
200+
): PromiseCancellable<NodeConnection>;
198201
@timedCancellable(
199202
true,
200203
config.defaultsSystem.nodesConnectionConnectTimeoutTime,
201204
)
202-
static async createNodeConnection<M extends ClientManifest>(
205+
static async createNodeConnection(
203206
{
204207
targetNodeIds,
205208
targetHost,
@@ -220,14 +223,14 @@ class NodeConnection<M extends ClientManifest> {
220223
targetHostname?: Hostname;
221224
crypto: ClientCryptoOps;
222225
tlsConfig: TLSConfig;
223-
manifest: M;
226+
manifest: AgentClientManifest;
224227
connectionKeepAliveIntervalTime?: number;
225228
connectionKeepAliveTimeoutTime?: number;
226229
quicSocket: QUICSocket;
227230
logger?: Logger;
228231
},
229232
@context ctx: ContextTimed,
230-
): Promise<NodeConnection<M>> {
233+
): Promise<NodeConnection> {
231234
logger.info(`Creating ${this.name}`);
232235
// Checking if attempting to connect to a wildcard IP
233236
if (networkUtils.isHostWildcard(targetHost)) {
@@ -297,7 +300,7 @@ class NodeConnection<M extends ClientManifest> {
297300
quicEvents.EventQUICConnectionStream.name,
298301
throwFunction,
299302
);
300-
const rpcClient = new RPCClient<M>({
303+
const rpcClient = new RPCClient<AgentClientManifest>({
301304
manifest,
302305
middlewareFactory: rpcUtilsMiddleware.defaultClientMiddlewareWrapper(),
303306
streamFactory: async () => {
@@ -320,7 +323,7 @@ class NodeConnection<M extends ClientManifest> {
320323
quicConnection.remoteHost
321324
}:${quicConnection.remotePort}]`,
322325
);
323-
const nodeConnection = new this<M>({
326+
const nodeConnection = new this({
324327
validatedNodeId,
325328
nodeId,
326329
host: targetHost,
@@ -367,7 +370,7 @@ class NodeConnection<M extends ClientManifest> {
367370
return nodeConnection;
368371
}
369372

370-
static createNodeConnectionReverse<M extends ClientManifest>({
373+
static createNodeConnectionReverse({
371374
certChain,
372375
nodeId,
373376
quicConnection,
@@ -377,12 +380,12 @@ class NodeConnection<M extends ClientManifest> {
377380
certChain: Array<Certificate>;
378381
nodeId: NodeId;
379382
quicConnection: QUICConnection;
380-
manifest: M;
383+
manifest: AgentClientManifest;
381384
logger?: Logger;
382-
}): NodeConnection<M> {
385+
}): NodeConnection {
383386
logger.info(`Creating ${this.name}`);
384387
// Creating RPCClient
385-
const rpcClient = new RPCClient<M>({
388+
const rpcClient = new RPCClient<AgentClientManifest>({
386389
manifest,
387390
middlewareFactory: rpcUtilsMiddleware.defaultClientMiddlewareWrapper(),
388391
streamFactory: async (_ctx) => {
@@ -392,7 +395,7 @@ class NodeConnection<M extends ClientManifest> {
392395
logger: logger.getChild(RPCClient.name),
393396
});
394397
// Creating NodeConnection
395-
const nodeConnection = new this<M>({
398+
const nodeConnection = new this({
396399
validatedNodeId: nodeId,
397400
nodeId: nodeId,
398401
localHost: quicConnection.localHost as unknown as Host,
@@ -461,7 +464,7 @@ class NodeConnection<M extends ClientManifest> {
461464
hostname?: Hostname;
462465
quicClient?: QUICClient;
463466
quicConnection: QUICConnection;
464-
rpcClient: RPCClient<M>;
467+
rpcClient: RPCClient<AgentClientManifest>;
465468
logger: Logger;
466469
}) {
467470
this.validatedNodeId = validatedNodeId;
@@ -539,7 +542,7 @@ class NodeConnection<M extends ClientManifest> {
539542
/**
540543
* Gets RPCClient for this node connection
541544
*/
542-
public getClient(): RPCClient<M> {
545+
public getClient(): RPCClient<AgentClientManifest> {
543546
return this.rpcClient;
544547
}
545548
}

0 commit comments

Comments
 (0)