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
// SPDX-License-Identifier: CC0-1.0 | |
// token bucket with shuffle-sharding for ip admissions | |
class ShardedTokenBucket { | |
#depth = 5 // buckets within buckets | |
#replenish = null // replenish interval | |
#allow = true // const | |
#deny = false // const | |
#marked = new Set() | |
#onesec = 1000 // rate at which to fill bucket capacity | |
#fps = 0 // fill rate per ratems |
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
// SPDX-License-Identifier: CC0-1.0 | |
// constant-time ring-buffer: https://news.ycombinator.com/item?id=14106577 | |
class ReqQueue { | |
constructor(cap, fillerFn, serverFn, cleanerFn) { | |
this.capacity = cap | |
// a ring buffer | |
this.rb = new Array(this.capacity) | |
this.rb.fill(null) | |
// fillIdx points to the current empty accept slot |