Skip to content

Commit 97890cf

Browse files
committed
[ML] Use abort signal instead of isCancelledRef.
1 parent 3e411cd commit 97890cf

File tree

3 files changed

+46
-54
lines changed

3 files changed

+46
-54
lines changed

x-pack/plugins/apm/public/components/app/correlations/use_failed_transactions_correlations.test.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ describe('useFailedTransactionsCorrelations', () => {
167167
jest.advanceTimersByTime(100);
168168
await waitFor(() => expect(result.current.progress.loaded).toBe(0));
169169
jest.advanceTimersByTime(100);
170-
await waitFor(() => expect(result.current.progress.loaded).toBe(0));
171-
jest.advanceTimersByTime(100);
172170
await waitFor(() => expect(result.current.progress.loaded).toBe(0.05));
173171

174172
expect(result.current.progress).toEqual({

x-pack/plugins/apm/public/components/app/correlations/use_failed_transactions_correlations.ts

+40-38
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,11 @@ export function useFailedTransactionsCorrelations() {
5252
[]
5353
);
5454

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

6257
const startFetch = useCallback(async () => {
6358
abortCtrl.current.abort();
6459
abortCtrl.current = new AbortController();
65-
isCancelledRef.current = false;
6660

6761
setResponse({
6862
...getInitialResponse(),
@@ -85,36 +79,46 @@ export function useFailedTransactionsCorrelations() {
8579
ccsWarning: false,
8680
};
8781

88-
// Initial call to fetch the overall distribution for the log-log plot.
89-
const { overallHistogram, percentileThresholdValue } = await callApmApi({
90-
endpoint: 'POST /internal/apm/latency/overall_distribution',
91-
signal: abortCtrl.current.signal,
92-
params: {
93-
body: {
94-
...fetchParams,
95-
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
96-
},
97-
},
98-
});
99-
responseUpdate.overallHistogram = overallHistogram;
100-
responseUpdate.percentileThresholdValue = percentileThresholdValue;
82+
const [overallHistogramResponse, errorHistogramRespone] =
83+
await Promise.all([
84+
// Initial call to fetch the overall distribution for the log-log plot.
85+
callApmApi({
86+
endpoint: 'POST /internal/apm/latency/overall_distribution',
87+
signal: abortCtrl.current.signal,
88+
params: {
89+
body: {
90+
...fetchParams,
91+
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
92+
},
93+
},
94+
}),
95+
callApmApi({
96+
endpoint: 'POST /internal/apm/latency/overall_distribution',
97+
signal: abortCtrl.current.signal,
98+
params: {
99+
body: {
100+
...fetchParams,
101+
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
102+
termFilters: [
103+
{
104+
fieldName: EVENT_OUTCOME,
105+
fieldValue: EventOutcome.failure,
106+
},
107+
],
108+
},
109+
},
110+
}),
111+
]);
112+
113+
const { overallHistogram, percentileThresholdValue } =
114+
overallHistogramResponse;
115+
const { overallHistogram: errorHistogram } = errorHistogramRespone;
101116

102-
const { overallHistogram: errorHistogram } = await callApmApi({
103-
endpoint: 'POST /internal/apm/latency/overall_distribution',
104-
signal: abortCtrl.current.signal,
105-
params: {
106-
body: {
107-
...fetchParams,
108-
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
109-
termFilters: [
110-
{ fieldName: EVENT_OUTCOME, fieldValue: EventOutcome.failure },
111-
],
112-
},
113-
},
114-
});
115117
responseUpdate.errorHistogram = errorHistogram;
118+
responseUpdate.overallHistogram = overallHistogram;
119+
responseUpdate.percentileThresholdValue = percentileThresholdValue;
116120

117-
if (isCancelledRef.current) {
121+
if (abortCtrl.current.signal.aborted) {
118122
return;
119123
}
120124

@@ -132,7 +136,7 @@ export function useFailedTransactionsCorrelations() {
132136
},
133137
});
134138

135-
if (isCancelledRef.current) {
139+
if (abortCtrl.current.signal.aborted) {
136140
return;
137141
}
138142

@@ -182,7 +186,7 @@ export function useFailedTransactionsCorrelations() {
182186
PROGRESS_STEP_P_VALUES,
183187
});
184188

185-
if (isCancelledRef.current) {
189+
if (abortCtrl.current.signal.aborted) {
186190
return;
187191
}
188192
}
@@ -204,7 +208,7 @@ export function useFailedTransactionsCorrelations() {
204208
setResponse({ ...responseUpdate, loaded: LOADED_DONE, isRunning: false });
205209
setResponse.flush();
206210
} catch (e) {
207-
if (!isCancelledRef.current) {
211+
if (!abortCtrl.current.signal.aborted) {
208212
const err = e as Error | IHttpFetchError<ResponseErrorBody>;
209213
setResponse({
210214
error:
@@ -219,7 +223,6 @@ export function useFailedTransactionsCorrelations() {
219223
}, [fetchParams, setResponse]);
220224

221225
const cancelFetch = useCallback(() => {
222-
isCancelledRef.current = true;
223226
abortCtrl.current.abort();
224227
setResponse({
225228
isRunning: false,
@@ -231,7 +234,6 @@ export function useFailedTransactionsCorrelations() {
231234
useEffect(() => {
232235
startFetch();
233236
return () => {
234-
isCancelledRef.current = true;
235237
abortCtrl.current.abort();
236238
};
237239
}, [startFetch, cancelFetch]);

x-pack/plugins/apm/public/components/app/correlations/use_latency_correlations.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,11 @@ export function useLatencyCorrelations() {
5454
[]
5555
);
5656

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

6459
const startFetch = useCallback(async () => {
6560
abortCtrl.current.abort();
6661
abortCtrl.current = new AbortController();
67-
isCancelledRef.current = false;
6862

6963
setResponse({
7064
...getInitialResponse(),
@@ -100,7 +94,7 @@ export function useLatencyCorrelations() {
10094
responseUpdate.overallHistogram = overallHistogram;
10195
responseUpdate.percentileThresholdValue = percentileThresholdValue;
10296

103-
if (isCancelledRef.current) {
97+
if (abortCtrl.current.signal.aborted) {
10498
return;
10599
}
106100

@@ -118,7 +112,7 @@ export function useLatencyCorrelations() {
118112
},
119113
});
120114

121-
if (isCancelledRef.current) {
115+
if (abortCtrl.current.signal.aborted) {
122116
return;
123117
}
124118

@@ -149,7 +143,7 @@ export function useLatencyCorrelations() {
149143
fieldValuePairs.push(...fieldValuePairChunkResponse.fieldValuePairs);
150144
}
151145

152-
if (isCancelledRef.current) {
146+
if (abortCtrl.current.signal.aborted) {
153147
return;
154148
}
155149

@@ -162,7 +156,7 @@ export function useLatencyCorrelations() {
162156
});
163157
}
164158

165-
if (isCancelledRef.current) {
159+
if (abortCtrl.current.signal.aborted) {
166160
return;
167161
}
168162

@@ -206,7 +200,7 @@ export function useLatencyCorrelations() {
206200
PROGRESS_STEP_CORRELATIONS,
207201
});
208202

209-
if (isCancelledRef.current) {
203+
if (abortCtrl.current.signal.aborted) {
210204
return;
211205
}
212206
}
@@ -232,7 +226,7 @@ export function useLatencyCorrelations() {
232226
});
233227
setResponse.flush();
234228
} catch (e) {
235-
if (!isCancelledRef.current) {
229+
if (!abortCtrl.current.signal.aborted) {
236230
const err = e as Error | IHttpFetchError<ResponseErrorBody>;
237231
setResponse({
238232
error:
@@ -247,7 +241,6 @@ export function useLatencyCorrelations() {
247241
}, [fetchParams, setResponse]);
248242

249243
const cancelFetch = useCallback(() => {
250-
isCancelledRef.current = true;
251244
abortCtrl.current.abort();
252245
setResponse({
253246
isRunning: false,
@@ -259,7 +252,6 @@ export function useLatencyCorrelations() {
259252
useEffect(() => {
260253
startFetch();
261254
return () => {
262-
isCancelledRef.current = true;
263255
abortCtrl.current.abort();
264256
};
265257
}, [startFetch, cancelFetch]);

0 commit comments

Comments
 (0)