Smarter Polling #3240

Only refresh statuses when on Status Page for
  some statuses (eg. hashrate)
This commit is contained in:
Nicolas Gotchac 2016-11-17 20:19:11 +01:00
parent 5fd66d2c97
commit 1c3923c9e8
5 changed files with 152 additions and 62 deletions

View File

@ -16,7 +16,7 @@
import { newError } from '../ui/Errors/actions'; import { newError } from '../ui/Errors/actions';
import { setAddressImage } from './providers/imagesActions'; import { setAddressImage } from './providers/imagesActions';
import { clearStatusLogs, toggleStatusLogs } from './providers/statusActions'; import { clearStatusLogs, toggleStatusLogs, toggleStatusRefresh } from './providers/statusActions';
import { toggleView } from '../views/Settings'; import { toggleView } from '../views/Settings';
export { export {
@ -24,5 +24,6 @@ export {
clearStatusLogs, clearStatusLogs,
setAddressImage, setAddressImage,
toggleStatusLogs, toggleStatusLogs,
toggleStatusRefresh,
toggleView toggleView
}; };

View File

@ -25,31 +25,19 @@ export default class Status {
this._pingable = false; this._pingable = false;
this._apiStatus = {}; this._apiStatus = {};
this._status = {}; this._status = {};
this._longStatus = {};
this._minerSettings = {};
this._pollPingTimeoutId = null; this._pollPingTimeoutId = null;
this._longStatusTimeoutId = null;
} }
start () { start () {
this._subscribeBlockNumber(); this._subscribeBlockNumber();
this._pollPing(); this._pollPing();
this._pollStatus(); this._pollStatus();
this._pollLongStatus();
this._pollLogs(); this._pollLogs();
this._fetchEnode();
}
_fetchEnode () {
this._api.parity
.enode()
.then((enode) => {
if (this._store.state.nodeStatus.enode !== enode) {
this._store.dispatch(statusCollection({ enode }));
}
})
.catch(() => {
window.setTimeout(() => {
this._fetchEnode();
}, 1000);
});
} }
_subscribeBlockNumber () { _subscribeBlockNumber () {
@ -121,12 +109,12 @@ export default class Status {
} }
_pollStatus = () => { _pollStatus = () => {
const { isConnected, isConnecting, needsToken, secureToken } = this._api;
const nextTimeout = (timeout = 1000) => { const nextTimeout = (timeout = 1000) => {
setTimeout(this._pollStatus, timeout); setTimeout(this._pollStatus, timeout);
}; };
const { isConnected, isConnecting, needsToken, secureToken } = this._api;
const apiStatus = { const apiStatus = {
isConnected, isConnected,
isConnecting, isConnecting,
@ -134,6 +122,12 @@ export default class Status {
secureToken secureToken
}; };
const gotReconnected = !this._apiStatus.isConnected && apiStatus.isConnected;
if (gotReconnected) {
this._pollLongStatus();
}
if (!isEqual(apiStatus, this._apiStatus)) { if (!isEqual(apiStatus, this._apiStatus)) {
this._store.dispatch(statusCollection(apiStatus)); this._store.dispatch(statusCollection(apiStatus));
this._apiStatus = apiStatus; this._apiStatus = apiStatus;
@ -147,57 +141,130 @@ export default class Status {
} }
if (!isConnected) { if (!isConnected) {
nextTimeout(250); return nextTimeout(250);
return; }
const { refreshStatus } = this._store.getState().nodeStatus;
const statusPromises = [ this._api.eth.syncing() ];
if (refreshStatus) {
statusPromises.push(this._api.eth.hashrate());
statusPromises.push(this._api.parity.netPeers());
} }
Promise Promise
.all([ .all(statusPromises)
this._api.web3.clientVersion(), .then((statusResults) => {
this._api.eth.coinbase(), const status = statusResults.length === 1
this._api.parity.defaultExtraData(), ? {
this._api.parity.extraData(), syncing: statusResults[0]
this._api.parity.gasFloorTarget(), }
this._api.eth.hashrate(), : {
this._api.parity.minGasPrice(), syncing: statusResults[0],
this._api.parity.netChain(), hashrate: statusResults[1],
this._api.parity.netPeers(), netPeers: statusResults[2]
this._api.parity.netPort(),
this._api.parity.nodeName(),
this._api.parity.rpcSettings(),
this._api.eth.syncing()
])
.then(([clientVersion, coinbase, defaultExtraData, extraData, gasFloorTarget, hashrate, minGasPrice, netChain, netPeers, netPort, nodeName, rpcSettings, syncing, traceMode]) => {
const isTest = netChain === 'morden' || netChain === 'testnet';
const status = {
clientVersion,
coinbase,
defaultExtraData,
extraData,
gasFloorTarget,
hashrate,
minGasPrice,
netChain,
netPeers,
netPort,
nodeName,
rpcSettings,
syncing,
isTest,
traceMode
}; };
if (!isEqual(status, this._status)) { if (!isEqual(status, this._status)) {
this._store.dispatch(statusCollection(status)); this._store.dispatch(statusCollection(status));
this._status = status; this._status = status;
} }
nextTimeout();
}) })
.catch((error) => { .catch((error) => {
console.error('_pollStatus', error); console.error('_pollStatus', error);
nextTimeout(250);
}); });
}
/**
* Miner settings should never changes unless
* Parity is restarted, or if the values are changed
* from the UI
*/
_pollMinerSettings = () => {
Promise
.all([
this._api.eth.coinbase(),
this._api.parity.extraData(),
this._api.parity.minGasPrice(),
this._api.parity.gasFloorTarget()
])
.then(([
coinbase, extraData, minGasPrice, gasFloorTarget
]) => {
const minerSettings = {
coinbase,
extraData,
minGasPrice,
gasFloorTarget
};
if (!isEqual(minerSettings, this._minerSettings)) {
this._store.dispatch(statusCollection(minerSettings));
this._minerSettings = minerSettings;
}
})
.catch((error) => {
console.error('_pollMinerSettings', error);
});
}
/**
* The data fetched here should not change
* unless Parity is restarted. They are thus
* fetched every 30s just in case, and whenever
* the client got reconnected.
*/
_pollLongStatus = () => {
const nextTimeout = (timeout = 30000) => {
if (this._longStatusTimeoutId) {
clearTimeout(this._longStatusTimeoutId);
}
this._longStatusTimeoutId = setTimeout(this._pollLongStatus, timeout);
};
// Poll Miner settings just in case
this._pollMinerSettings();
Promise
.all([
this._api.web3.clientVersion(),
this._api.parity.defaultExtraData(),
this._api.parity.netChain(),
this._api.parity.netPort(),
this._api.parity.rpcSettings(),
this._api.parity.enode()
])
.then(([
clientVersion, defaultExtraData, netChain, netPort, rpcSettings, enode
]) => {
const isTest = netChain === 'morden' || netChain === 'testnet';
const longStatus = {
clientVersion,
defaultExtraData,
netChain,
netPort,
rpcSettings,
enode,
isTest
};
if (!isEqual(longStatus, this._longStatus)) {
this._store.dispatch(statusCollection(longStatus));
this._longStatus = longStatus;
}
nextTimeout(); nextTimeout();
})
.catch((error) => {
console.error('_pollLongStatus', error);
nextTimeout(250);
});
} }
_pollLogs = () => { _pollLogs = () => {

View File

@ -47,3 +47,10 @@ export function clearStatusLogs () {
type: 'clearStatusLogs' type: 'clearStatusLogs'
}; };
} }
export function toggleStatusRefresh (refreshStatus) {
return {
type: 'toggleStatusRefresh',
refreshStatus
};
}

View File

@ -37,12 +37,13 @@ const initialState = {
max: new BigNumber(0) max: new BigNumber(0)
}, },
netPort: new BigNumber(0), netPort: new BigNumber(0),
nodeName: '',
rpcSettings: {}, rpcSettings: {},
syncing: false, syncing: false,
isApiConnected: true, isConnected: false,
isPingConnected: true, isConnecting: false,
isPingable: false,
isTest: false, isTest: false,
refreshStatus: false,
traceMode: undefined traceMode: undefined
}; };
@ -73,5 +74,10 @@ export default handleActions({
clearStatusLogs (state, action) { clearStatusLogs (state, action) {
return Object.assign({}, state, { devLogs: [] }); return Object.assign({}, state, { devLogs: [] });
},
toggleStatusRefresh (state, action) {
const { refreshStatus } = action;
return Object.assign({}, state, { refreshStatus });
} }
}, initialState); }, initialState);

View File

@ -18,7 +18,7 @@ import React, { Component, PropTypes } from 'react';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { clearStatusLogs, toggleStatusLogs } from '../../../../redux/actions'; import { clearStatusLogs, toggleStatusLogs, toggleStatusRefresh } from '../../../../redux/actions';
import Debug from '../../components/Debug'; import Debug from '../../components/Debug';
import Status from '../../components/Status'; import Status from '../../components/Status';
@ -31,6 +31,14 @@ class StatusPage extends Component {
actions: PropTypes.object.isRequired actions: PropTypes.object.isRequired
} }
componentWillMount () {
this.props.actions.toggleStatusRefresh(true);
}
componentWillUnmount () {
this.props.actions.toggleStatusRefresh(false);
}
render () { render () {
return ( return (
<div className={ styles.body }> <div className={ styles.body }>
@ -49,7 +57,8 @@ function mapDispatchToProps (dispatch) {
return { return {
actions: bindActionCreators({ actions: bindActionCreators({
clearStatusLogs, clearStatusLogs,
toggleStatusLogs toggleStatusLogs,
toggleStatusRefresh
}, dispatch) }, dispatch)
}; };
} }