Skip to content

Instantly share code, notes, and snippets.

@kharandziuk
Last active October 24, 2018 19:41
Show Gist options
  • Save kharandziuk/9bbefb916de522777d3ca2595e256d40 to your computer and use it in GitHub Desktop.
Save kharandziuk/9bbefb916de522777d3ca2595e256d40 to your computer and use it in GitHub Desktop.
const log = require('debug')('app')
const request = require('superagent')
const H = require('highland')
function batchCreate(bodies) {
const calls = H(bodies)
.map(body => {
log(`start ${body.name}`)
return H(request
.post('localhost:3000/people')
.send(body)
.then(r => {
log(`finished ${body.name}`)
return r
})
.then(r => r.status)
)
})
.parallel(5)
.ratelimit(5, 1100)
.collect()
.toPromise(Promise)
return calls
}
batchCreate(Array.from(Array(20)).map((_, i) => ({name: i})))
.then(console.log)
superagent: Enable experimental feature http2
2018-10-24T19:28:07.051Z app start 0
2018-10-24T19:28:07.059Z app start 1
2018-10-24T19:28:07.061Z app start 2
2018-10-24T19:28:07.061Z app start 3
2018-10-24T19:28:07.062Z app start 4
(node:24164) ExperimentalWarning: The http2 module is an experimental API.
2018-10-24T19:28:07.288Z app finished 1
2018-10-24T19:28:07.587Z app finished 2
2018-10-24T19:28:08.547Z app finished 3
2018-10-24T19:28:08.636Z app finished 4
2018-10-24T19:28:08.982Z app finished 0
2018-10-24T19:28:08.982Z app start 5
2018-10-24T19:28:08.983Z app start 6
2018-10-24T19:28:08.984Z app start 7
2018-10-24T19:28:08.984Z app start 8
2018-10-24T19:28:08.985Z app start 9
2018-10-24T19:28:09.219Z app finished 7
2018-10-24T19:28:09.427Z app finished 6
2018-10-24T19:28:09.524Z app finished 8
2018-10-24T19:28:10.786Z app finished 9
2018-10-24T19:28:10.790Z app finished 5
2018-10-24T19:28:11.797Z app start 10
2018-10-24T19:28:11.798Z app start 11
2018-10-24T19:28:11.798Z app start 12
2018-10-24T19:28:11.799Z app start 13
2018-10-24T19:28:11.801Z app start 14
2018-10-24T19:28:12.299Z app finished 14
2018-10-24T19:28:12.396Z app finished 10
2018-10-24T19:28:13.107Z app finished 13
2018-10-24T19:28:13.136Z app finished 12
2018-10-24T19:28:13.309Z app finished 11
2018-10-24T19:28:13.401Z app start 15
2018-10-24T19:28:13.402Z app start 16
2018-10-24T19:28:13.403Z app start 17
2018-10-24T19:28:13.403Z app start 18
2018-10-24T19:28:13.404Z app start 19
2018-10-24T19:28:13.803Z app finished 16
2018-10-24T19:28:14.095Z app finished 15
2018-10-24T19:28:14.449Z app finished 18
2018-10-24T19:28:14.652Z app finished 17
2018-10-24T19:28:15.138Z app finished 19
[ 201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201,
201 ]
const debug = require('debug')('app')
const throttle = require("express-throttle")
const express = require('express')
const app = express()
const port = 3000
app.use(express.json())
app.post(
'/people',
throttle({ "burst": 5, "period": "1s" }),
(req, res) => {
debug(`call ${req.body.name}`)
setTimeout(
() => res.status(201).json({status: 'created'}),
Math.ceil(Math.random() * 2000)
)
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Example app listening on port 3000!
2018-10-24T19:28:07.085Z app call 0
2018-10-24T19:28:07.087Z app call 1
2018-10-24T19:28:07.087Z app call 3
2018-10-24T19:28:07.087Z app call 2
2018-10-24T19:28:07.087Z app call 4
2018-10-24T19:28:08.987Z app call 7
2018-10-24T19:28:08.988Z app call 6
2018-10-24T19:28:08.988Z app call 5
2018-10-24T19:28:08.988Z app call 8
2018-10-24T19:28:08.988Z app call 9
2018-10-24T19:28:11.803Z app call 13
2018-10-24T19:28:11.803Z app call 12
2018-10-24T19:28:11.804Z app call 11
2018-10-24T19:28:11.804Z app call 10
2018-10-24T19:28:11.804Z app call 14
2018-10-24T19:28:13.407Z app call 18
2018-10-24T19:28:13.407Z app call 17
2018-10-24T19:28:13.407Z app call 16
2018-10-24T19:28:13.407Z app call 15
2018-10-24T19:28:13.408Z app call 19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment