Skip to content

Commit

Permalink
Added logic to turn off network requests when Scope dismounts
Browse files Browse the repository at this point in the history
  • Loading branch information
jpellizzari committed Mar 1, 2017
1 parent b417bcd commit 6878780
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
7 changes: 6 additions & 1 deletion client/app/scripts/actions/app-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { parseQuery } from '../utils/search-utils';
import { bufferDeltaUpdate, resumeUpdate,
resetUpdateBuffer } from '../utils/update-buffer-utils';
import { doControlRequest, getAllNodes, getNodesDelta, getNodeDetails,
getTopologies, deletePipe } from '../utils/web-api-utils';
getTopologies, deletePipe, stopTopologyPolling, teardownWebsockets } from '../utils/web-api-utils';
import { getActiveTopologyOptions,
getCurrentTopologyUrl } from '../utils/topology-utils';
import { storageSet } from '../utils/storage-utils';
Expand Down Expand Up @@ -716,3 +716,8 @@ export function changeInstance() {
);
};
}

export function shutdown() {
stopTopologyPolling();
teardownWebsockets();
}
3 changes: 2 additions & 1 deletion client/app/scripts/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Topologies from './topologies';
import TopologyOptions from './topology-options';
import { getApiDetails, getTopologies } from '../utils/web-api-utils';
import { focusSearch, pinNextMetric, hitBackspace, hitEnter, hitEsc, unpinMetric,
selectMetric, toggleHelp, toggleGridMode } from '../actions/app-actions';
selectMetric, toggleHelp, toggleGridMode, shutdown } from '../actions/app-actions';
import Details from './details';
import Nodes from './nodes';
import GridModeSelector from './grid-mode-selector';
Expand Down Expand Up @@ -52,6 +52,7 @@ class App extends React.Component {
componentWillUnmount() {
window.removeEventListener('keypress', this.onKeyPress);
window.removeEventListener('keyup', this.onKeyUp);
shutdown();
}

onKeyUp(ev) {
Expand Down
19 changes: 9 additions & 10 deletions client/app/scripts/utils/router-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,20 @@ export function updateRoute(getState) {


export function getRouter(dispatch, initialState) {
let mergedState = initialState;
// strip any trailing '/'s.
page.base(window.location.pathname.replace(/\/$/, ''));

const storageState = storageGet(STORAGE_STATE_KEY);
if (storageState) {
window.location.hash = `!/state/${storageState}`;
const parsedState = JSON.parse(decodeURL(storageState));
mergedState = Object.assign(initialState, parsedState);
}

page('/', () => {
// recover from storage state on empty URL
const storageState = storageGet(STORAGE_STATE_KEY);
if (storageState) {
// push storage state to URL
window.location.hash = `!/state/${storageState}`;
const parsedState = JSON.parse(decodeURL(storageState));
const mergedState = Object.assign(initialState, parsedState);
dispatch(route(mergedState));
} else {
dispatch(route(initialState));
}
dispatch(route(mergedState));
});

page('/state/:state', (ctx) => {
Expand Down
27 changes: 23 additions & 4 deletions client/app/scripts/utils/web-api-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ function createWebsocket(topologyUrl, optionsQuery, dispatch) {
* Any opts that get passed in will override the defaults.
*/
function doRequest(opts) {
const config = defaults(opts, { contentType: 'application/json' });
const config = defaults(opts, {
contentType: 'application/json',
type: 'json'
});
if (csrfToken) {
config.headers = Object.assign({}, config.headers, { 'X-CSRF-Token': csrfToken });
}
Expand Down Expand Up @@ -193,9 +196,10 @@ export function getTopologies(options, dispatch) {

export function getNodesDelta(topologyUrl, options, dispatch, forceReload) {
const optionsQuery = buildOptionsQuery(options);
// only recreate websocket if url changed or if forced (weave cloud instance reload);
const isNewUrl = topologyUrl && (topologyUrl !== currentUrl || currentOptions !== optionsQuery);

// Only recreate websocket if url changed or if forced (weave cloud instance reload);
// Check for truthy options and that options have changed.
const isNewOptions = currentOptions && currentOptions !== optionsQuery;
const isNewUrl = topologyUrl && (topologyUrl !== currentUrl || isNewOptions);
if (forceReload || isNewUrl) {
createWebsocket(topologyUrl, optionsQuery, dispatch);
currentUrl = topologyUrl;
Expand Down Expand Up @@ -348,3 +352,18 @@ export function getPipeStatus(pipeId, dispatch) {
}
});
}

export function stopTopologyPolling() {
clearTimeout(topologyTimer);
topologyTimer = 0;
}

export function teardownWebsockets() {
clearTimeout(reconnectTimer);
if (socket) {
socket.onerror = null;
socket.onclose = null;
socket.close();
currentOptions = null;
}
}

0 comments on commit 6878780

Please sign in to comment.