Skip to content
This repository was archived by the owner on Oct 26, 2023. It is now read-only.

Commit 682bf7b

Browse files
committed
Don't use bluebird cancellation feature
using simple timeout instead with check
1 parent fcfdb20 commit 682bf7b

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

setup-test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
import Promise from 'bluebird';
2-
3-
Promise.config({
4-
warnings: true,
5-
longStackTraces: true,
6-
cancellation: true,
7-
monitoring: true,
8-
});
9-
101
beforeEach(() => {
112
jest.clearAllMocks();
123
jest.clearAllTimers();

src/hedge-request.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ export function computeHedgeRequestDelay(
3434
return -1;
3535
}
3636

37-
export function cancelOthersOnFinish(main: Promise<any>, tied: Promise<any>): void {
38-
let finished = false;
39-
main.then(() => (!finished ? tied.cancel() : {}));
40-
tied.then(() => { finished = true; });
41-
}
42-
4337
export function computeLatency(
4438
promise: Promise<any>,
4539
timer: Measured.Stopwatch,
@@ -89,6 +83,24 @@ function registerRequestStat<Req>(
8983
hedgeRequest.then(onFinish('hedge'));
9084
}
9185

86+
function createHeadgeRequest<Req, Rep>(
87+
originalPromise: Promise<Rep>,
88+
delay: number,
89+
service: Service<Req, Rep>,
90+
input: Req
91+
): Promise<Rep> {
92+
let aborted = false;
93+
const onOriginalFinish = () => { aborted = true; };
94+
originalPromise.then(onOriginalFinish, onOriginalFinish);
95+
96+
return new Promise((resolve) => {
97+
const executeHedgeRequest = () => {
98+
if (!aborted) resolve(service(input));
99+
};
100+
setTimeout(executeHedgeRequest, delay);
101+
});
102+
}
103+
92104
export default function hedgeRequestFilter<Req, Rep>(
93105
options: HedgeServiceOptions<Req>,
94106
): Filter<Req, Req, Rep, Rep> {
@@ -112,8 +124,7 @@ export default function hedgeRequestFilter<Req, Rep>(
112124

113125
const hedgeDelay = computeHedgeRequestDelay(latencyHistogram, options);
114126
if (hedgeDelay >= minMs) {
115-
const hedgeRequest = Promise.delay(hedgeDelay).then(() => service(input));
116-
cancelOthersOnFinish(mainRequest, hedgeRequest);
127+
const hedgeRequest = createHeadgeRequest(mainRequest, hedgeDelay, service, input);
117128

118129
const hedgeTimer = new Measured.Stopwatch();
119130
computeLatency(hedgeRequest, hedgeTimer, latencyHistogram, hedgeDelay);

0 commit comments

Comments
 (0)