Code Explainers

Code explainers tagged #throttling

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
javascript
const progressBar = document.getElementById('reading-progress');
const article = document.querySelector('article');
 
function updateProgress() {

Building a scroll-driven reading progress bar

dom scroll-events throttling
Intermediate 9 steps
javascript
class ToastQueue {
  constructor(container, { duration = 4000, max = 3 } = {}) {
    this.container = container;
    this.duration = duration;

Building a rate-limited toast queue

queue dom manipulation throttling
Intermediate 8 steps
typescript
import { useCallback, useEffect, useRef, useState } from "react";
 
interface UseResendCooldownOptions {
  cooldownSeconds?: number;

A resend cooldown hook in React

custom-hooks timers state-management
Intermediate 7 steps
ruby
namespace :counter_cache do
  desc "Recalculate comments_count for posts after a backfill"
  task warm_post_comments: :environment do
    scope = Post.where(comments_count: nil).or(Post.where("comments_count < 0"))

Backfilling counter caches in a Rake task in Rails

counter-cache batching rake-tasks
Intermediate 6 steps
ruby
class Api::MessagesController < ApiController
  before_action :authenticate_api_key!
 
  rate_limit to: 100,

Layered API rate limiting in Rails

rate-limiting api-authentication throttling
Intermediate 9 steps
javascript
const IDLE_TIMEOUT = 15 * 60 * 1000;
const WARNING_BEFORE = 60 * 1000;
const ACTIVITY_EVENTS = ['mousemove', 'keydown', 'scroll', 'touchstart', 'click'];
 

How an idle-session monitor logs you out

timers throttling broadcastchannel
Intermediate 8 steps
ruby
class DatasetImportJob < ApplicationJob
  queue_as :default
 
  def perform(import)

Streaming import progress with a Rails job

background-jobs turbo-streams progress-tracking
Intermediate 9 steps
php
<?php
 
function fetchAll(array $urls, int $concurrency = 10, int $timeout = 15): array
{

Bounded-concurrency HTTP fetching with curl_multi

concurrency http curl
Advanced 8 steps
javascript
import { useRef, useCallback, useEffect, useState } from "react";
 
function useThrottle(callback, delay) {
  const lastRun = useRef(0);

Building a throttle hook in React

throttling custom-hooks refs
Intermediate 8 steps
php
final class TokenBucketLimiter
{
    private float $tokens;
    private float $lastRefill;

How a token bucket rate limiter works

rate-limiting token-bucket throttling
Intermediate 7 steps
python
from django.core.cache import cache
from rest_framework.throttling import SimpleRateThrottle
 
 

A login rate throttle in Django REST Framework

rate-limiting caching throttling
Intermediate 8 steps
typescript
function throttle<T extends (...args: any[]) => void>(
  fn: T,
  limit: number
): (...args: Parameters<T>) => void {

Building a trailing-edge throttle in TypeScript

throttling closures generics
Intermediate 7 steps
javascript
function debounce(fn, delay) {
  let timeoutId = null;
 
  function debounced(...args) {

Building a debounce function in JavaScript

closures timers higher-order-functions
Intermediate 6 steps