Skip to content

Commit

Permalink
fix: general fixes for connecting to seed node
Browse files Browse the repository at this point in the history
Fixes #489
  • Loading branch information
tegefaulkes committed Oct 26, 2022
1 parent 0421f73 commit 4d86839
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ const config = {
clientPort: 0 as Port,
},
proxyConfig: {
connConnectTime: 20000,
connConnectTime: 2000,
connKeepAliveTimeoutTime: 20000,
connEndTime: 1000,
connPunchIntervalTime: 1000,
connKeepAliveIntervalTime: 1000,
},
nodeConnectionManagerConfig: {
connConnectTime: 20000,
connConnectTime: 2000,
connTimeoutTime: 60000,
initialClosestNodes: 3,
},
Expand Down
24 changes: 16 additions & 8 deletions src/network/ConnectionForward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class ConnectionForward extends Connection {
}

public async start({ ctx }: { ctx: ContextTimed }): Promise<void> {
if (ctx.timer.getTimeout() >= 20000) {
throw new networkErrors.ErrorConnectionStartTimeoutMax();
}
this.logger.info('Starting Connection Forward');
// Promise for ready
const { p: readyP, resolveP: resolveReadyP } = promise<void>();
Expand All @@ -122,7 +125,15 @@ class ConnectionForward extends Connection {
// Promise for abortion and timeout
const { p: abortedP, resolveP: resolveAbortedP } = promise<void>();
if (ctx.signal.aborted) {
resolveAbortedP();
this.logger.info(`Forward was aborted with: ${ctx.signal.reason}`);
// This is for arbitrary abortion reason provided by the caller
// Re-throw the default timeout error as a network timeout error
if (
ctx.signal.reason instanceof contextsErrors.ErrorContextsTimedTimeOut
) {
throw new networkErrors.ErrorConnectionStartTimeout();
}
throw ctx.signal.reason;
} else {
ctx.signal.addEventListener('abort', () => resolveAbortedP());
}
Expand Down Expand Up @@ -159,7 +170,7 @@ class ConnectionForward extends Connection {
await this.send(networkUtils.pingBuffer);
}, this.punchIntervalTime);
await Promise.race([
Promise.all([readyP, secureConnectP]).then(() => {}),
Promise.all([readyP, secureConnectP]),
errorP,
abortedP,
]);
Expand All @@ -171,12 +182,7 @@ class ConnectionForward extends Connection {
this.tlsSocket.destroy();
}
this.utpSocket.off('message', this.handleMessage);
throw new networkErrors.ErrorConnectionStart(e.message, {
data: {
code: e.code,
errno: e.errno,
syscall: e.syscall,
},
throw new networkErrors.ErrorConnectionStart(undefined, {
cause: e,
});
} finally {
Expand All @@ -192,6 +198,8 @@ class ConnectionForward extends Connection {
this.tlsSocket.destroy();
}
this.utpSocket.off('message', this.handleMessage);
// This is for arbitrary abortion reason provided by the caller
// Re-throw the default timeout error as a network timeout error
if (
ctx.signal.reason instanceof contextsErrors.ErrorContextsTimedTimeOut
) {
Expand Down
33 changes: 25 additions & 8 deletions src/network/ConnectionReverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class ConnectionReverse extends Connection {
}

public async start({ ctx }: { ctx: ContextTimed }): Promise<void> {
if (ctx.timer.getTimeout() >= 20000) {
throw new networkErrors.ErrorConnectionStartTimeoutMax();
}
this.logger.info('Starting Connection Reverse');
// Promise for ready
const { p: readyP, resolveP: resolveReadyP } = promise<void>();
Expand All @@ -118,7 +121,19 @@ class ConnectionReverse extends Connection {
const { p: errorP, rejectP: rejectErrorP } = promise<void>();
// Promise for abortion and timeout
const { p: abortedP, resolveP: resolveAbortedP } = promise<void>();
ctx.signal.addEventListener('abort', () => resolveAbortedP());
if (ctx.signal.aborted) {
this.logger.info(`Reverse was aborted with: ${ctx.signal.reason}`);
// This is for arbitrary abortion reason provided by the caller
// Re-throw the default timeout error as a network timeout error
if (
ctx.signal.reason instanceof contextsErrors.ErrorContextsTimedTimeOut
) {
throw new networkErrors.ErrorConnectionStartTimeout();
}
throw ctx.signal.reason;
} else {
ctx.signal.addEventListener('abort', () => resolveAbortedP());
}
this.resolveReadyP = resolveReadyP;
this.utpSocket.on('message', this.handleMessage);
this.serverSocket = net.connect(this.serverPort, this.serverHost, () => {
Expand Down Expand Up @@ -151,12 +166,7 @@ class ConnectionReverse extends Connection {
// Socket isn't established yet, so it is destroyed
this.serverSocket.destroy();
this.utpSocket.off('message', this.handleMessage);
throw new networkErrors.ErrorConnectionStart(e.message, {
data: {
code: e.code,
errno: e.errno,
syscall: e.syscall,
},
throw new networkErrors.ErrorConnectionStart(undefined, {
cause: e,
});
} finally {
Expand All @@ -169,7 +179,14 @@ class ConnectionReverse extends Connection {
// Socket isn't established yet, so it is destroyed
this.serverSocket.destroy();
this.utpSocket.off('message', this.handleMessage);
throw new networkErrors.ErrorConnectionStartTimeout();
// This is for arbitrary abortion reason provided by the caller
// Re-throw the default timeout error as a network timeout error
if (
ctx.signal.reason instanceof contextsErrors.ErrorContextsTimedTimeOut
) {
throw new networkErrors.ErrorConnectionStartTimeout();
}
throw ctx.signal.reason;
}
this.connections.proxy.set(this.address, this);
this.connections.reverse.set(this.proxyAddress, this);
Expand Down
2 changes: 1 addition & 1 deletion src/network/Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Proxy {

constructor({
authToken,
connConnectTime = 20000,
connConnectTime = 2000,
connKeepAliveTimeoutTime = 20000,
connEndTime = 1000,
connPunchIntervalTime = 50,
Expand Down
7 changes: 7 additions & 0 deletions src/network/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class ErrorConnectionStartTimeout<T> extends ErrorConnectionStart<T> {
exitCode = sysexits.NOHOST;
}

class ErrorConnectionStartTimeoutMax<T> extends ErrorConnectionStart<T> {
static description =
'Connection start timeout exceeds max allowable of 20 seconds';
exitCode = sysexits.USAGE;
}

/**
* Used by ConnectionReverse
*/
Expand Down Expand Up @@ -144,6 +150,7 @@ export {
ErrorConnectionEndTimeout,
ErrorConnectionStart,
ErrorConnectionStartTimeout,
ErrorConnectionStartTimeoutMax,
ErrorConnectionCompose,
ErrorConnectionComposeTimeout,
ErrorCertChain,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class NodeConnectionManager {
taskManager,
seedNodes = {},
initialClosestNodes = 3,
connConnectTime = 20000,
connConnectTime = 2000,
connTimeoutTime = 60000,
pingTimeout = 2000,
logger,
Expand Down
7 changes: 0 additions & 7 deletions src/tasks/TaskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,13 +940,6 @@ class TaskManager {
);
await this.gcTask(taskId, tran);
tran.queueSuccess(() => {
// THIS only runs after the transaction is committed
// IS IT POSSIBLE
// that I HAVE REGISTERED EVENT HANDLERS is at there
// cause if so, it would then be able to
// to get an event listener registered
// only afterwards

this.taskEvents.dispatchEvent(
new TaskEvent(taskIdEncoded, {
detail: {
Expand Down

0 comments on commit 4d86839

Please sign in to comment.