throttling

javascript
class TokenBucket {
  constructor(capacity, refillRatePerSec) {
    this.capacity = capacity;
    this.refillRate = refillRatePerSec;
    this.tokens = capacity;
    this.lastRefill = Date.now();

Token Bucket Rate Limiter as Express Middleware

express rate-limiting token-bucket
by codesnips 3 tabs
ruby
module Middleware
  class RateLimiter
    def initialize(app, redis:, limit:, window:)
      @app = app
      @limiter = SlidingWindowLimiter.new(redis: redis, limit: limit, window: window)
      @limit = limit

Sliding-Window API Rate Limiting with a Rack Middleware and Redis in Rails

rails rack redis
by codesnips 3 tabs
ruby
require "securerandom"
require "digest"

class SlidingWindowLimiter
  Result = Struct.new(:allowed, :count, :remaining)

Sliding-Window API Rate Limiting with Rack Middleware and Redis

ruby rack redis
by codesnips 4 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Throttled Last-Seen Tracking with Laravel Middleware and Redis Cache

laravel middleware redis
by codesnips 4 tabs
php
<?php

namespace App\Services\RateLimit;

use Illuminate\Support\Facades\Redis;

Rate-Limited HTTP Client Wrapper With Redis Token Bucket and a Queued Dispatcher

laravel redis rate-limiting
by codesnips 4 tabs
python
from django.core.cache import cache


class RateLimiter:
    def __init__(self, scope, limit, window_seconds):
        self.scope = scope

Rate-Limiting Django Password Reset Requests in a Form's clean() Method

django rate-limiting caching
by codesnips 3 tabs
ruby
class LastSeenTracker
  THROTTLE = 5.minutes
  PENDING_KEY = "pending:last_seen".freeze

  class << self
    def touch(user_id, at: Time.current)

Database “Last Seen” without Hot Row Updates

rails performance redis
by codesnips 3 tabs
go
package ratelimit

import (
	"sync"
	"time"
)

Token Bucket Rate-Limiting Middleware for a Go net/http API

go net-http middleware
by codesnips 4 tabs
lua
-- KEYS[1] = bucket key
-- ARGV: capacity, refillPerSec, now(ms), cost, ttl(sec)
local capacity      = tonumber(ARGV[1])
local refill        = tonumber(ARGV[2])
local now           = tonumber(ARGV[3])
local cost          = tonumber(ARGV[4])

Token-Bucket Rate Limiting Middleware for Express with Per-Route Config

express rate-limiting token-bucket
by codesnips 3 tabs
ruby
class CounterBuffer
  DELTA_HASH = "counter:deltas".freeze

  READ_RESET = <<~LUA.freeze
    local v = redis.call('HGET', KEYS[1], ARGV[1])
    if v then redis.call('HDEL', KEYS[1], ARGV[1]) end

Debounce Expensive Counter Cache Updates with a Throttled Redis Buffer in Rails

rails redis counter-cache
by codesnips 3 tabs
lua
-- KEYS[1] = bucket key
-- ARGV[1] = capacity, ARGV[2] = refill_per_sec, ARGV[3] = now (float seconds)
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])

Redis-Backed Token Bucket Rate Limiter as FastAPI Middleware

fastapi redis rate-limiting
by codesnips 4 tabs
php
<?php

declare(strict_types=1);

namespace App\Security;

PHP Login Rate Limiting with a Sliding-Window Throttle Middleware

php rate-limiting middleware
by codesnips 3 tabs