Skip to content

Commit

Permalink
fix: update noop interval management
Browse files Browse the repository at this point in the history
refs: IRIS-4216
  • Loading branch information
CataldoMazzilli authored May 3, 2023
2 parents 40b05a1 + ea81c89 commit 819376c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
9 changes: 4 additions & 5 deletions src/network/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { report } from '../reporting';
import { useAccountStore } from '../store/account';
import { IS_STANDALONE, SHELL_APP_ID } from '../constants';
import { useNetworkStore } from '../store/network';
import { handleSync } from '../store/network/utils';
import { getPollingInterval, handleSync } from '../store/network/utils';

export const fetchNoOp = (): void => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
Expand Down Expand Up @@ -92,7 +92,7 @@ const normalizeContext = (context: any): SoapContext => {
};

const handleResponse = <R>(api: string, res: SoapResponse<R>): R | ErrorSoapBodyResponse => {
const { pollingInterval, noOpTimeout } = useNetworkStore.getState();
const { noOpTimeout } = useNetworkStore.getState();
const { usedQuota } = useAccountStore.getState();
clearTimeout(noOpTimeout);
if (res.Body.Fault) {
Expand Down Expand Up @@ -126,9 +126,8 @@ const handleResponse = <R>(api: string, res: SoapResponse<R>): R | ErrorSoapBody
useAccountStore.setState({
usedQuota: responseUsedQuota ?? usedQuota
});
const nextPollingInterval = (res?.Body as { waitDisallowed?: number })?.waitDisallowed
? 10000
: pollingInterval;

const nextPollingInterval = getPollingInterval(res);
useNetworkStore.setState({
noOpTimeout: setTimeout(() => fetchNoOp(), nextPollingInterval),
pollingInterval: nextPollingInterval,
Expand Down
18 changes: 2 additions & 16 deletions src/network/get-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,11 @@ import { filter } from 'lodash';
import { SHELL_APP_ID } from '../constants';
import { useAppStore } from '../store/app';
import { normalizeAccount } from '../store/account/normalization';
import { AccountSettings, GetInfoResponse, CarbonioModule } from '../../types';
import { GetInfoResponse, CarbonioModule } from '../../types';
import { getSoapFetch } from './fetch';
import { useAccountStore } from '../store/account';
import { useNetworkStore } from '../store/network';

const parsePollingInterval = (settings: AccountSettings): number => {
const pollingPref = (settings.prefs?.zimbraPrefMailPollingInterval ?? '') as string;
if (pollingPref === '500') {
return 500;
}
const pollingValue = parseInt(pollingPref, 10);
if (Number.isNaN(pollingValue)) {
return 30000;
}
if (pollingPref.includes('m')) {
return pollingValue * 60 * 1000;
}
return pollingValue * 1000;
};
import { parsePollingInterval } from '../store/network/utils';

export const getInfo = (): Promise<void> =>
fetch('/static/iris/components.json')
Expand Down
53 changes: 52 additions & 1 deletion src/store/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,62 @@
*/

import { forEach } from 'lodash';
import { SoapContext } from '../../../types';
import { AccountSettings, SoapContext, SoapResponse } from '../../../types';
import { folderWorker, tagWorker } from '../../workers';
import { useFolderStore } from '../folder';
import { useTagStore } from '../tags';
import { useNetworkStore } from './store';
import { useAccountStore } from '../account';

/**
* Polling interval to use if the long polling delay
* is not allowed for the user
*/
const POLLING_NOWAIT_INTERVAL = 10_000;

/**
* Polling interval to use if a previous request failed
* with a 500 error
*/
const POLLING_RETRY_INTERVAL = 60_000;

export const parsePollingInterval = (settings: AccountSettings): number => {
const pollingPref = (settings.prefs?.zimbraPrefMailPollingInterval ?? '') as string;
if (pollingPref === '500') {
return 500;
}
const pollingValue = parseInt(pollingPref, 10);
if (Number.isNaN(pollingValue)) {
return 30000;
}
if (pollingPref.includes('m')) {
return pollingValue * 60 * 1000;
}
return pollingValue * 1000;
};

/**
* Return the polling interval for the next NoOp request.
* The interval length depends on the user settings, but it can be
* overridden by the server response/errors
* @param res
*/
export const getPollingInterval = <R>(res: SoapResponse<R>): number => {
const { pollingInterval } = useNetworkStore.getState();
const { settings } = useAccountStore.getState();
const waitDisallowed = (res?.Body as { waitDisallowed?: number })?.waitDisallowed;
const fault = res?.Body?.Fault;
if (fault) {
return POLLING_RETRY_INTERVAL;
}
if (waitDisallowed) {
return POLLING_NOWAIT_INTERVAL;
}
if (!fault) {
return parsePollingInterval(settings);
}
return pollingInterval;
};

export const handleSync = ({ refresh, notify }: SoapContext): Promise<void> =>
new Promise((r) => {
Expand Down

0 comments on commit 819376c

Please sign in to comment.