-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path02-1fps.js
More file actions
32 lines (24 loc) · 793 Bytes
/
02-1fps.js
File metadata and controls
32 lines (24 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const Rx = require('rx');
const requests_ = new Rx.Subject();
const started = +(new Date())
function sendHello(e) {
console.log('sending hello at', +(new Date()) - started);
e.res.writeHead(200, { 'Content-Type': 'text/plain' });
e.res.end('Hello World\n');
}
const interval = 1000
const rateLimit = require('./rate-limit')
const limited_ = rateLimit(
requests_.tap(e => console.log(`request to ${e.req.url} at`, +(new Date) - started))
, interval)
limited_.subscribe(sendHello)
// server
const http = require('http');
const hostname = '127.0.0.1';
const port = 1337;
var prevMs = +(new Date())
http.createServer((req, res) => {
requests_.onNext({ req: req, res: res });
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});