Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added connection retry limit in case of errors #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module.exports = {
pass: "donations",
percentage: 0.01 // 1%
}
]
],
maxRetry: 10,
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 13 additions & 2 deletions src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Options = {
port: number;
ssl: boolean;
donation: boolean;
maxRetry: number;
};

class Connection extends EventEmitter {
Expand All @@ -42,13 +43,17 @@ class Connection extends EventEmitter {
miners: Miner[] = [];
donations: Donation[] = [];
donation: boolean;
connectionErrors: number = 0;
maxRetry: number = 0;
isDead: boolean = false;

constructor(options: Options) {
super();
this.host = options.host;
this.port = options.port;
this.ssl = options.ssl;
this.donation = options.donation;
this.maxRetry = options.maxRetry;
}

connect() {
Expand All @@ -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", () => {
Expand All @@ -84,6 +94,7 @@ class Connection extends EventEmitter {
}

kill() {
this.isDead = true
if (this.socket != null) {
try {
this.socket.end();
Expand Down
8 changes: 6 additions & 2 deletions src/Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type Options = {
path: string;
server: http.Server | https.Server;
credentials: Credentials;
maxRetry: number;
};

class Proxy extends EventEmitter {
Expand All @@ -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<Options> = defaults) {
super();
Expand All @@ -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);
Expand Down Expand Up @@ -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})`);
Expand All @@ -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
);
}

Expand Down