Code Explainers

Code explainers tagged #concurrency

java
public final class TokenBucketRateLimiter {
 
    private final long capacity;
    private final double refillTokensPerNano;

How a token bucket rate limiter works

rate-limiting concurrency token-bucket
Intermediate 8 steps
python
from decimal import Decimal
 
from django.db import transaction
from django.core.exceptions import ValidationError

Placing an order atomically in Django

transactions row-locking concurrency
Advanced 9 steps
go
package fetcher
 
import (
	"context"

Bounded-concurrency HTTP fetching in Go

concurrency semaphore context-cancellation
Advanced 9 steps
php
<?php
 
namespace App\Services;
 

Placing an order atomically in Laravel

transactions pessimistic-locking concurrency
Advanced 8 steps
java
@Aspect
@Component
public class IdempotencyAspect {
 

How an idempotency aspect works in Spring

aop idempotency caching
Advanced 9 steps
typescript
import { Injectable } from '@angular/core';
import {
  HttpInterceptor,
  HttpHandler,

Refreshing tokens with an Angular HttpInterceptor

http-interceptor token-refresh rxjs
Advanced 8 steps
go
package ratelimit
 
import (
	"context"

A token bucket rate limiter in Go

rate-limiting concurrency goroutines
Intermediate 7 steps
typescript
type Fetcher<T> = (key: string) => Promise<T>;
 
export class RequestDeduplicator<T> {
  private inFlight = new Map<string, Promise<T>>();

Deduplicating in-flight requests in TypeScript

deduplication promises caching
Intermediate 7 steps
go
package cache
 
import (
	"context"

Cache-aside reads with singleflight in Go

caching singleflight concurrency
Advanced 8 steps
ruby
require 'monitor'
 
class TokenBucket
  include MonitorMixin

A thread-safe token bucket rate limiter in Ruby

rate-limiting concurrency thread-safety
Intermediate 8 steps
go
package scheduler
 
import (
	"context"

A context-aware heartbeat ticker in Go

concurrency channels context
Intermediate 5 steps
go
package pipeline
 
import "sync"
 

The fan-in pattern in Go channels

concurrency channels fan-in
Advanced 8 steps
python
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
 

Parallel file downloads with a thread pool

concurrency thread-pool streaming-io
Intermediate 8 steps
java
public class EventBus {
 
    private final Map<Class<?>, List<Subscriber>> subscribers = new ConcurrentHashMap<>();
    private final Executor executor;

Building an annotation-driven EventBus in Java

publish-subscribe reflection annotations
Intermediate 7 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
java
@Component
public class RateLimitingFilter extends OncePerRequestFilter {
 
    private static final int MAX_REQUESTS = 100;

How a rate-limiting filter works in Spring

rate limiting concurrency servlet filter
Advanced 8 steps
java
public final class RetryExecutor {
 
    private final int maxAttempts;
    private final Duration initialDelay;

Exponential backoff retry in Java

retry exponential-backoff jitter
Intermediate 8 steps
java
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
 

A thread-safe LRU cache in Java

lru-cache concurrency linkedhashmap
Intermediate 7 steps