Skip to content

Commit b1e3d56

Browse files
committed
multipart: bump archan, add concurrency control
1 parent 2aebbd6 commit b1e3d56

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

multipart/app.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@ var app = module.exports = koa();
1010

1111
app.use(function *(){
1212
// create a go-like channel for control flow
13-
var ch = archan();
13+
var ch = archan({
14+
concurrency: 1 // save only 1 file at a time
15+
});
1416

1517
// parse the multipart body
1618
var parts = parse(this, {
17-
autoFields: true
19+
autoFields: true // saves the fields to parts.field(s)
1820
});
1921

2022
// create a temporary folder to store files
2123
var tmpdir = path.join(os.tmpdir(), Math.random().toString(36).slice(2));
2224

23-
// make the directory
25+
// make the temporary directory
2426
yield fs.mkdir.bind(null, tmpdir);
2527

2628
// yield each part as a stream
2729
var part;
2830
while (part = yield parts) {
31+
// wait if there are too many file descriptors opened
32+
yield* ch.drain();
2933
// save each part to a file,
3034
// but do it in a different channel
3135
// so we don't block this particular while loop.
@@ -34,7 +38,7 @@ app.use(function *(){
3438

3539
// return all the filenames as an array
3640
// after all the files have finished downloading
37-
this.body = yield* ch.end();
41+
this.body = yield* ch.flush();
3842
})
3943

4044
if (!module.parent) app.listen(3000);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"koa-session": "koajs/session",
1010
"co-busboy": "cojs/busboy",
1111
"save-to": "~1.0.0",
12-
"archan": "~0.3.0",
12+
"archan": "~0.4.0",
1313
"raw-body": "~1.1.1"
1414
},
1515
"devDependencies": {

0 commit comments

Comments
 (0)