-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrate-limit.js
84 lines (74 loc) · 1.89 KB
/
rate-limit.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const Rx = require('rx')
const debug = require('debug')('rate')
function rateLimit(stream_, delayMs) {
const out_ = new Rx.Subject()
var nextTimestamp = +(new Date())
var hasEvents = 0;
const started = nextTimestamp
function onEvent(e) {
var now = +(new Date())
debug('now', now - started, 'next', nextTimestamp - started)
if (now > nextTimestamp) {
nextTimestamp = now + delayMs
debug('now %d set next timestamp at',
now - started, nextTimestamp - started)
out_.onNext(e)
return
}
// delay the response
const sleepMs = nextTimestamp - now;
debug('need to sleep for %d ms at', sleepMs, now - started)
nextTimestamp += delayMs
hasEvents += 1
setTimeout(function () {
debug('sending', e, 'at', +(new Date()) - started)
out_.onNext(e)
hasEvents -= 1
if (!hasEvents) {
out_.onCompleted()
}
}, sleepMs)
}
stream_.subscribe(
onEvent,
out_.onError.bind(out_)
)
return out_
}
module.exports = rateLimit
if (!module.parent) {
const in_ = Rx.Observable
.interval(200)
.take(5)
// without rate limit
// in_
// .timeInterval()
// .subscribe(
// console.log.bind(console),
// console.error.bind(console),
// console.log.bind(console, 'completed')
// )
// output
// { value: 0, interval: 203 }
// { value: 1, interval: 232 }
// { value: 2, interval: 207 }
// { value: 3, interval: 206 }
// { value: 4, interval: 202 }
// completed
// with rate limit
const limited_ = rateLimit(in_, 1000)
limited_
.timeInterval()
.subscribe(
console.log,
console.error,
console.log.bind(console, 'limited completed')
)
// output
// { value: 0, interval: 203 }
// { value: 1, interval: 1005 }
// { value: 2, interval: 1001 }
// { value: 3, interval: 996 }
// { value: 4, interval: 999 }
// limited completed
}