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

Reconnect on connection not open on send() #210

Merged
merged 5 commits into from
Aug 12, 2020
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
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