|
| 1 | +import { CodeError } from '@libp2p/interface' |
| 2 | +import delay from 'delay' |
| 3 | + |
| 4 | +export interface RateLimiterInit { |
| 5 | + /** |
| 6 | + * Number of points |
| 7 | + * |
| 8 | + * @default 4 |
| 9 | + */ |
| 10 | + points?: number |
| 11 | + |
| 12 | + /** |
| 13 | + * Per seconds |
| 14 | + * |
| 15 | + * @default 1 |
| 16 | + */ |
| 17 | + duration?: number |
| 18 | + |
| 19 | + /** |
| 20 | + * Block if consumed more than points in current duration for blockDuration seconds |
| 21 | + * |
| 22 | + * @default 0 |
| 23 | + */ |
| 24 | + blockDuration?: number |
| 25 | + |
| 26 | + /** |
| 27 | + * Execute allowed actions evenly over duration |
| 28 | + * |
| 29 | + * @default false |
| 30 | + */ |
| 31 | + execEvenly?: boolean |
| 32 | + |
| 33 | + /** |
| 34 | + * ms, works with execEvenly=true option |
| 35 | + * |
| 36 | + * @default duration * 1000 / points |
| 37 | + */ |
| 38 | + execEvenlyMinDelayMs?: number |
| 39 | + |
| 40 | + /** |
| 41 | + * @default rlflx |
| 42 | + */ |
| 43 | + keyPrefix?: string |
| 44 | +} |
| 45 | + |
| 46 | +export interface GetKeySecDurationOptions { |
| 47 | + customDuration?: number |
| 48 | +} |
| 49 | + |
| 50 | +export interface RateLimiterResult { |
| 51 | + remainingPoints: number |
| 52 | + msBeforeNext: number |
| 53 | + consumedPoints: number |
| 54 | + isFirstInDuration: boolean |
| 55 | +} |
| 56 | + |
| 57 | +export interface RateRecord { |
| 58 | + value: number |
| 59 | + expiresAt?: Date |
| 60 | + timeoutId?: ReturnType<typeof setTimeout> |
| 61 | +} |
| 62 | + |
| 63 | +export class RateLimiter { |
| 64 | + public readonly memoryStorage: MemoryStorage |
| 65 | + protected points: number |
| 66 | + protected duration: number |
| 67 | + protected blockDuration: number |
| 68 | + protected execEvenly: boolean |
| 69 | + protected execEvenlyMinDelayMs: number |
| 70 | + protected keyPrefix: string |
| 71 | + |
| 72 | + constructor (opts: RateLimiterInit = {}) { |
| 73 | + this.points = opts.points ?? 4 |
| 74 | + this.duration = opts.duration ?? 1 |
| 75 | + this.blockDuration = opts.blockDuration ?? 0 |
| 76 | + this.execEvenly = opts.execEvenly ?? false |
| 77 | + this.execEvenlyMinDelayMs = opts.execEvenlyMinDelayMs ?? (this.duration * 1000 / this.points) |
| 78 | + this.keyPrefix = opts.keyPrefix ?? 'rlflx' |
| 79 | + this.memoryStorage = new MemoryStorage() |
| 80 | + } |
| 81 | + |
| 82 | + async consume (key: string, pointsToConsume: number = 1, options: GetKeySecDurationOptions = {}): Promise<RateLimiterResult> { |
| 83 | + const rlKey = this.getKey(key) |
| 84 | + const secDuration = this._getKeySecDuration(options) |
| 85 | + let res = this.memoryStorage.incrby(rlKey, pointsToConsume, secDuration) |
| 86 | + res.remainingPoints = Math.max(this.points - res.consumedPoints, 0) |
| 87 | + |
| 88 | + if (res.consumedPoints > this.points) { |
| 89 | + // Block only first time when consumed more than points |
| 90 | + if (this.blockDuration > 0 && res.consumedPoints <= (this.points + pointsToConsume)) { |
| 91 | + // Block key |
| 92 | + res = this.memoryStorage.set(rlKey, res.consumedPoints, this.blockDuration) |
| 93 | + } |
| 94 | + |
| 95 | + throw new CodeError('Rate limit exceeded', 'ERR_RATE_LIMIT_EXCEEDED', res) |
| 96 | + } else if (this.execEvenly && res.msBeforeNext > 0 && !res.isFirstInDuration) { |
| 97 | + // Execute evenly |
| 98 | + let delayMs = Math.ceil(res.msBeforeNext / (res.remainingPoints + 2)) |
| 99 | + if (delayMs < this.execEvenlyMinDelayMs) { |
| 100 | + delayMs = res.consumedPoints * this.execEvenlyMinDelayMs |
| 101 | + } |
| 102 | + |
| 103 | + await delay(delayMs) |
| 104 | + } |
| 105 | + |
| 106 | + return res |
| 107 | + } |
| 108 | + |
| 109 | + penalty (key: string, points: number = 1, options: GetKeySecDurationOptions = {}): RateLimiterResult { |
| 110 | + const rlKey = this.getKey(key) |
| 111 | + const secDuration = this._getKeySecDuration(options) |
| 112 | + const res = this.memoryStorage.incrby(rlKey, points, secDuration) |
| 113 | + res.remainingPoints = Math.max(this.points - res.consumedPoints, 0) |
| 114 | + |
| 115 | + return res |
| 116 | + } |
| 117 | + |
| 118 | + reward (key: string, points: number = 1, options: GetKeySecDurationOptions = {}): RateLimiterResult { |
| 119 | + const rlKey = this.getKey(key) |
| 120 | + const secDuration = this._getKeySecDuration(options) |
| 121 | + const res = this.memoryStorage.incrby(rlKey, -points, secDuration) |
| 122 | + res.remainingPoints = Math.max(this.points - res.consumedPoints, 0) |
| 123 | + |
| 124 | + return res |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Block any key for secDuration seconds |
| 129 | + * |
| 130 | + * @param key |
| 131 | + * @param secDuration |
| 132 | + */ |
| 133 | + block (key: string, secDuration: number): RateLimiterResult { |
| 134 | + const msDuration = secDuration * 1000 |
| 135 | + const initPoints = this.points + 1 |
| 136 | + |
| 137 | + this.memoryStorage.set(this.getKey(key), initPoints, secDuration) |
| 138 | + |
| 139 | + return { |
| 140 | + remainingPoints: 0, |
| 141 | + msBeforeNext: msDuration === 0 ? -1 : msDuration, |
| 142 | + consumedPoints: initPoints, |
| 143 | + isFirstInDuration: false |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + set (key: string, points: number, secDuration: number = 0): RateLimiterResult { |
| 148 | + const msDuration = (secDuration >= 0 ? secDuration : this.duration) * 1000 |
| 149 | + |
| 150 | + this.memoryStorage.set(this.getKey(key), points, secDuration) |
| 151 | + |
| 152 | + return { |
| 153 | + remainingPoints: 0, |
| 154 | + msBeforeNext: msDuration === 0 ? -1 : msDuration, |
| 155 | + consumedPoints: points, |
| 156 | + isFirstInDuration: false |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + get (key: string): RateLimiterResult | undefined { |
| 161 | + const res = this.memoryStorage.get(this.getKey(key)) |
| 162 | + |
| 163 | + if (res != null) { |
| 164 | + res.remainingPoints = Math.max(this.points - res.consumedPoints, 0) |
| 165 | + } |
| 166 | + |
| 167 | + return res |
| 168 | + } |
| 169 | + |
| 170 | + delete (key: string): void { |
| 171 | + this.memoryStorage.delete(this.getKey(key)) |
| 172 | + } |
| 173 | + |
| 174 | + private _getKeySecDuration (options?: GetKeySecDurationOptions): number { |
| 175 | + if (options?.customDuration != null && options.customDuration >= 0) { |
| 176 | + return options.customDuration |
| 177 | + } |
| 178 | + |
| 179 | + return this.duration |
| 180 | + } |
| 181 | + |
| 182 | + getKey (key: string): string { |
| 183 | + return this.keyPrefix.length > 0 ? `${this.keyPrefix}:${key}` : key |
| 184 | + } |
| 185 | + |
| 186 | + parseKey (rlKey: string): string { |
| 187 | + return rlKey.substring(this.keyPrefix.length) |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +class MemoryStorage { |
| 192 | + public readonly storage: Map<string, RateRecord> |
| 193 | + |
| 194 | + constructor () { |
| 195 | + this.storage = new Map() |
| 196 | + } |
| 197 | + |
| 198 | + incrby (key: string, value: number, durationSec: number): RateLimiterResult { |
| 199 | + const existing = this.storage.get(key) |
| 200 | + |
| 201 | + if (existing != null) { |
| 202 | + const msBeforeExpires = existing.expiresAt != null |
| 203 | + ? existing.expiresAt.getTime() - new Date().getTime() |
| 204 | + : -1 |
| 205 | + |
| 206 | + if (existing.expiresAt == null || msBeforeExpires > 0) { |
| 207 | + // Change value |
| 208 | + existing.value += value |
| 209 | + |
| 210 | + return { |
| 211 | + remainingPoints: 0, |
| 212 | + msBeforeNext: msBeforeExpires, |
| 213 | + consumedPoints: existing.value, |
| 214 | + isFirstInDuration: false |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return this.set(key, value, durationSec) |
| 219 | + } |
| 220 | + |
| 221 | + return this.set(key, value, durationSec) |
| 222 | + } |
| 223 | + |
| 224 | + set (key: string, value: number, durationSec: number): RateLimiterResult { |
| 225 | + const durationMs = durationSec * 1000 |
| 226 | + const existing = this.storage.get(key) |
| 227 | + |
| 228 | + if (existing != null) { |
| 229 | + clearTimeout(existing.timeoutId) |
| 230 | + } |
| 231 | + |
| 232 | + const record: RateRecord = { |
| 233 | + value, |
| 234 | + expiresAt: durationMs > 0 ? new Date(Date.now() + durationMs) : undefined |
| 235 | + } |
| 236 | + |
| 237 | + this.storage.set(key, record) |
| 238 | + |
| 239 | + if (durationMs > 0) { |
| 240 | + record.timeoutId = setTimeout(() => { |
| 241 | + this.storage.delete(key) |
| 242 | + }, durationMs) |
| 243 | + |
| 244 | + if (record.timeoutId.unref != null) { |
| 245 | + record.timeoutId.unref() |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + return { |
| 250 | + remainingPoints: 0, |
| 251 | + msBeforeNext: durationMs === 0 ? -1 : durationMs, |
| 252 | + consumedPoints: record.value, |
| 253 | + isFirstInDuration: true |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + get (key: string): RateLimiterResult | undefined { |
| 258 | + const existing = this.storage.get(key) |
| 259 | + |
| 260 | + if (existing != null) { |
| 261 | + const msBeforeExpires = existing.expiresAt != null |
| 262 | + ? existing.expiresAt.getTime() - new Date().getTime() |
| 263 | + : -1 |
| 264 | + return { |
| 265 | + remainingPoints: 0, |
| 266 | + msBeforeNext: msBeforeExpires, |
| 267 | + consumedPoints: existing.value, |
| 268 | + isFirstInDuration: false |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + delete (key: string): boolean { |
| 274 | + const record = this.storage.get(key) |
| 275 | + |
| 276 | + if (record != null) { |
| 277 | + if (record.timeoutId != null) { |
| 278 | + clearTimeout(record.timeoutId) |
| 279 | + } |
| 280 | + |
| 281 | + this.storage.delete(key) |
| 282 | + |
| 283 | + return true |
| 284 | + } |
| 285 | + return false |
| 286 | + } |
| 287 | +} |
0 commit comments