-
Notifications
You must be signed in to change notification settings - Fork 23
/
leaky_bucket.ts
154 lines (126 loc) · 5.29 KB
/
leaky_bucket.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Adapted from eventEmitter/leaky-bucket, licensed under:
*
* Copyright (C) 2015 Michael van der Weg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import * as FastMutex from 'fast-mutex'
import * as CONFIG from './bib_config'
import { RateLimitError } from './bib_lib'
export class CrossTabLeakyBucket {
// CONF: size of the slot where capacity tokens can be used in seconds, defaults to 60
slotSize: number = 60
// CONF: capacity, defaults to 60 tokens / minute
capacity: number = 60
// CONF: how many seconds it takes to refill one item
refillRate: number = 1
// CONF: how long can a item wait before it gets a timeout defaults to 300 seconds
maxWaitingTime: number = 300
// GLOBAL: timestamp of the last request
last: number = 0
// GLOBAL: indicates how long the next item has to wait until its executed, stored as reserved cost, not time
waitTime: number = 0
// GLOBAL: how many slots are left to fill
left: number = 0
// LOCAL: list of tasks to complete
queue: any[] = []
// LOCAL: timer which may be set to work the queue
timer: any = null
storage: any
storename: string = ''
constructor(capacity: number, interval: number = 60, maxWaitingTime: number = 300) {
this.capacity = capacity
this.slotSize = interval
this.maxWaitingTime = maxWaitingTime
this.refillRate = this.slotSize / this.capacity
this.left = this.capacity
this.queue = []
this.storename = CONFIG.POLICY_LIMITER_LOCALSTORAGE_KEY
this.storage = localStorage || window.localStorage
}
storage_load() {
const txt = this.storage.getItem(this.storename) || ''
try {
if (!txt) { return }
const data = JSON.parse(txt)
if (!data) { return }
this.last = data.last
this.waitTime = data.waitTime
this.left = data.left
} catch (e) {
console.log(e)
}
}
storage_save() {
const out = {last: this.last, waitTime: this.waitTime, left: this.left}
this.storage.setItem(this.storename, JSON.stringify(out))
}
throttle(promise: () => Promise<any>) {
const mutex = new FastMutex()
return new Promise(
(resolve, reject) => {
mutex.lock(this.storename)
.then(() => this.storage_load())
.then(() => this._throttle())
.then(() => this.storage_save())
.then(() => mutex.release(this.storename))
.then(() => resolve())
.catch((e) => reject(e))
}
).then(() => promise())
}
_throttle(fromQueue: boolean = false) {
let waitTime: number = 0
const now = Date.now()
this.left = Math.min(((now - this.last) / (1000 * this.refillRate)) + this.left, this.capacity)
this.last = now
if (this.left >= 1 && (!this.queue.length || fromQueue)) {
this.left -= 1
if (fromQueue) {
this.waitTime -= 1
}
return
} else {
waitTime = Math.max((this.waitTime * (1000 * this.refillRate) - (now - this.last)) / 1000, 0)
if (waitTime >= this.maxWaitingTime) {
console.log(
`Timeout exceeded, too many waiting requests! Would take ${waitTime} seconds to complete, ` +
`the max waiting time is ${this.maxWaitingTime}.`
)
throw new RateLimitError('Too many requests, please try again in a few seconds.')
} else {
this.waitTime += 1
if (fromQueue) {
this.queue.unshift(1)
} else {
this.queue.push(1)
}
}
}
if (this.queue.length && this.timer === null) {
this.timer = setTimeout(
() => {
this.timer = null
if (this.queue.length) {
this._throttle(true)
}
},
Math.max(Math.ceil((1 - this.left) * this.refillRate * 1000), 0)
)
}
}
}
export const api_bucket = new CrossTabLeakyBucket(CONFIG.POLICY_LIMITER_RATE, 1, CONFIG.POLICY_LIMITER_CAPACITY)