* Tests rate limiting memory usage
()
| 345 | * Tests rate limiting memory usage |
| 346 | */ |
| 347 | async function testRateLimitMemory () { |
| 348 | console.log("🔥 Rate Limiting Memory Test"); |
| 349 | console.log("-".repeat(50)); |
| 350 | |
| 351 | const monitor = new MemoryMonitor(); |
| 352 | const server = tenso({ |
| 353 | silent: true, |
| 354 | logging: { enabled: false }, |
| 355 | rate: { |
| 356 | enabled: true, |
| 357 | limit: 100, |
| 358 | reset: 900 |
| 359 | } |
| 360 | }); |
| 361 | |
| 362 | monitor.snapshot("Initial state"); |
| 363 | |
| 364 | // Simulate many different clients |
| 365 | const clientCount = 10000; |
| 366 | for (let i = 0; i < clientCount; i++) { |
| 367 | const req = { |
| 368 | sessionID: `client-${i}`, |
| 369 | ip: `192.168.${Math.floor(i / 256)}.${i % 256}` |
| 370 | }; |
| 371 | |
| 372 | // Simulate rate limit check |
| 373 | server.rateLimit(req); |
| 374 | |
| 375 | // Cleanup every 1000 clients |
| 376 | if (i % 1000 === 0 && i > 0) { |
| 377 | monitor.forceGC(); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | monitor.snapshot(`After ${clientCount} rate limit checks`); |
| 382 | |
| 383 | // Simulate time passing and cleanup |
| 384 | const rateStore = server.rates; |
| 385 | const currentTime = Math.floor(Date.now() / 1000); |
| 386 | |
| 387 | // Mark many as expired |
| 388 | let expiredCount = 0; |
| 389 | for (const [key, value] of rateStore.entries()) { // eslint-disable-line no-unused-vars |
| 390 | if (expiredCount < clientCount / 2) { |
| 391 | value.reset = currentTime - 1; |
| 392 | expiredCount++; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // Cleanup expired entries |
| 397 | const toDelete = []; |
| 398 | for (const [key, value] of rateStore.entries()) { |
| 399 | if (currentTime >= value.reset) { |
| 400 | toDelete.push(key); |
| 401 | } |
| 402 | } |
| 403 | toDelete.forEach(key => rateStore.delete(key)); |
| 404 |