implemented websocket flow control to prevent over buffering
This commit is contained in:
parent
015067648e
commit
531584dbf1
4 changed files with 207 additions and 191 deletions
|
@ -3,10 +3,11 @@ const storage = require('../storage');
|
|||
const config = require('../config');
|
||||
const mozlog = require('../log');
|
||||
const Limiter = require('../limiter');
|
||||
const Parser = require('../streamparser');
|
||||
const wsStream = require('websocket-stream/stream');
|
||||
const fxa = require('../fxa');
|
||||
|
||||
const { Duplex } = require('stream');
|
||||
|
||||
const log = mozlog('send.upload');
|
||||
|
||||
module.exports = function(ws, req) {
|
||||
|
@ -72,12 +73,27 @@ module.exports = function(ws, req) {
|
|||
id: newId
|
||||
})
|
||||
);
|
||||
|
||||
const limiter = new Limiter(maxFileSize);
|
||||
const parser = new Parser();
|
||||
const flowControl = new Duplex({
|
||||
read() {
|
||||
ws.resume();
|
||||
},
|
||||
write(chunk, encoding, callback) {
|
||||
if (chunk.length === 1 && chunk[0] === 0) {
|
||||
this.push(null);
|
||||
} else {
|
||||
if (!this.push(chunk)) {
|
||||
ws.pause();
|
||||
}
|
||||
}
|
||||
callback();
|
||||
}
|
||||
});
|
||||
|
||||
fileStream = wsStream(ws, { binary: true })
|
||||
.pipe(limiter)
|
||||
.pipe(parser);
|
||||
.pipe(flowControl);
|
||||
|
||||
await storage.set(newId, fileStream, meta, timeLimit);
|
||||
|
||||
if (ws.readyState === 1) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue