Code Explainers

Code explainers tagged #rate-limiting

python
import secrets
 
from django.contrib.auth import authenticate, login
from django.core.cache import cache

Two-factor login with OTP in Django

two-factor-auth one-time-passwords caching
Intermediate 9 steps
go
package middleware
 
import (
	"net/http"

Per-plan export limits in Gin middleware

middleware rate-limiting authorization
Intermediate 7 steps
rust
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time::{interval, MissedTickBehavior};
 

A token-bucket rate limiter in Tokio

rate-limiting channels async
Advanced 8 steps
php
class TwoFactorController extends Controller
{
    public function show(Request $request): View|RedirectResponse
    {

Two-factor auth challenge flow in Laravel

authentication two-factor middleware
Intermediate 10 steps
python
import asyncio
import logging
from dataclasses import dataclass
 

A concurrent batch geocoder in asyncio

asyncio producer-consumer rate-limiting
Advanced 8 steps
java
public final class ThrottledLogger {
 
    private final Logger delegate;
    private final long windowMillis;

Rate-limiting log spam with per-message windows

concurrency rate-limiting throttling
Advanced 8 steps
python
import time
import logging
from threading import Lock
 

Rate-limiting noisy logs in Python

throttling thread-safety logging
Intermediate 6 steps
rust
use axum::{
    async_trait,
    extract::FromRequestParts,
    http::{request::Parts, StatusCode},

A proof-of-work extractor in Axum

proof-of-work extractors hashing
Advanced 9 steps
go
package retry
 
import (
	"errors"

How a retry budget caps retries in Go

rate-limiting sliding-window concurrency
Intermediate 7 steps
rust
use axum::{
    body::Body,
    extract::State,
    http::{Request, StatusCode},

A sliding-window rate limiter in Axum

rate-limiting middleware sliding-window
Intermediate 10 steps
php
<?php
 
namespace App\Services;
 

Throttling login attempts in Laravel

rate-limiting authentication security
Intermediate 6 steps
typescript
type Bucket = { start: number; count: number };
 
export class SlidingWindowCounter {
  private readonly windowMs: number;

A sliding window rate counter in TypeScript

sliding-window ring-buffer rate-limiting
Intermediate 7 steps
javascript
import { NextResponse } from 'next/server';
import { Redis } from '@upstash/redis';
 
export const runtime = 'edge';

A deduped page-view counter in Next.js

edge-runtime redis rate-limiting
Intermediate 8 steps
go
package logsample
 
import (
	"sync"

A per-key rate limiter for log sampling

rate-limiting concurrency time-windows
Intermediate 7 steps
go
package server
 
import (
	"net/http"

Rate limiting HTTP handlers with a token bucket

rate-limiting token-bucket middleware
Advanced 7 steps
typescript
import { Body, Controller, Ip, Post, UnauthorizedException } from '@nestjs/common';
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
import { UseGuards } from '@nestjs/common';
import { AuthService } from './auth.service';

Rate-limiting an auth flow in NestJS

rate-limiting authentication guards
Intermediate 8 steps
java
public class RequestThrottler {
 
    private final Semaphore permits;
    private final long acquireTimeoutMillis;

Bounding concurrency with a Semaphore in Java

concurrency semaphore rate-limiting
Intermediate 6 steps
python
import time
import threading
from flask import Flask, request, jsonify, g
 

A token-bucket rate limiter in Flask

rate-limiting token-bucket middleware
Intermediate 7 steps