|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import { RateLimitKey, RateLimitStore } from '../../types'; |
| 4 | + |
| 5 | +interface DatabaseEntry { |
| 6 | + reset: number; |
| 7 | + methodInfo: any; |
| 8 | +} |
| 9 | + |
| 10 | +interface MethodDatabase { |
| 11 | + methodName: string; |
| 12 | + remaining: number; |
| 13 | + total: number; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * LRU-based in-memory rate limit store. |
| 18 | + * Tracks request counts per IP and method within a time window. |
| 19 | + */ |
| 20 | +export class LruRateLimitStore implements RateLimitStore { |
| 21 | + private database: any; |
| 22 | + private duration: number; |
| 23 | + |
| 24 | + /** |
| 25 | + * Initializes the store with the specified duration window. |
| 26 | + * @param duration - Time window in milliseconds for rate limiting. |
| 27 | + */ |
| 28 | + constructor(duration: number) { |
| 29 | + this.database = Object.create(null); |
| 30 | + this.duration = duration; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Increments the request count for a given IP and method, checking if the limit is exceeded. |
| 35 | + * @param key - The rate limit key containing IP and method information. |
| 36 | + * @param limit - Maximum allowed requests in the current window. |
| 37 | + * @returns True if rate limit exceeded, false otherwise. |
| 38 | + */ |
| 39 | + async incrementAndCheck(key: RateLimitKey, limit: number): Promise<boolean> { |
| 40 | + const { ip, method } = key; |
| 41 | + |
| 42 | + this.precheck(ip, method, limit); |
| 43 | + if (!this.shouldReset(ip)) { |
| 44 | + if (this.checkRemaining(ip, method)) { |
| 45 | + this.decreaseRemaining(ip, method); |
| 46 | + return false; |
| 47 | + } |
| 48 | + return true; |
| 49 | + } else { |
| 50 | + this.reset(ip, method, limit); |
| 51 | + this.decreaseRemaining(ip, method); |
| 52 | + return false; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Ensures the IP and method are initialized in the database. |
| 58 | + * @param ip - The IP address to check. |
| 59 | + * @param methodName - The method name to check. |
| 60 | + * @param total - The total number of allowed requests. |
| 61 | + */ |
| 62 | + private precheck(ip: string, methodName: string, total: number): void { |
| 63 | + if (!this.checkIpExist(ip)) { |
| 64 | + this.setNewIp(ip); |
| 65 | + } |
| 66 | + |
| 67 | + if (!this.checkMethodExist(ip, methodName)) { |
| 68 | + this.setNewMethod(ip, methodName, total); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Initializes a new IP entry in the database. |
| 74 | + * @param ip - The IP address to initialize. |
| 75 | + */ |
| 76 | + private setNewIp(ip: string): void { |
| 77 | + const entry: DatabaseEntry = { |
| 78 | + reset: Date.now() + this.duration, |
| 79 | + methodInfo: {}, |
| 80 | + }; |
| 81 | + this.database[ip] = entry; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Initializes a new method entry for a given IP in the database. |
| 86 | + * @param ip - The IP address associated with the method. |
| 87 | + * @param methodName - The method name to initialize. |
| 88 | + * @param total - The total number of allowed requests. |
| 89 | + */ |
| 90 | + private setNewMethod(ip: string, methodName: string, total: number): void { |
| 91 | + const entry: MethodDatabase = { |
| 92 | + methodName: methodName, |
| 93 | + remaining: total, |
| 94 | + total: total, |
| 95 | + }; |
| 96 | + this.database[ip].methodInfo[methodName] = entry; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Checks if an IP exists in the database. |
| 101 | + * @param ip - The IP address to check. |
| 102 | + * @returns True if the IP exists, false otherwise. |
| 103 | + */ |
| 104 | + private checkIpExist(ip: string): boolean { |
| 105 | + return this.database[ip] !== undefined; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Checks if a method exists for a given IP in the database. |
| 110 | + * @param ip - The IP address associated with the method. |
| 111 | + * @param method - The method name to check. |
| 112 | + * @returns True if the method exists, false otherwise. |
| 113 | + */ |
| 114 | + private checkMethodExist(ip: string, method: string): boolean { |
| 115 | + return this.database[ip].methodInfo[method] !== undefined; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Checks if there are remaining requests for a given IP and method. |
| 120 | + * @param ip - The IP address associated with the method. |
| 121 | + * @param methodName - The method name to check. |
| 122 | + * @returns True if there are remaining requests, false otherwise. |
| 123 | + */ |
| 124 | + private checkRemaining(ip: string, methodName: string): boolean { |
| 125 | + return this.database[ip].methodInfo[methodName].remaining > 0; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Determines if the rate limit should be reset for a given IP. |
| 130 | + * @param ip - The IP address to check. |
| 131 | + * @returns True if the rate limit should be reset, false otherwise. |
| 132 | + */ |
| 133 | + private shouldReset(ip: string): boolean { |
| 134 | + return this.database[ip].reset < Date.now(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Resets the rate limit for a given IP and method. |
| 139 | + * @param ip - The IP address associated with the method. |
| 140 | + * @param methodName - The method name to reset. |
| 141 | + * @param total - The total number of allowed requests. |
| 142 | + */ |
| 143 | + private reset(ip: string, methodName: string, total: number): void { |
| 144 | + this.database[ip].reset = Date.now() + this.duration; |
| 145 | + for (const [keyMethod] of Object.entries(this.database[ip].methodInfo)) { |
| 146 | + this.database[ip].methodInfo[keyMethod].remaining = this.database[ip].methodInfo[keyMethod].total; |
| 147 | + } |
| 148 | + // Ensure the current method being checked is reset with the potentially new total (limit) |
| 149 | + this.database[ip].methodInfo[methodName].remaining = total; |
| 150 | + this.database[ip].methodInfo[methodName].total = total; // also update total if it changed |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Decreases the remaining request count for a given IP and method. |
| 155 | + * @param ip - The IP address associated with the method. |
| 156 | + * @param methodName - The method name to decrease the count for. |
| 157 | + */ |
| 158 | + private decreaseRemaining(ip: string, methodName: string): void { |
| 159 | + const currentRemaining = this.database[ip].methodInfo[methodName].remaining; |
| 160 | + this.database[ip].methodInfo[methodName].remaining = currentRemaining > 0 ? currentRemaining - 1 : 0; |
| 161 | + } |
| 162 | +} |
0 commit comments