Skip to content

Commit 6da61c4

Browse files
authored
fix uncaught promise and add opt to disable timeout (#16)
1 parent 26a1d80 commit 6da61c4

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/socket.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export type FluentSocketOptions = {
6868
* The socket timeout to set. After timing out, the socket will be idle and reconnect once the client wants to write something.
6969
*
7070
* Defaults to 3000 (3 seconds)
71+
*
72+
* Set to -1 for no timeout
7173
*/
7274
timeout?: number;
7375
/**
@@ -387,7 +389,8 @@ export class FluentSocket extends EventEmitter {
387389

388390
this.reconnectTimeoutId = setTimeout(() => {
389391
this.reconnectTimeoutId = null;
390-
this.connect();
392+
// Ignore errors if there are any
393+
this.connect().catch(() => {});
391394
}, reconnectInterval);
392395
}
393396

@@ -404,7 +407,11 @@ export class FluentSocket extends EventEmitter {
404407
* @returns A new socket to use for the connection
405408
*/
406409
private createTcpSocket(): net.Socket {
407-
return net.createConnection({...this.socketParams, timeout: this.timeout});
410+
let opts: net.NetConnectOpts = this.socketParams;
411+
if (this.timeout >= 0) {
412+
opts = {...opts, timeout: this.timeout};
413+
}
414+
return net.createConnection(opts);
408415
}
409416

410417
/**
@@ -703,7 +710,8 @@ export class FluentSocket extends EventEmitter {
703710
) {
704711
// Resume from idle state
705712
if (this.state === SocketState.IDLE) {
706-
this.connect();
713+
// Ignore errors if there are any
714+
this.connect().catch(() => {});
707715
}
708716
return false;
709717
}

0 commit comments

Comments
 (0)