Skip to content

Commit

Permalink
add clearTimeout to avoid reconnection hell (#306)
Browse files Browse the repository at this point in the history
* add clearTimeout to avoid reconnection hell

* removed console.log
  • Loading branch information
iambeone authored Feb 11, 2020
1 parent f9ebedd commit 0fbb0c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/block-listeners/cosmos-node-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const config = require('../../config.js')
const WAIT_FOR_BLOCK_DELAY = 5000
const EXPECTED_MAX_BLOCK_WINDOW = 120000

let reconnectionTimeout = {}

// This class establishes an rpc connection to Tendermint.
// Used for listening to events, such as new blocks.
class CosmosNodeSubscription {
Expand Down Expand Up @@ -72,7 +74,12 @@ class CosmosNodeSubscription {
scope.setExtra('rpc_url', network.rpc_url)
Sentry.captureException(new Error(`Lost Tendermint connection`))
})
setTimeout(() => this.connectTendermint(network), 3000)
// need to clear previous timeout to evoid connection hell
clearTimeout(reconnectionTimeout[network.id])
reconnectionTimeout[network.id] = setTimeout(
() => this.connectTendermint(network),
3000
)
}
})
.catch(e => {
Expand All @@ -82,8 +89,12 @@ class CosmosNodeSubscription {
Sentry.captureException(e)
})

clearTimeout(reconnectionTimeout[network.id])
// if can't connect, retry
setTimeout(() => this.connectTendermint(network), 3000)
reconnectionTimeout[network.id] = setTimeout(
() => this.connectTendermint(network),
3000
)
})
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dev-docker": "docker build -t lunie-api-dev . && docker run --init -it -p 4000:4000 lunie-api-dev",
"lint": "eslint index.js config.js lib tests",
"lint-fix": "yarn lint --fix",
"start": "node index.js",
"start": "node index.js ",
"test": "jest"
},
"husky": {
Expand Down

0 comments on commit 0fbb0c9

Please sign in to comment.