Code Explainers

Code explainers tagged #concurrency

java
public class SlidingLogRateLimiter {
 
    private final int maxRequests;
    private final long windowMillis;

How a sliding-log rate limiter works

rate-limiting concurrency sliding-window
Advanced 8 steps
go
package theme
 
import (
	"fmt"

Per-tenant HTML templates with Gin's renderer

multi-tenancy concurrency html-templates
Advanced 8 steps
python
import time
import threading
from enum import Enum
from functools import wraps

Building a circuit breaker in Python

circuit-breaker resilience decorators
Advanced 7 steps
typescript
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http';
import { BehaviorSubject, Observable, throwError } from 'rxjs';
import { catchError, filter, take, switchMap } from 'rxjs/operators';

Refreshing auth tokens in an Angular interceptor

http-interceptor token-refresh rxjs
Advanced 8 steps
php
<?php
 
namespace App\Http\Middleware;
 

Idempotent requests with Laravel middleware

idempotency middleware caching
Advanced 8 steps
go
package middleware
 
import (
	"net/http"

A maintenance-mode gate in Gin

middleware atomic-flag concurrency
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

Optimistic locking on invoices in Laravel

optimistic-locking transactions concurrency
Advanced 6 steps
typescript
export class Semaphore {
  private available: number;
  private readonly waiters: Array<() => void> = [];
 

Building an async Semaphore in TypeScript

concurrency async-await promises
Advanced 6 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
rust
use axum::{
    async_trait,
    extract::{FromRequestParts, State},
    http::{request::Parts, StatusCode},

Idempotent payments with an Axum extractor

idempotency extractors shared-state
Intermediate 8 steps
go
package middleware
 
import (
	"bytes"

Building a response cache middleware in Gin

middleware caching concurrency
Intermediate 8 steps
java
@RestController
@RequestMapping("/api/notifications")
public class NotificationStreamController {
 

Server-Sent Events per user in Spring

server-sent-events concurrency event-driven
Advanced 8 steps
ruby
class InventoryItem < ApplicationRecord
  belongs_to :warehouse
 
  validates :quantity, numericality: { greater_than_or_equal_to: 0 }

Safe inventory updates in Rails

concurrency pessimistic-locking optimistic-locking
Advanced 7 steps
typescript
type Task<T> = () => Promise<T>;
 
export class Mutex {
  private queue: Array<() => void> = [];

Building an async mutex in TypeScript

concurrency promises locking
Advanced 7 steps
go
package middleware
 
import (
	"net/http"

Per-IP rate limiting middleware in Gin

rate-limiting token-bucket concurrency
Intermediate 7 steps
rust
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
use chrono::{Duration, Utc};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::Arc};

Refresh token rotation in Axum

authentication token-rotation reuse-detection
Advanced 9 steps
php
<?php
 
namespace App\Jobs;
 

Serializing balance reconciliation in Laravel

queues concurrency idempotency
Intermediate 7 steps
rust
use axum::{extract::{Path, State}, http::StatusCode, Json};
use serde::Serialize;
use std::sync::Arc;
 

Concurrent dashboard aggregation in Axum

concurrency error-handling graceful-degradation
Intermediate 7 steps