-
Notifications
You must be signed in to change notification settings - Fork 53
Limit multiple actions #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huksley
wants to merge
4
commits into
peterkhayes:master
Choose a base branch
from
huksley:multiple-actions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -47,11 +47,11 @@ export class RateLimiter { | |
| } | ||
|
|
||
| /** | ||
| * Attempts an action for the provided ID. Return information about whether the action was | ||
| * Attempts one or more actions for the provided ID. Return information about whether the action(s) were | ||
| * allowed and why, and whether upcoming actions will be allowed. | ||
| */ | ||
| async limitWithInfo(id: Id): Promise<RateLimitInfo> { | ||
| const timestamps = await this.getTimestamps(id, true); | ||
| async limitWithInfo(id: Id, count = 1): Promise<RateLimitInfo> { | ||
| const timestamps = await this.getTimestamps(id, count); | ||
| return this.calculateInfo(timestamps); | ||
| } | ||
|
|
||
|
|
@@ -60,15 +60,15 @@ export class RateLimiter { | |
| */ | ||
| async wouldLimitWithInfo(id: Id): Promise<RateLimitInfo> { | ||
| const currentTimestamp = getCurrentMicroseconds(); | ||
| const existingTimestamps = await this.getTimestamps(id, false); | ||
| const existingTimestamps = await this.getTimestamps(id, 0); | ||
| return this.calculateInfo([...existingTimestamps, currentTimestamp]); | ||
| } | ||
|
|
||
| /** | ||
| * Attempts an action for the provided ID. Returns whether it was blocked. | ||
| * Attempts one or more actions for the provided ID. Return whether any actions were blocked. | ||
| */ | ||
| async limit(id: Id): Promise<boolean> { | ||
| return (await this.limitWithInfo(id)).blocked; | ||
| async limit(id: Id, count = 1): Promise<boolean> { | ||
huksley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return (await this.limitWithInfo(id, count)).blocked; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -87,11 +87,11 @@ export class RateLimiter { | |
|
|
||
| /** | ||
| * Returns the list of timestamps of actions attempted within `interval` for the provided ID. If | ||
| * `addNewTimestamp` flag is set, adds a new action with the current microsecond timestamp. | ||
| * `addNewTimestamps` is set, adds a new actions with the current microsecond timestamp and more, incrementing by 1. | ||
| */ | ||
| protected async getTimestamps( | ||
| _id: Id, | ||
| _addNewTimestamp: boolean, | ||
| _addNewTimestamps: number, | ||
| ): Promise<Array<Microseconds>> { | ||
| return Promise.reject(new Error('Not implemented')); | ||
| } | ||
|
|
@@ -159,14 +159,17 @@ export class InMemoryRateLimiter extends RateLimiter { | |
| } | ||
| } | ||
|
|
||
| protected async getTimestamps(id: Id, addNewTimestamp: boolean) { | ||
| protected async getTimestamps( | ||
| id: Id, | ||
| addNewTimestamps: number, | ||
| ): Promise<Array<Microseconds>> { | ||
| const currentTimestamp = getCurrentMicroseconds(); | ||
| // Update the stored timestamps, including filtering out old ones, and adding the new one. | ||
| const clearBefore = currentTimestamp - this.interval; | ||
| const storedTimestamps = (this.storage[id] || []).filter((t) => t > clearBefore); | ||
|
|
||
| if (addNewTimestamp) { | ||
| storedTimestamps.push(currentTimestamp); | ||
| for (let i = 0; i < addNewTimestamps; i++) { | ||
| storedTimestamps.push(currentTimestamp + i); | ||
|
|
||
| // Set a new TTL, and cancel the old one, if present. | ||
| const ttl = this.ttls[id]; | ||
|
|
@@ -233,16 +236,16 @@ export class RedisRateLimiter extends RateLimiter { | |
|
|
||
| protected async getTimestamps( | ||
| id: Id, | ||
| addNewTimestamp: boolean, | ||
| addNewTimestamps: number, | ||
| ): Promise<Array<Microseconds>> { | ||
| const now = getCurrentMicroseconds(); | ||
| const key = this.makeKey(id); | ||
| const clearBefore = now - this.interval; | ||
|
|
||
| const batch = this.client.multi(); | ||
| batch.zremrangebyscore(key, 0, clearBefore); | ||
| if (addNewTimestamp) { | ||
| batch.zadd(key, String(now), uuid()); | ||
| for (let i = 0; i < addNewTimestamps; i++) { | ||
| batch.zadd(key, String(now + i), uuid()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch! |
||
| } | ||
| batch.zrange(key, 0, -1, 'WITHSCORES'); | ||
| batch.expire(key, this.ttl); | ||
|
|
@@ -251,7 +254,7 @@ export class RedisRateLimiter extends RateLimiter { | |
| batch.exec((err, result) => { | ||
| if (err) return reject(err); | ||
|
|
||
| const zRangeOutput = (addNewTimestamp ? result[2] : result[1]) as Array<unknown>; | ||
| const zRangeOutput = result[1 + addNewTimestamps] as Array<unknown>; | ||
| const zRangeResult = this.getZRangeResult(zRangeOutput); | ||
| const timestamps = this.extractTimestampsFromZRangeResult(zRangeResult); | ||
| return resolve(timestamps); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.