Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retry stuck requests #4426

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions lib/common/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as url from "url";
import { EOL } from "os";
import { TLSSocket } from "tls";
import * as helpers from "./helpers";
import * as zlib from "zlib";
import * as util from "util";
Expand Down Expand Up @@ -105,8 +106,6 @@ export class HttpClient implements Server.IHttpClient {

const result = new Promise<Server.IResponse>((resolve, reject) => {
let timerId: number;
let stuckRequestTimerId: number;
let hasResponse = false;
const cleanupRequestData: ICleanupRequestData = Object.create({ timers: [] });
this.cleanupData.push(cleanupRequestData);

Expand All @@ -133,17 +132,6 @@ export class HttpClient implements Server.IHttpClient {
const requestObj = request(options);
cleanupRequestData.req = requestObj;

if (options.method !== "PUT" && options.method !== "POST") {
stuckRequestTimerId = setTimeout(() => {
clearTimeout(stuckRequestTimerId);
stuckRequestTimerId = null;
if (!hasResponse) {
this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(HttpClient.STUCK_REQUEST_ERROR_MESSAGE) });
}
}, options.timeout || HttpClient.STUCK_REQUEST_TIMEOUT);
cleanupRequestData.timers.push(stuckRequestTimerId);
}

requestObj
.on("error", (err: IHttpRequestError) => {
this.$logger.trace("An error occurred while sending the request:", err);
Expand All @@ -158,9 +146,22 @@ export class HttpClient implements Server.IHttpClient {
err.message = errorMessage || err.message;
this.setResponseResult(promiseActions, cleanupRequestData, { err });
})
.on("socket", (s: TLSSocket) => {
let stuckRequestTimerId: number;

stuckRequestTimerId = setTimeout(() => {
this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(HttpClient.STUCK_REQUEST_ERROR_MESSAGE) });
}, options.timeout || HttpClient.STUCK_REQUEST_TIMEOUT);

cleanupRequestData.timers.push(stuckRequestTimerId);

s.once("secureConnect", () => {
clearTimeout(stuckRequestTimerId);
stuckRequestTimerId = null;
});
})
.on("response", (response: Server.IRequestResponseData) => {
cleanupRequestData.res = response;
hasResponse = true;
let lastChunkTimestamp = Date.now();
cleanupRequestData.stuckResponseIntervalId = setInterval(() => {
if (Date.now() - lastChunkTimestamp > HttpClient.STUCK_RESPONSE_CHECK_INTERVAL) {
Expand Down