Skip to content

Commit

Permalink
HD-3340 drain stream when receiving an error on a PUT
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit-a committed Oct 12, 2023
1 parent 63c7254 commit 3fdb3ed
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/hdcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function _createRequest(req, log, callback) {
error.code = response.statusCode;
error.isExpected = true;
log.debug('got expected response code:', { statusCode: response.statusCode });
response.resume(); // Drain the response stream
return callback(error);
}
return callback(undefined, response);
Expand Down Expand Up @@ -267,14 +268,16 @@ class HDProxydClient {
put(stream, size, params, reqUids, callback) {
const log = this.createLogger(reqUids);
this._failover('POST', stream, size, '', 0, log, (err, response) => {
if (response) {
response.resume();
}
if (err || !response) {
return callback(err);
}
if (!response.headers['scal-key']) {
return callback(new HDProxydError('no key returned'));
}
const key = response.headers['scal-key'];
response.resume(); // drain the stream
response.on('end', () => {
return callback(undefined, key);
});
Expand Down
7 changes: 5 additions & 2 deletions src/hdcontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class HDProxydError extends Error {

type HDProxydCallback = (error?: HDProxydError, res?: http.IncomingMessage) => void;

type HDProxydClientPutCallback = (error?: HDProxydError, key?: string) => void;
type HDProxydClientPutCallback = (error?: HDProxydError, key?: string) => void;
type HDProxydClientGetCallback = (error?: HDProxydError, res?: Stream) => void;
type HDProxydClientDeleteCallback = (error?: HDProxydError) => void;

Expand All @@ -37,6 +37,7 @@ function _createRequest(req: http.RequestOptions, log: RequestLogger, callback:
error.isExpected = true;
log.debug('got expected response code:',
{ statusCode: response.statusCode });
response.resume(); // Drain the response stream
return callback(error);
}
return callback(undefined, response);
Expand Down Expand Up @@ -311,14 +312,16 @@ export class HDProxydClient {
public put(stream: Stream, size: number, params: any, reqUids: string, callback: HDProxydClientPutCallback): void {
const log = this.createLogger(reqUids);
this._failover('POST', stream, size, '', 0, log, (err?: HDProxydError, response?: http.IncomingMessage) => {
if (response) {
response.resume();
}
if (err || !response) {
return callback(err);
}
if (!response.headers['scal-key']) {
return callback(new HDProxydError('no key returned'));
}
const key = response.headers['scal-key'] as string;
response.resume(); // drain the stream
response.on('end', () => {
return callback(undefined, key);
});
Expand Down

0 comments on commit 3fdb3ed

Please sign in to comment.