Skip to content

Commit

Permalink
Reconnect on connection not open on send() (#210)
Browse files Browse the repository at this point in the history
* update web3

* setup reconnect

* extend error condition

* delay reconnect attempt

* reconnect on interval
  • Loading branch information
Velenir authored Aug 12, 2020
1 parent d7d35c2 commit 44099f5
Show file tree
Hide file tree
Showing 3 changed files with 463 additions and 291 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"node-cache": "^5.1.0",
"node-telegram-bot-api": "^0.40.0",
"rxjs": "7.0.0-beta.0",
"web3": "^1.2.7",
"web3-core": "^1.2.7"
"web3": "^1.2.11",
"web3-core": "^1.2.11"
},
"devDependencies": {
"@types/debug": "^4.1.5",
Expand Down
34 changes: 34 additions & 0 deletions src/services/DfusionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ import { version as dexJsVersion } from '@gnosis.pm/dex-js/package.json'
import { version as contractsVersion } from '@gnosis.pm/dex-contracts/package.json'
import { TCR_LIST_ID, TCR_CACHE_TIME, TOKEN_OVERRIDES } from 'config'

// declaration merging
// to allow for error callback
declare module 'web3-core' {
interface WebsocketProvider {
on(type: string, callback: () => void): void;
on(type: 'error', callback: (error: Error) => void): void
}
interface IpcProvider {
on(type: string, callback: () => void): void;
on(type: 'error', callback: (error: Error) => void): void
}

}

const PEER_COUNT_WARN_THRESHOLD = 3 // Warning if the node has less than X peers
const BLOCK_TIME_ERR_THRESHOLD_MINUTES = 2 // Error if there's no a new block in X min

Expand Down Expand Up @@ -121,6 +135,26 @@ export class DfusionRepoImpl implements DfusionService {
this._tokenIdsFilter = tokenIdsFilter
this._web3 = web3

const provider = web3.currentProvider
if (provider && typeof provider === 'object' && 'on' in provider) {
provider.on('error', (error: Error): void => {
log.error('Web3 Provider error: ', error)
// for now triggers `connection not open on send()`
// but we can aniticipate `on request()`
if (error.message.includes('connection not open on ') || error.message.includes('connection got closed')) {
const intervalId = setInterval(() => {
log.error('Connection failure. Reconnecting...')
provider.reconnect()

provider.once('connect', () => {
log.info('Reconnection successfull')
clearInterval(intervalId)
})
}, 5000)
}
})
}

this._cache = new NodeCache({ useClones: false })
}

Expand Down
Loading

0 comments on commit 44099f5

Please sign in to comment.