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 if connection drops #313

Merged
merged 2 commits into from
Jan 3, 2018
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
12 changes: 9 additions & 3 deletions app/src/renderer/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ module.exports = function (nodeIP) {
rpcOpen: true,
initRPC (nodeIP) {
if (node.rpc) {
console.log('removing old websocket')

// ignore disconnect error
node.rpc.removeAllListeners('error')
node.rpc.on('error', () => {})

node.rpc.ws.destroy()
}

Expand All @@ -29,15 +35,15 @@ module.exports = function (nodeIP) {
// we need to check immediately if he connection fails. later we will not be able to check this error
newRpc.on('error', err => {
console.log('rpc error', err)
if (err.code === 'ECONNREFUSED') {
if (err.code === 'ECONNREFUSED' || err.code === 'ENETUNREACH') {
node.rpcOpen = false
}
})

node.rpc = newRpc
},
rpcReconnect: async (rpcConnecting = node.rpcConnecting) => {
if (rpcConnecting) return
rpcReconnect: async (alreadyConnecting = node.rpcConnecting) => {
if (alreadyConnecting) return null
node.rpcConnecting = true

console.log('trying to reconnect')
Expand Down
26 changes: 25 additions & 1 deletion app/src/renderer/vuex/modules/node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { setTimeout } from 'timers'

'use strict'

export default function ({ node }) {
Expand All @@ -24,7 +26,8 @@ export default function ({ node }) {
state.lastHeader = header
dispatch('maybeUpdateValidators', header)
},
async reconnect ({dispatch}) {
async reconnect ({commit, dispatch}) {
commit('setConnected', false)
await node.rpcReconnect()
dispatch('nodeSubscribe')
},
Expand Down Expand Up @@ -58,6 +61,8 @@ export default function ({ node }) {
commit('setConnected', true)
dispatch('setLastHeader', event.data.data.header)
})

dispatch('pollRPCConnection')
},
async checkConnection ({ commit }) {
try {
Expand All @@ -67,6 +72,25 @@ export default function ({ node }) {
commit('notifyError', {title: 'Critical Error', body: `Couldn't initialize blockchain connector`})
return false
}
},
pollRPCConnection ({state, commit, dispatch}, timeout = 3000) {
if (state.nodeTimeout) return

state.nodeTimeout = setTimeout(() => {
// clear timeout doesn't work
if (state.nodeTimeout) {
state.nodeTimeout = null
dispatch('reconnect')
}
}, timeout)
node.rpc.status((err, res) => {
if (!err) {
state.nodeTimeout = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this call clearTimeout?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see the comment now. Why doesn't it work? I didn't think nulling the return value would work 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, missed the if (state.nodeTimeout). 👍

setTimeout(() => {
dispatch('pollRPCConnection')
}, timeout)
}
})
}
}

Expand Down
14 changes: 13 additions & 1 deletion test/unit/specs/store/node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Module: Node', () => {

it('reacts to rpc disconnection with reconnect', done => {
let failed = false
node.rpc.status = () => done()
node.rpcReconnect = () => done()
node.rpc.on = jest.fn((value, cb) => {
if (value === 'error' && !failed) {
failed = true
Expand Down Expand Up @@ -116,4 +116,16 @@ describe('Module: Node', () => {
}
store.dispatch('nodeSubscribe')
})

it('should ping the node to check connection status', done => {
node.rpc.status = () => done()
store.dispatch('pollRPCConnection')
expect(store.state.node.nodeTimeout).toBeDefined()
})

it('should reconnect if pinging node fails', done => {
node.rpcReconnect = () => done()
node.rpc.status = (cb) => {}
store.dispatch('pollRPCConnection', 0)
})
})