Skip to content

Commit

Permalink
fix(retr): pause read stream to avoid memory build up
Browse files Browse the repository at this point in the history
  • Loading branch information
voxsoftware authored and trs committed Jan 10, 2018
1 parent e87c36d commit 286c106
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/commands/registration/retr.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ module.exports = {
.tap(() => this.commandSocket.pause())
.then(() => when.try(this.fs.read.bind(this.fs), command.arg, {start: this.restByteCount}))
.then(stream => {
this.restByteCount = 0;
const destroyConnection = (connection, reject) => err => connection && connection.destroy(err) && reject(err);

const eventsPromise = when.promise((resolve, reject) => {
this.connector.socket.once('error', err => reject(err));

stream.on('data', data => this.connector.socket
&& this.connector.socket.write(data, this.transferType));
stream.once('error', err => reject(err));
stream.on('data', data => {
if (stream) stream.pause();
if (this.connector.socket) {
this.connector.socket.write(data, this.transferType, () => stream && stream.resume());
}
});
stream.once('end', () => resolve());
stream.once('error', destroyConnection(this.connector.socket, reject));

this.connector.socket.once('error', destroyConnection(stream, reject));
});

return this.reply(150).then(() => this.connector.socket.resume())
this.restByteCount = 0;

return this.reply(150).then(() => stream.resume() && this.connector.socket.resume())
.then(() => eventsPromise)
.finally(() => stream.destroy ? stream.destroy() : null);
.finally(() => stream.destroy && stream.destroy());
})
.then(() => this.reply(226))
.catch(when.TimeoutError, err => {
Expand Down

0 comments on commit 286c106

Please # to comment.