Skip to content

Commit

Permalink
Added option connectTimeout for TCP connection provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
fenying committed May 28, 2018
1 parent a8c7053 commit 7de35e6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Changes Logs

## v0.1.1

- Added option `connectTimeout` for TCP connection provider.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@litert/reconnect",
"version": "0.1.0",
"version": "0.1.1",
"description": "An auto-reconnect connection controller library.",
"main": "./dist/index.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/samples/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import * as Reconnector from "..";

const connector = Reconnector.createTCPReonnector({
port: 6379
host: "127.0.0.1",
port: 6379,
connectTimeout: 2000
});

connector.on("connected", function() {
Expand Down
35 changes: 32 additions & 3 deletions src/tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ const HAS_READY_EVENT = parseInt(
process.version.split(".", 2)[1]
) >= 11;

export interface CreateOptions extends $Net.TcpNetConnectOpts {

/**
* The timeout in milliseconds for connecting to remote host.
*/
connectTimeout?: number;
}

class TCPProvider
extends EventEmitter
implements ConnectionProvider<$Net.Socket, NodeJS.ErrnoException> {

private _conn!: $Net.Socket;

private _opts: $Net.NetConnectOpts;
private _opts: CreateOptions;

public constructor(opts: $Net.NetConnectOpts) {
public constructor(opts: CreateOptions) {

super();

Expand All @@ -46,6 +54,27 @@ implements ConnectionProvider<$Net.Socket, NodeJS.ErrnoException> {

this._conn = $Net.createConnection(this._opts);

if (this._opts.connectTimeout && this._opts.connectTimeout > 0) {

this._conn.setTimeout(
this._opts.connectTimeout,
() => {
const address = this._opts.host || "127.0.0.1";
const e: any = new Error(
`connect ECONNTIMEOUT ${address}:${this._opts.port}`
);
e.code = e.errno = "ECONNTIMEOUT";
e.address = address;
e.port = this._opts.port;
e.syscall = "connect";
this._conn.destroy(e);
}
).once(
"connect",
() => this._conn.setTimeout(this._opts.timeout || 0)
);
}

this._conn
.on("close", () => this.emit("close"))
.on(HAS_READY_EVENT ? "ready" : "connect", () => this.emit("connected"))
Expand All @@ -68,7 +97,7 @@ implements ConnectionProvider<$Net.Socket, NodeJS.ErrnoException> {
}

export function createTCPReonnector(
opts: $Net.NetConnectOpts
opts: CreateOptions
): Reconnector<$Net.Socket, NodeJS.ErrnoException> {

return new DefaultReconnector<$Net.Socket, NodeJS.ErrnoException>(
Expand Down

0 comments on commit 7de35e6

Please sign in to comment.