Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Make stale connection detection configurable #505

Merged
merged 3 commits into from
Dec 19, 2016
Merged
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
40 changes: 21 additions & 19 deletions lib/Slackbot_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function(botkit, config) {
// Set when destroy() is called - prevents a reconnect from completing
// if it was fired off prior to destroy being called
var destroyed = false;
var pingIntervalId = null;
var pingTimeoutId = null;
var retryBackoff = null;

// config.retry, can be Infinity too
Expand Down Expand Up @@ -68,8 +68,8 @@ module.exports = function(botkit, config) {
bot.rtm.close();
}

if (pingIntervalId) {
clearInterval(pingIntervalId);
if (pingTimeoutId) {
clearTimeout(pingTimeoutId);
}

botkit.trigger('rtm_close', [bot, err]);
Expand Down Expand Up @@ -166,24 +166,26 @@ module.exports = function(botkit, config) {

bot.rtm.on('pong', function(obj) {
lastPong = Date.now();
botkit.debug('PONG received');
});

bot.rtm.on('open', function() {
botkit.log.notice('RTM websocket opened');

pingIntervalId = setInterval(function() {
if (lastPong && lastPong + 12000 < Date.now()) {
var err = new Error('Stale RTM connection, closing RTM');
botkit.log.error(err);
bot.closeRTM(err);
clearInterval(pingIntervalId);
return;
}
var pinger = function () {
var pongTimeout = bot.botkit.config.stale_connection_timeout || 12000
if (lastPong && lastPong + pongTimeout < Date.now()) {
var err = new Error('Stale RTM connection, closing RTM');
botkit.log.error(err);
bot.closeRTM(err);
clearTimeout(pingTimeoutId);
return;
}

bot.rtm.ping(null, null, true);
pingTimeoutId = setTimeout(pinger, 5000);
};

botkit.debug('PING sent');
bot.rtm.ping(null, null, true);
}, 5000);
pingTimeoutId = setTimeout(pinger, 5000);

botkit.trigger('rtm_open', [bot]);
bot.rtm.on('message', function(data, flags) {
Expand Down Expand Up @@ -212,16 +214,16 @@ module.exports = function(botkit, config) {

bot.rtm.on('error', function(err) {
botkit.log.error('RTM websocket error!', err);
if (pingIntervalId) {
clearInterval(pingIntervalId);
if (pingTimeoutId) {
clearTimeout(pingTimeoutId);
}
botkit.trigger('rtm_close', [bot, err]);
});

bot.rtm.on('close', function(code, message) {
botkit.log.notice('RTM close event: ' + code + ' : ' + message);
if (pingIntervalId) {
clearInterval(pingIntervalId);
if (pingTimeoutId) {
clearTimeout(pingTimeoutId);
}
botkit.trigger('rtm_close', [bot]);

Expand Down
19 changes: 19 additions & 0 deletions readme-slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ var controller = Botkit.slackbot({
});
```

#### Botkit.slackbot()
| Argument | Description
|--- |---
| config | Configuration object

Creates a new Botkit SlackBot controller.

```javascript
var controller = Botkit.slackbot({debug: true})
```

`config` object accepts these properties:

| Name | Value | Description
|--- |--- |---
| debug | Boolean | Enable debug logging
| stale_connection_timeout | Positive integer | Number of milliseconds to wait for a connection keep-alive "pong" response before declaring the connection stale. Default is `12000`


#### controller.spawn()
| Argument | Description
|--- |---
Expand Down