forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spec(aiscript): Mk:apiの呼び出しにエンドポイントごとのレートリミットを設定 (#522)
- Loading branch information
Showing
9 changed files
with
624 additions
and
540 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* SPDX-FileCopyrightText: Isaac Z. Schlueter and Contributors of https://github.com/isaacs/ttlcache | ||
* SPDX-License-Identifier: ISC | ||
* | ||
* This file is derived from the project that has licensed under the ISC license. | ||
* This file SHOULD NOT be considered as a part of the this project that has licensed under AGPL-3.0-only | ||
* Adapted from https://github.com/isaacs/ttlcache/blob/b6002f971e122e3b35e23d00ac6a8365d505c14d/examples/rate-limiter-window.ts | ||
*/ | ||
|
||
import TTLCache from '@isaacs/ttlcache'; | ||
import type { Options as TTLCacheOptions } from '@isaacs/ttlcache'; | ||
|
||
export interface Options { | ||
duration: number; | ||
max: number; | ||
} | ||
|
||
interface RLEntryOptions extends TTLCacheOptions<number, boolean> { | ||
onEmpty: () => void; | ||
} | ||
|
||
class RLEntry extends TTLCache<number, boolean> { | ||
onEmpty: () => void; | ||
|
||
constructor(options: RLEntryOptions) { | ||
super(options); | ||
this.onEmpty = options.onEmpty; | ||
} | ||
|
||
purgeStale() { | ||
const ret = super.purgeStale(); | ||
if (this.size === 0 && ret) { | ||
this.onEmpty(); | ||
} | ||
return ret; | ||
} | ||
} | ||
|
||
export class RateLimiter<K> extends Map<K, TTLCache<number, boolean>> { | ||
duration: number; | ||
max: number; | ||
|
||
constructor(options: Options) { | ||
super(); | ||
this.duration = options.duration; | ||
this.max = options.max; | ||
} | ||
|
||
hit(key: K) { | ||
const c = | ||
super.get(key) ?? | ||
new RLEntry({ | ||
ttl: this.duration, | ||
onEmpty: () => this.delete(key), | ||
}); | ||
|
||
this.set(key, c); | ||
|
||
if (c.size > this.max) { | ||
// rejected, too many hits within window | ||
return false; | ||
} | ||
c.set(performance.now(), true); | ||
return true; | ||
} | ||
|
||
count(key: K) { | ||
const c = super.get(key); | ||
return c ? c.size : 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.