Skip to content

Commit

Permalink
fix(net): should reconnect if fails during connect
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Nov 16, 2024
1 parent fe9cd4c commit 6cbb55a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
11 changes: 5 additions & 6 deletions net/lib/ConnectionToCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export default class ConnectionToCore<
TCoreApiHandlers extends IApiHandlers,
TEventSpec,
> extends TypedEventEmitter<IConnectionToCoreEvents<TEventSpec>> {
public static readonly MinimumAutoReconnectMillis = 1000;
public static MinimumAutoReconnectMillis = 1000;
public connectAction: IConnectAction;
public disconnectAction: IConnectAction;

public autoReconnect = true;

public hooks: {
Expand All @@ -54,8 +53,8 @@ export default class ConnectionToCore<

protected events = new EventSubscriber();

private didCallConnectionTerminated = false;
private lastDisconnectDate?: Date;
protected didCallConnectionTerminated = false;
protected lastDisconnectDate?: Date;

constructor(public transport: ITransport) {
super();
Expand Down Expand Up @@ -85,16 +84,17 @@ export default class ConnectionToCore<
startTime: Date.now(),
resolvable: new Resolvable(),
};
this.connectAction = connectAction;
this.disconnectAction = null;

try {
this.connectAction = connectAction;
await this.transport.connect?.(timeoutMs);
this.didCallConnectionTerminated = false;
await this.afterConnectHook();
connectAction.resolvable.resolve();
this.emit('connected');
} catch (err) {
delete this.connectAction;
connectAction.resolvable.reject(err, true);
}

Expand Down Expand Up @@ -147,7 +147,6 @@ export default class ConnectionToCore<
): Promise<ICoreResponsePayload<TCoreApiHandlers, T>['data']> {
const activeConnectHook = this.connectAction?.isCallingHook && this.connectAction;
const activeDisconnectHook = this.disconnectAction?.isCallingHook && this.disconnectAction;

// if we are not connected, try to connect (except during a disconnect)
if (this.shouldAutoConnect()) {
await this.connect({ timeoutMs, isAutoConnect: true });
Expand Down
33 changes: 33 additions & 0 deletions net/test/ConnectionToCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,36 @@ test('should be able to reconnect after a disconnect', async () => {
server.unref().close();
}
});

test('should be able to reconnect after a failed connect', async () => {
const server = new Server();
const wss = new WebsocketServer({ server });
apiSpy.mockClear();
try {
server.listen(0);
let counter = 0;
wss.on('connection', (ws, req) => {
if (counter++ < 1) {
req.destroy(new Error('test'));
return;
}
const transport = new WsTransportToClient(ws, req);
new ConnectionToClient(transport, apiSpec);
});
const host = server.address() as AddressInfo;

const wsTransportToCore = new WsTransportToCore(`ws://localhost:${host.port}`);

const connectionToCore = new ConnectionToCore(wsTransportToCore);
needsClosing.push(() => connectionToCore.disconnect());
await expect(connectionToCore.sendRequest({ command: 'api', args: [1] })).rejects.toThrow();
expect(apiSpy).toHaveBeenCalledTimes(0);

ConnectionToCore.MinimumAutoReconnectMillis = 0;
await expect(connectionToCore.sendRequest({ command: 'api', args: [1] })).resolves.toBeTruthy();
expect(apiSpy).toHaveBeenCalledTimes(1);
} finally {
wss.close();
server.unref().close();
}
});

0 comments on commit 6cbb55a

Please sign in to comment.