-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
token-pool.js
298 lines (258 loc) · 8.03 KB
/
token-pool.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict'
const crypto = require('crypto')
const PriorityQueue = require('priorityqueuejs')
// Compute a one-way hash of the input string.
function sanitizeToken(id) {
return crypto
.createHash('sha256')
.update(id, 'utf-8')
.digest('hex')
}
function getUtcEpochSeconds() {
return (Date.now() / 1000) >>> 0
}
// Encapsulate a rate-limited token, with a user-provided ID and user-provided data.
//
// Each token has a notion of the number of uses remaining until exhausted,
// and the next reset time, when it can be used again even if it's exhausted.
class Token {
constructor(id, data, usesRemaining, nextReset) {
// Use underscores to avoid conflict with property accessors.
Object.assign(this, {
_id: id,
_data: data,
_usesRemaining: usesRemaining,
_nextReset: nextReset,
_isValid: true,
_isFrozen: false,
})
}
get id() {
return this._id
}
get data() {
return this._data
}
get usesRemaining() {
return this._usesRemaining
}
get nextReset() {
return this._nextReset
}
get isValid() {
return this._isValid
}
get isFrozen() {
return this._isFrozen
}
get hasReset() {
return getUtcEpochSeconds() >= this.nextReset
}
get isExhausted() {
return this.usesRemaining <= 0 && !this.hasReset
}
// Update the uses remaining and next reset time for a token.
update(usesRemaining, nextReset) {
if (!Number.isInteger(usesRemaining)) {
throw Error('usesRemaining must be an integer')
}
if (!Number.isInteger(nextReset)) {
throw Error('nextReset must be an integer')
}
if (this._isFrozen) {
return
}
// Since the token pool will typically return the same token for many uses
// before moving on to another, `update()` may be called many times. Since
// the sequence of responses may be indeterminate, keep the "worst case"
// value for uses remaining.
if (
this._nextReset === this.constructor.nextResetNever ||
nextReset > this._nextReset
) {
this._nextReset = nextReset
this._usesRemaining = usesRemaining
} else if (nextReset === this._nextReset) {
this._usesRemaining = Math.min(this._usesRemaining, usesRemaining)
} else {
// Discard the new update; it's older than the values we have.
}
}
// Indicate that the token should no longer be used.
invalidate() {
this._isValid = false
}
// Freeze the uses remaining and next reset values. Helpful for keeping
// stable ordering for a valid priority queue.
freeze() {
this._isFrozen = true
}
// Unfreeze the uses remaining and next reset values.
unfreeze() {
this._isFrozen = false
}
getDebugInfo({ sanitize = true } = {}) {
const { id, data, usesRemaining, nextReset, isValid, isFrozen } = this
if (sanitize) {
return {
id: sanitizeToken(id),
data: '[redacted]',
usesRemaining,
nextReset,
isValid,
isFrozen,
}
} else {
return { id, data, usesRemaining, nextReset, isValid, isFrozen }
}
}
}
// Large sentinel value which means "never reset".
Token.nextResetNever = Number.MAX_SAFE_INTEGER
// Encapsulate a collection of rate-limited tokens and choose the next
// available token when one is needed.
//
// Designed for the Github API, though may be also useful with other rate-
// limited APIs.
class TokenPool {
// batchSize: The maximum number of times we use each token before moving
// on to the next one.
constructor({ batchSize = 1 } = {}) {
this.batchSize = batchSize
this.currentBatch = { currentToken: null, remaining: 0 }
// A set of IDs used for deduplication.
this.tokenIds = new Set()
// See discussion on the FIFO and priority queues in `next()`.
this.fifoQueue = []
this.priorityQueue = new PriorityQueue(this.constructor.compareTokens)
}
// Use the token whose current rate allotment is expiring soonest.
static compareTokens(first, second) {
return second.nextReset - first.nextReset
}
// Add a token with user-provided ID and data.
//
// The ID can be a primitive value or an object reference, and is used (with
// `Set`) for deduplication. If a token already exists with a given id, it
// will be ignored.
add(id, data, usesRemaining, nextReset) {
if (this.tokenIds.has(id)) {
return false
}
this.tokenIds.add(id)
usesRemaining = usesRemaining === undefined ? this.batchSize : usesRemaining
nextReset = nextReset === undefined ? Token.nextResetNever : nextReset
const token = new Token(id, data, usesRemaining, nextReset)
this.fifoQueue.push(token)
return true
}
// Prepare to start a new batch by obtaining and returning the next usable
// token.
_nextBatch() {
let next
while ((next = this.fifoQueue.shift())) {
if (!next.isValid) {
// Discard, and
continue
} else if (next.isExhausted) {
next.freeze()
this.priorityQueue.enq(next)
} else {
return next
}
}
while (
!this.priorityQueue.isEmpty() &&
(next = this.priorityQueue.peek())
) {
if (!next.isValid) {
// Discard, and
continue
} else if (next.isExhausted) {
// No need to check any more tokens, since they all reset after this
// one.
break
} else {
this.priorityQueue.deq() // deq next
next.unfreeze()
return next
}
}
throw Error('Token pool is exhausted')
}
// Obtain the next available token, returning `null` if no tokens are
// available.
//
// Tokens are initially pulled from a FIFO queue. The first token is used
// for a batch of requests, then returned to the queue to give those
// requests the opportunity to complete. The next token is used for the next
// batch of requests.
//
// This strategy allows a token to be used for concurrent requests, not just
// sequential request, and simplifies token recovery after errored and timed
// out requests.
//
// By the time the original token re-emerges, its requests should have long
// since completed. Even if a couple them are still running, they can
// reasonably be ignored. The uses remaining won't be 100% correct, but
// that's fine, because Shields uses only 75%
//
// The process continues until an exhausted token is pulled from the FIFO
// queue. At that time it's placed in the priority queue based on its
// scheduled reset time. To ensure the priority queue works as intended,
// the scheduled reset time is frozen then.
//
// After obtaining a token using `next()`, invoke `update()` on it to set a
// new use-remaining count and next-reset time. Invoke `invalidate()` to
// indicate it should not be reused.
next() {
let token = this.currentBatch.token
const remaining = this.currentBatch.remaining
if (remaining <= 0 || !token.isValid || token.isExhausted) {
token = this._nextBatch()
this.currentBatch = {
token,
remaining: token.hasReset
? this.batchSize
: Math.min(this.batchSize, token.usesRemaining),
}
this.fifoQueue.push(token)
}
this.currentBatch.remaining -= 1
return token
}
// Iterate over all valid tokens.
forEach(callback) {
function visit(item) {
if (item.isValid) {
callback(item)
}
}
this.fifoQueue.forEach(visit)
this.priorityQueue.forEach(visit)
}
allValidTokenIds() {
const result = []
this.forEach(({ id }) => result.push(id))
return result
}
serializeDebugInfo({ sanitize = true } = {}) {
const maybeSanitize = sanitize ? id => sanitizeToken(id) : id => id
const priorityQueue = []
this.priorityQueue.forEach(t =>
priorityQueue.push(t.getDebugInfo({ sanitize }))
)
return {
utcEpochSeconds: getUtcEpochSeconds(),
allValidTokenIds: this.allValidTokenIds().map(maybeSanitize),
fifoQueue: this.fifoQueue.map(t => t.getDebugInfo({ sanitize })),
priorityQueue,
sanitized: sanitize,
}
}
}
module.exports = {
sanitizeToken,
Token,
TokenPool,
}