Skip to content

Commit

Permalink
Connection timeout (#185)
Browse files Browse the repository at this point in the history
* Client side connection timeout control
  • Loading branch information
rmruano committed Sep 27, 2021
1 parent 4ea9684 commit 2b04ef0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ session.on('debug', function(type, msg, payload) {
});
```

### Handling client connection errors:
In case of errors while trying to connect, an `error` event will be emitted by the session and the program will be terminated if it's not listened. This is how you should check for errors.

``` javascript
session.on('error', function(e) {
// empty callback to catch emitted errors to prevent exit due unhandled errors
if (e.code === "ETIMEOUT") {
// TIMEOUT
} else if (e.code === "ECONNREFUSED" {
// CONNECTION REFUSED
} else {
// OTHER ERROR
}
});
```
### Connection timeout:
By default the socket will be dropped after 30000 ms if it doesn't connect. A `connectTimeout` option can be sent when making connections with the server in order to change this setting.
Encodings
---------
Expand Down
16 changes: 15 additions & 1 deletion lib/smpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function Session(options) {
this.options = options || {};
var self = this;
var transport = net;
var connectTimeout;
this._extractPDUs = this._extractPDUs.bind(self);
this.sequence = 0;
this.paused = false;
Expand All @@ -32,8 +33,18 @@ function Session(options) {
if (options.tls) {
transport = tls;
}
connectTimeout = setTimeout(function() {
if (self.socket) {
var e = new Error("Timeout of " + options.connectTimeout + "ms while connecting to " +
self.options.host + ":" + self.options.port);
e.code = "ETIMEOUT";
e.timeout = options.connectTimeout;
self.socket.destroy(e);
}
}, options.connectTimeout);
this.socket = transport.connect(this.options);
this.socket.on('connect', (function() {
clearTimeout(connectTimeout);
self.remoteAddress = self.socket.remoteAddress;
self.debug("server.connected", "connected to server", {secure: options.tls});
self.emit('connect'); // @todo should emmit the session, but it would break BC
Expand All @@ -60,6 +71,7 @@ function Session(options) {
self._extractPDUs();
});
this.socket.on('close', function() {
clearTimeout(connectTimeout);
if (self._mode === "server") {
self.debug("client.disconnected", "client has disconnected");
} else {
Expand All @@ -72,12 +84,13 @@ function Session(options) {
}
});
this.socket.on('error', function(e) {
clearTimeout(connectTimeout);
self.debug("socket.error", e.message, e);
self.emit('error', e); // Emitted errors will kill the program if they're not captured.
if(self._interval) {
clearInterval(self._interval);
self._interval = 0;
}
self.emit('error', e); // Emitted errors will kill the program if they're not captured.
});
}

Expand Down Expand Up @@ -316,6 +329,7 @@ exports.connect = exports.createSession = function(url, listener) {
}
options.port = options.port || (options.tls ? 3550 : 2775);
options.debug = options.debug || false;
options.connectTimeout = options.connectTimeout || 30000;

var session = new Session(options);
if (listener) {
Expand Down
10 changes: 10 additions & 0 deletions test/smpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,14 @@ describe('Client/Server simulations', function() {
});
});

it('should fail to connect with an invalid host and trigger a ETIMEOUT error', function (done) {
var session = smpp.connect({url: 'smpp://1.1.1.1:2775', connectTimeout: 25}, function () {});
session.on('error', function(e) {
// empty callback to catch emitted errors to prevent exit due unhandled errors
assert.equal(e.code, "ETIMEOUT");
done();
});
});


});

0 comments on commit 2b04ef0

Please sign in to comment.