Skip to content

Commit

Permalink
[ML] Use abort signal instead of isCancelledRef.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Nov 5, 2021
1 parent 3e411cd commit 97890cf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ describe('useFailedTransactionsCorrelations', () => {
jest.advanceTimersByTime(100);
await waitFor(() => expect(result.current.progress.loaded).toBe(0));
jest.advanceTimersByTime(100);
await waitFor(() => expect(result.current.progress.loaded).toBe(0));
jest.advanceTimersByTime(100);
await waitFor(() => expect(result.current.progress.loaded).toBe(0.05));

expect(result.current.progress).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,11 @@ export function useFailedTransactionsCorrelations() {
[]
);

// `abortCtrl` is used to cancel individual requests that already started.
// `isCancelledRef` is used to cancel the overall task in between requests in the `startFetch` callback.
const abortCtrl = useRef(new AbortController());
// We're using a ref here because otherwise the startFetch function might have
// a stale value for checking if the task has been cancelled.
const isCancelledRef = useRef(false);

const startFetch = useCallback(async () => {
abortCtrl.current.abort();
abortCtrl.current = new AbortController();
isCancelledRef.current = false;

setResponse({
...getInitialResponse(),
Expand All @@ -85,36 +79,46 @@ export function useFailedTransactionsCorrelations() {
ccsWarning: false,
};

// Initial call to fetch the overall distribution for the log-log plot.
const { overallHistogram, percentileThresholdValue } = await callApmApi({
endpoint: 'POST /internal/apm/latency/overall_distribution',
signal: abortCtrl.current.signal,
params: {
body: {
...fetchParams,
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
},
},
});
responseUpdate.overallHistogram = overallHistogram;
responseUpdate.percentileThresholdValue = percentileThresholdValue;
const [overallHistogramResponse, errorHistogramRespone] =
await Promise.all([
// Initial call to fetch the overall distribution for the log-log plot.
callApmApi({
endpoint: 'POST /internal/apm/latency/overall_distribution',
signal: abortCtrl.current.signal,
params: {
body: {
...fetchParams,
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
},
},
}),
callApmApi({
endpoint: 'POST /internal/apm/latency/overall_distribution',
signal: abortCtrl.current.signal,
params: {
body: {
...fetchParams,
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
termFilters: [
{
fieldName: EVENT_OUTCOME,
fieldValue: EventOutcome.failure,
},
],
},
},
}),
]);

const { overallHistogram, percentileThresholdValue } =
overallHistogramResponse;
const { overallHistogram: errorHistogram } = errorHistogramRespone;

const { overallHistogram: errorHistogram } = await callApmApi({
endpoint: 'POST /internal/apm/latency/overall_distribution',
signal: abortCtrl.current.signal,
params: {
body: {
...fetchParams,
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
termFilters: [
{ fieldName: EVENT_OUTCOME, fieldValue: EventOutcome.failure },
],
},
},
});
responseUpdate.errorHistogram = errorHistogram;
responseUpdate.overallHistogram = overallHistogram;
responseUpdate.percentileThresholdValue = percentileThresholdValue;

if (isCancelledRef.current) {
if (abortCtrl.current.signal.aborted) {
return;
}

Expand All @@ -132,7 +136,7 @@ export function useFailedTransactionsCorrelations() {
},
});

if (isCancelledRef.current) {
if (abortCtrl.current.signal.aborted) {
return;
}

Expand Down Expand Up @@ -182,7 +186,7 @@ export function useFailedTransactionsCorrelations() {
PROGRESS_STEP_P_VALUES,
});

if (isCancelledRef.current) {
if (abortCtrl.current.signal.aborted) {
return;
}
}
Expand All @@ -204,7 +208,7 @@ export function useFailedTransactionsCorrelations() {
setResponse({ ...responseUpdate, loaded: LOADED_DONE, isRunning: false });
setResponse.flush();
} catch (e) {
if (!isCancelledRef.current) {
if (!abortCtrl.current.signal.aborted) {
const err = e as Error | IHttpFetchError<ResponseErrorBody>;
setResponse({
error:
Expand All @@ -219,7 +223,6 @@ export function useFailedTransactionsCorrelations() {
}, [fetchParams, setResponse]);

const cancelFetch = useCallback(() => {
isCancelledRef.current = true;
abortCtrl.current.abort();
setResponse({
isRunning: false,
Expand All @@ -231,7 +234,6 @@ export function useFailedTransactionsCorrelations() {
useEffect(() => {
startFetch();
return () => {
isCancelledRef.current = true;
abortCtrl.current.abort();
};
}, [startFetch, cancelFetch]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,11 @@ export function useLatencyCorrelations() {
[]
);

// `abortCtrl` is used to cancel individual requests that already started.
// `isCancelledRef` is used to cancel the overall task in between requests in the `startFetch` callback.
const abortCtrl = useRef(new AbortController());
// We're using a ref here because otherwise the startFetch function might have
// a stale value for checking if the task has been cancelled.
const isCancelledRef = useRef(false);

const startFetch = useCallback(async () => {
abortCtrl.current.abort();
abortCtrl.current = new AbortController();
isCancelledRef.current = false;

setResponse({
...getInitialResponse(),
Expand Down Expand Up @@ -100,7 +94,7 @@ export function useLatencyCorrelations() {
responseUpdate.overallHistogram = overallHistogram;
responseUpdate.percentileThresholdValue = percentileThresholdValue;

if (isCancelledRef.current) {
if (abortCtrl.current.signal.aborted) {
return;
}

Expand All @@ -118,7 +112,7 @@ export function useLatencyCorrelations() {
},
});

if (isCancelledRef.current) {
if (abortCtrl.current.signal.aborted) {
return;
}

Expand Down Expand Up @@ -149,7 +143,7 @@ export function useLatencyCorrelations() {
fieldValuePairs.push(...fieldValuePairChunkResponse.fieldValuePairs);
}

if (isCancelledRef.current) {
if (abortCtrl.current.signal.aborted) {
return;
}

Expand All @@ -162,7 +156,7 @@ export function useLatencyCorrelations() {
});
}

if (isCancelledRef.current) {
if (abortCtrl.current.signal.aborted) {
return;
}

Expand Down Expand Up @@ -206,7 +200,7 @@ export function useLatencyCorrelations() {
PROGRESS_STEP_CORRELATIONS,
});

if (isCancelledRef.current) {
if (abortCtrl.current.signal.aborted) {
return;
}
}
Expand All @@ -232,7 +226,7 @@ export function useLatencyCorrelations() {
});
setResponse.flush();
} catch (e) {
if (!isCancelledRef.current) {
if (!abortCtrl.current.signal.aborted) {
const err = e as Error | IHttpFetchError<ResponseErrorBody>;
setResponse({
error:
Expand All @@ -247,7 +241,6 @@ export function useLatencyCorrelations() {
}, [fetchParams, setResponse]);

const cancelFetch = useCallback(() => {
isCancelledRef.current = true;
abortCtrl.current.abort();
setResponse({
isRunning: false,
Expand All @@ -259,7 +252,6 @@ export function useLatencyCorrelations() {
useEffect(() => {
startFetch();
return () => {
isCancelledRef.current = true;
abortCtrl.current.abort();
};
}, [startFetch, cancelFetch]);
Expand Down

0 comments on commit 97890cf

Please sign in to comment.