Skip to content

Instantly share code, notes, and snippets.

@ignoramous
ignoramous / tokenbucket.js
Last active November 1, 2021 14:50
sharded token bucket for admission control
// 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
@ignoramous
ignoramous / lrb.js
Last active October 31, 2021 18:49
adaptive lifo ring buffer
// 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