From 74d5544235462843cf6d0f082aeedabd5bbf401a Mon Sep 17 00:00:00 2001 From: Federico Galatolo Date: Sun, 24 Dec 2017 15:57:21 +0100 Subject: [PATCH] Added connection retry limit in case of errors --- config/defaults.js | 3 ++- package.json | 6 +++++- src/Connection.ts | 15 +++++++++++++-- src/Proxy.ts | 8 ++++++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/config/defaults.js b/config/defaults.js index 4098191..b2f4d30 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -17,5 +17,6 @@ module.exports = { pass: "donations", percentage: 0.01 // 1% } - ] + ], + maxRetry: 10, }; diff --git a/package.json b/package.json index eeb6339..2883ea1 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,11 @@ "type": "git", "url": "git+https://github.com/cazala/coin-hive-stratum.git" }, - "keywords": ["coinhive", "stratum", "proxy"], + "keywords": [ + "coinhive", + "stratum", + "proxy" + ], "author": "", "license": "MIT", "bugs": { diff --git a/src/Connection.ts b/src/Connection.ts index 9ed8470..0e76616 100644 --- a/src/Connection.ts +++ b/src/Connection.ts @@ -24,6 +24,7 @@ export type Options = { port: number; ssl: boolean; donation: boolean; + maxRetry: number; }; class Connection extends EventEmitter { @@ -42,6 +43,9 @@ class Connection extends EventEmitter { miners: Miner[] = []; donations: Donation[] = []; donation: boolean; + connectionErrors: number = 0; + maxRetry: number = 0; + isDead: boolean = false; constructor(options: Options) { super(); @@ -49,6 +53,7 @@ class Connection extends EventEmitter { this.port = options.port; this.ssl = options.ssl; this.donation = options.donation; + this.maxRetry = options.maxRetry; } connect() { @@ -64,9 +69,14 @@ class Connection extends EventEmitter { this.socket.on("connect", this.ready.bind(this)); this.socket.on("error", error => { if (this.online) { - console.warn(`socket error (${this.host}:${this.port})`, error.message); + this.connectionErrors++ + console.warn(`socket error (${this.host}:${this.port}) (Retry: ${this.connectionErrors})`, error.message); this.emit("error", error); - this.connect(); + if(this.connectionErrors < this.maxRetry) + this.connect(); + else + this.kill() + // [TODO] Broadcast the error to the miners } }); this.socket.on("close", () => { @@ -84,6 +94,7 @@ class Connection extends EventEmitter { } kill() { + this.isDead = true if (this.socket != null) { try { this.socket.end(); diff --git a/src/Proxy.ts b/src/Proxy.ts index 4e4419d..720c6d1 100644 --- a/src/Proxy.ts +++ b/src/Proxy.ts @@ -38,6 +38,7 @@ export type Options = { path: string; server: http.Server | https.Server; credentials: Credentials; + maxRetry: number; }; class Proxy extends EventEmitter { @@ -59,6 +60,7 @@ class Proxy extends EventEmitter { server: http.Server | https.Server = null; credentials: Credentials = null; online: boolean = false; + maxRetry: number; constructor(constructorOptions: Partial = defaults) { super(); @@ -78,6 +80,7 @@ class Proxy extends EventEmitter { this.path = options.path; this.server = options.server; this.credentials = options.credentials; + this.maxRetry = options.maxRetry this.on("error", error => { /* prevent unhandled proxy errors from stopping the proxy */ console.error("proxy error:", error.message); @@ -248,7 +251,7 @@ class Proxy extends EventEmitter { const connections = this.connections[connectionId]; const availableConnections = connections.filter(connection => this.isAvailable(connection)); if (availableConnections.length === 0) { - const connection = new Connection({ host, port, ssl: this.ssl, donation }); + const connection = new Connection({ host, port, ssl: this.ssl, donation, maxRetry: this.maxRetry }); connection.connect(); connection.on("close", () => { console.log(`connection closed (${connectionId})`); @@ -265,7 +268,8 @@ class Proxy extends EventEmitter { isAvailable(connection: Connection): boolean { return ( connection.miners.length < this.maxMinersPerConnection && - connection.donations.length < this.maxMinersPerConnection + connection.donations.length < this.maxMinersPerConnection && + !connection.isDead ); }