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 c952990 commit c66f4b1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/hdcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 @@ -61,7 +62,7 @@ class HDProxydClient {
const options = opts || {};
this.bootstrap = opts.bootstrap === undefined ?
[['localhost', '18888']] : _parseBootstrapList(opts.bootstrap);
this.bootstrap = shuffle_1.shuffle(this.bootstrap);
this.bootstrap = (0, shuffle_1.shuffle)(this.bootstrap);
this.path = '/store/';
this.setCurrentBootstrap(this.bootstrap[0]);
this.httpAgent = new http.Agent({ keepAlive: true });
Expand Down Expand Up @@ -253,14 +254,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 @@ -16,7 +16,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 @@ -34,6 +34,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 @@ -298,14 +299,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 c66f4b1

Please sign in to comment.