-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
614 additions
and
11 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
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,65 @@ | ||
import {MapDef} from "../../util/map"; | ||
|
||
type RateTrackerOpts = { | ||
limit: number; | ||
timeoutMs: number; | ||
}; | ||
|
||
const BUCKET_SIZE_MS = 1000; | ||
|
||
/** | ||
* The generic rate tracker allows up to `limit` objects in a period of time. | ||
* This could apply to both request count or block count, for both requests and responses. | ||
*/ | ||
export class RateTracker { | ||
private requestsWithinWindow = 0; | ||
private limit: number; | ||
private timeoutMs: number; | ||
/** Key as time in second and value as object requested */ | ||
private requests: MapDef<number, number>; | ||
|
||
constructor(opts: RateTrackerOpts, requests = new MapDef<number, number>(() => 0)) { | ||
this.limit = opts.limit; | ||
this.timeoutMs = opts.timeoutMs; | ||
this.requests = requests; | ||
} | ||
|
||
requestObjects(objectCount: number): number { | ||
if (objectCount <= 0) throw Error("Invalid objectCount " + objectCount); | ||
this.prune(); | ||
if (this.requestsWithinWindow >= this.limit) { | ||
return 0; | ||
} | ||
|
||
this.requestsWithinWindow += objectCount; | ||
const key = Math.floor(Date.now() / BUCKET_SIZE_MS); | ||
const curObjectCount = this.requests.getOrDefault(key); | ||
this.requests.set(key, curObjectCount + objectCount); | ||
|
||
return objectCount; | ||
} | ||
|
||
getRequestedObjectsWithinWindow(): number { | ||
return this.requestsWithinWindow; | ||
} | ||
|
||
private prune(): void { | ||
const now = Date.now(); | ||
|
||
for (const [timeInSec, count] of this.requests.entries()) { | ||
// reclaim the quota for old requests | ||
if (now - timeInSec * BUCKET_SIZE_MS >= this.timeoutMs) { | ||
this.requestsWithinWindow -= count; | ||
this.requests.delete(timeInSec); | ||
} else { | ||
// Break after the first entry within the timeout window. | ||
// Since the entries are added in order, all the rest will be within the window. | ||
break; | ||
} | ||
} | ||
|
||
if (this.requestsWithinWindow < 0) { | ||
this.requestsWithinWindow = 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
Oops, something went wrong.