-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathrateLimitManager.ts
More file actions
379 lines (349 loc) · 11.8 KB
/
rateLimitManager.ts
File metadata and controls
379 lines (349 loc) · 11.8 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { RATE_LIMIT_EVENTS } from '@/events'
import { eventBus, SendTarget } from '@/eventbus'
import { IConfigPresenter, LLM_PROVIDER } from '@shared/presenter'
import {
ExecuteWithRateLimitOptions,
ProviderRateLimitState,
QueueItem,
RateLimitConfig,
RateLimitQueueSnapshot
} from '../types'
const createAbortError = (): Error => {
if (typeof DOMException !== 'undefined') {
return new DOMException('Aborted', 'AbortError')
}
const error = new Error('Aborted')
error.name = 'AbortError'
return error
}
export class RateLimitManager {
private readonly providerRateLimitStates: Map<string, ProviderRateLimitState> = new Map()
private readonly DEFAULT_RATE_LIMIT_CONFIG: RateLimitConfig = {
qpsLimit: 0.1,
enabled: false
}
constructor(private readonly configPresenter: IConfigPresenter) {}
initializeProviderRateLimitConfigs(): void {
const providers = this.configPresenter.getProviders()
for (const provider of providers) {
if (provider.rateLimit) {
this.setProviderRateLimitConfig(provider.id, {
enabled: provider.rateLimit.enabled,
qpsLimit: provider.rateLimit.qpsLimit
})
}
}
console.log(
`[RateLimitManager] Initialized rate limit configs for ${providers.length} providers`
)
}
updateProviderRateLimit(providerId: string, enabled: boolean, qpsLimit: number): void {
let finalConfig = { enabled, qpsLimit }
if (
finalConfig.qpsLimit !== undefined &&
(finalConfig.qpsLimit <= 0 || !isFinite(finalConfig.qpsLimit))
) {
if (finalConfig.enabled === true) {
console.warn(
`[RateLimitManager] Invalid qpsLimit (${finalConfig.qpsLimit}) for provider ${providerId}, disabling rate limit`
)
finalConfig.enabled = false
}
const provider = this.configPresenter.getProviderById(providerId)
finalConfig.qpsLimit = provider?.rateLimit?.qpsLimit ?? 0.1
}
this.setProviderRateLimitConfig(providerId, finalConfig)
const provider = this.configPresenter.getProviderById(providerId)
if (provider) {
const updatedProvider: LLM_PROVIDER = {
...provider,
rateLimit: {
enabled: finalConfig.enabled,
qpsLimit: finalConfig.qpsLimit
}
}
this.configPresenter.setProviderById(providerId, updatedProvider)
console.log(`[RateLimitManager] Updated persistent config for ${providerId}`)
}
}
getProviderRateLimitStatus(providerId: string): {
config: { enabled: boolean; qpsLimit: number }
currentQps: number
queueLength: number
lastRequestTime: number
} {
const config = this.getProviderRateLimitConfig(providerId)
const currentQps = this.getCurrentQps(providerId)
const queueLength = this.getQueueLength(providerId)
const lastRequestTime = this.getLastRequestTime(providerId)
return {
config,
currentQps,
queueLength,
lastRequestTime
}
}
getAllProviderRateLimitStatus(): Record<
string,
{
config: { enabled: boolean; qpsLimit: number }
currentQps: number
queueLength: number
lastRequestTime: number
}
> {
const status: Record<string, any> = {}
for (const [providerId, state] of this.providerRateLimitStates) {
status[providerId] = {
config: state.config,
currentQps: this.getCurrentQps(providerId),
queueLength: state.queue.length,
lastRequestTime: state.lastRequestTime
}
}
return status
}
async executeWithRateLimit(
providerId: string,
options?: ExecuteWithRateLimitOptions
): Promise<void> {
const state = this.getOrCreateRateLimitState(providerId)
if (options?.signal?.aborted) {
throw createAbortError()
}
if (!state.config.enabled) {
this.recordRequest(providerId)
return Promise.resolve()
}
if (this.canExecuteImmediately(providerId)) {
this.recordRequest(providerId)
return Promise.resolve()
}
return new Promise<void>((resolve, reject) => {
let settled = false
let abortCleanup: (() => void) | null = null
const settle = (callback: () => void) => {
if (settled) {
return
}
settled = true
abortCleanup?.()
abortCleanup = null
callback()
}
const queueItem: QueueItem = {
id: `${providerId}-${Date.now()}-${Math.random()}`,
timestamp: Date.now(),
resolve: () => settle(resolve),
reject: (error) => settle(() => reject(error))
}
state.queue.push(queueItem)
const snapshot = this.buildQueueSnapshot(providerId, state)
console.log(
`[RateLimitManager] Request queued for ${providerId}, queue length: ${state.queue.length}`
)
eventBus.send(RATE_LIMIT_EVENTS.REQUEST_QUEUED, SendTarget.ALL_WINDOWS, {
providerId,
queueLength: state.queue.length,
requestId: queueItem.id
})
try {
options?.onQueued?.(snapshot)
} catch (error) {
console.warn(`[RateLimitManager] onQueued callback failed for ${providerId}:`, error)
}
const signal = options?.signal
if (signal) {
const onAbort = () => {
const removed = this.removeQueueItem(providerId, queueItem.id)
if (removed) {
console.log(`[RateLimitManager] Request aborted while queued for ${providerId}`)
}
queueItem.reject(createAbortError())
}
signal.addEventListener('abort', onAbort, { once: true })
abortCleanup = () => signal.removeEventListener('abort', onAbort)
if (signal.aborted) {
onAbort()
return
}
}
this.processRateLimitQueue(providerId)
})
}
syncProviders(providers: LLM_PROVIDER[]): void {
for (const provider of providers) {
if (provider.rateLimit) {
this.setProviderRateLimitConfig(provider.id, {
enabled: provider.rateLimit.enabled,
qpsLimit: provider.rateLimit.qpsLimit
})
}
}
const currentProviderIds = new Set(providers.map((p) => p.id))
const allStatus = this.getAllProviderRateLimitStatus()
for (const providerId of Object.keys(allStatus)) {
if (!currentProviderIds.has(providerId)) {
this.cleanupProviderRateLimit(providerId)
}
}
}
cleanupProviderRateLimit(providerId: string): void {
const state = this.providerRateLimitStates.get(providerId)
if (state) {
while (state.queue.length > 0) {
const queueItem = state.queue.shift()
if (queueItem) {
queueItem.reject(new Error('Provider removed'))
}
}
this.providerRateLimitStates.delete(providerId)
console.log(`[RateLimitManager] Cleaned up rate limit state for ${providerId}`)
}
}
private setProviderRateLimitConfig(providerId: string, config: Partial<RateLimitConfig>): void {
const currentState = this.providerRateLimitStates.get(providerId)
const newConfig = {
...this.DEFAULT_RATE_LIMIT_CONFIG,
...currentState?.config,
...config
}
if (!currentState) {
this.providerRateLimitStates.set(providerId, {
config: newConfig,
queue: [],
lastRequestTime: 0,
isProcessing: false
})
} else {
currentState.config = newConfig
}
console.log(`[RateLimitManager] Updated rate limit config for ${providerId}:`, newConfig)
eventBus.send(RATE_LIMIT_EVENTS.CONFIG_UPDATED, SendTarget.ALL_WINDOWS, {
providerId,
config: newConfig
})
}
getProviderRateLimitConfig(providerId: string): RateLimitConfig {
const state = this.providerRateLimitStates.get(providerId)
return state?.config || this.DEFAULT_RATE_LIMIT_CONFIG
}
canExecuteImmediately(providerId: string): boolean {
const state = this.providerRateLimitStates.get(providerId)
if (!state || !state.config.enabled) {
return true
}
const now = Date.now()
const intervalMs = (1 / state.config.qpsLimit) * 1000
return now - state.lastRequestTime >= intervalMs
}
private recordRequest(providerId: string): void {
const state = this.getOrCreateRateLimitState(providerId)
const now = Date.now()
state.lastRequestTime = now
eventBus.send(RATE_LIMIT_EVENTS.REQUEST_EXECUTED, SendTarget.ALL_WINDOWS, {
providerId,
timestamp: now,
currentQps: this.getCurrentQps(providerId)
})
}
private async processRateLimitQueue(providerId: string): Promise<void> {
const state = this.providerRateLimitStates.get(providerId)
if (!state || state.isProcessing || state.queue.length === 0) {
return
}
state.isProcessing = true
try {
while (state.queue.length > 0) {
if (this.canExecuteImmediately(providerId)) {
const queueItem = state.queue.shift()
if (queueItem) {
this.recordRequest(providerId)
queueItem.resolve()
console.log(
`[RateLimitManager] Request executed for ${providerId}, remaining queue: ${state.queue.length}`
)
}
} else {
const now = Date.now()
const intervalMs = (1 / state.config.qpsLimit) * 1000
const nextAllowedTime = state.lastRequestTime + intervalMs
const waitTime = Math.max(0, nextAllowedTime - now)
if (waitTime > 0) {
await new Promise((resolve) => setTimeout(resolve, waitTime))
}
}
}
} catch (error) {
console.error(
`[RateLimitManager] Error processing rate limit queue for ${providerId}:`,
error
)
while (state.queue.length > 0) {
const queueItem = state.queue.shift()
if (queueItem) {
queueItem.reject(new Error('Rate limit processing failed'))
}
}
} finally {
state.isProcessing = false
}
}
private getOrCreateRateLimitState(providerId: string): ProviderRateLimitState {
let state = this.providerRateLimitStates.get(providerId)
if (!state) {
state = {
config: { ...this.DEFAULT_RATE_LIMIT_CONFIG },
queue: [],
lastRequestTime: 0,
isProcessing: false
}
this.providerRateLimitStates.set(providerId, state)
}
return state
}
getCurrentQps(providerId: string): number {
const state = this.providerRateLimitStates.get(providerId)
if (!state || !state.config.enabled || state.lastRequestTime === 0) return 0
const now = Date.now()
const timeSinceLastRequest = now - state.lastRequestTime
const intervalMs = (1 / state.config.qpsLimit) * 1000
return timeSinceLastRequest < intervalMs ? 1 : 0
}
getQueueLength(providerId: string): number {
const state = this.providerRateLimitStates.get(providerId)
return state?.queue.length || 0
}
private removeQueueItem(providerId: string, queueItemId: string): boolean {
const state = this.providerRateLimitStates.get(providerId)
if (!state) {
return false
}
const index = state.queue.findIndex((item) => item.id === queueItemId)
if (index === -1) {
return false
}
state.queue.splice(index, 1)
return true
}
private buildQueueSnapshot(
providerId: string,
state: ProviderRateLimitState
): RateLimitQueueSnapshot {
const intervalMs = (1 / state.config.qpsLimit) * 1000
const nextAllowedTime = state.lastRequestTime + intervalMs
const baseWaitTime = Math.max(0, nextAllowedTime - Date.now())
const additionalQueuedIntervals = Math.max(0, state.queue.length - 1) * intervalMs
return {
providerId,
qpsLimit: state.config.qpsLimit,
currentQps: this.getCurrentQps(providerId),
queueLength: state.queue.length,
estimatedWaitTime: Math.max(0, baseWaitTime + additionalQueuedIntervals)
}
}
private getLastRequestTime(providerId: string): number {
const state = this.providerRateLimitStates.get(providerId)
return state?.lastRequestTime || 0
}
}