Skip to content

Commit 9609814

Browse files
Cleanup the req/res streams
1 parent 1242a7b commit 9609814

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lib/common/declarations.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ declare module Server {
163163
interface IRequestResponseData {
164164
statusCode: number;
165165
headers: { [index: string]: any };
166+
complete: boolean;
166167
pipe(destination: any, options?: { end?: boolean; }): IRequestResponseData;
167168
on(event: string, listener: Function): void;
169+
destroy(error?: Error): void;
168170
}
169171
}
170172

@@ -758,7 +760,7 @@ interface IAnalyticsSettingsService {
758760
* Gets information for projects that are exported from playground
759761
* @param projectDir Project directory path
760762
*/
761-
getPlaygroundInfo(projectDir?: string): Promise<IPlaygroundInfo>;
763+
getPlaygroundInfo(projectDir?: string): Promise<IPlaygroundInfo>;
762764
}
763765

764766
/**

lib/common/http-client.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export class HttpClient implements Server.IHttpClient {
2424
private $staticConfig: Config.IStaticConfig) {
2525
this.cleanupData = [];
2626
this.$processService.attachToProcessExitSignals(this, () => {
27-
this.cleanupData.forEach(d => this.cleanupAfterRequest(d));
27+
this.cleanupData.forEach(d => {
28+
this.cleanupAfterRequest(d);
29+
});
2830
});
2931
}
3032

@@ -106,7 +108,7 @@ export class HttpClient implements Server.IHttpClient {
106108
let timerId: number;
107109
let stuckRequestTimerId: number;
108110
let hasResponse = false;
109-
const cleanupRequestData: ICleanupRequestData = { timers: [], stuckResponseIntervalId: null };
111+
const cleanupRequestData: ICleanupRequestData = Object.create({ timers: [] });
110112
this.cleanupData.push(cleanupRequestData);
111113

112114
const promiseActions: IPromiseActions<Server.IResponse> = {
@@ -130,12 +132,12 @@ export class HttpClient implements Server.IHttpClient {
130132

131133
this.$logger.trace("httpRequest: %s", util.inspect(options));
132134
const requestObj = request(options);
135+
cleanupRequestData.req = requestObj;
133136

134137
stuckRequestTimerId = setTimeout(() => {
135138
clearTimeout(stuckRequestTimerId);
136139
stuckRequestTimerId = null;
137140
if (!hasResponse) {
138-
requestObj.abort();
139141
this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(HttpClient.STUCK_REQUEST_ERROR_MESSAGE) });
140142
}
141143
}, options.timeout || HttpClient.STUCK_REQUEST_TIMEOUT);
@@ -156,14 +158,11 @@ export class HttpClient implements Server.IHttpClient {
156158
this.setResponseResult(promiseActions, cleanupRequestData, { err });
157159
})
158160
.on("response", (response: Server.IRequestResponseData) => {
161+
cleanupRequestData.res = response;
159162
hasResponse = true;
160163
let lastChunkTimestamp = Date.now();
161164
cleanupRequestData.stuckResponseIntervalId = setInterval(() => {
162165
if (Date.now() - lastChunkTimestamp > HttpClient.STUCK_RESPONSE_CHECK_INTERVAL) {
163-
if ((<any>response).destroy) {
164-
(<any>response).destroy();
165-
}
166-
167166
this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(HttpClient.STUCK_RESPONSE_ERROR_MESSAGE) });
168167
}
169168
}, HttpClient.STUCK_RESPONSE_CHECK_INTERVAL);
@@ -245,8 +244,8 @@ export class HttpClient implements Server.IHttpClient {
245244
this.cleanupAfterRequest(cleanupRequestData);
246245
if (!result.isResolved()) {
247246
result.isResolved = () => true;
248-
if (resultData.err) {
249-
return result.reject(resultData.err);
247+
if (resultData.err || !resultData.response.complete) {
248+
return result.reject(resultData.err || new Error("Request canceled"));
250249
}
251250

252251
const finalResult: any = resultData;
@@ -327,12 +326,23 @@ export class HttpClient implements Server.IHttpClient {
327326
clearInterval(data.stuckResponseIntervalId);
328327
data.stuckResponseIntervalId = null;
329328
}
329+
330+
if (data.req) {
331+
data.req.abort();
332+
}
333+
334+
if (data.res) {
335+
data.res.destroy();
336+
}
330337
}
338+
331339
}
332340

333341
interface ICleanupRequestData {
334342
timers: number[];
335343
stuckResponseIntervalId: NodeJS.Timer;
344+
req: request.Request;
345+
res: Server.IRequestResponseData;
336346
}
337347

338348
$injector.register("httpClient", HttpClient);

0 commit comments

Comments
 (0)