concurrency

python
import time
import threading
from collections import deque, defaultdict


class SlidingWindowLimiter:

Sliding-Window Rate Limiting in Flask With a Custom Decorator and In-Memory Buckets

flask rate-limiting decorators
by codesnips 3 tabs
ruby
require "sidekiq/api"

class QueueDepthGuard
  class QueueSaturated < StandardError
    attr_reader :queue, :depth

Background Job Backpressure with Queue Depth Guard

rails reliability sidekiq
by codesnips 3 tabs
java
package com.shop.inventory;

import jakarta.persistence.*;

@Entity
@Table(name = "inventory_item")

Optimistic Locking on Inventory Updates with @Version and a Retrying Conflict Handler in Spring Boot

spring-boot jpa hibernate
by codesnips 3 tabs
ruby
# Basic Ractor usage
ractor = Ractor.new do
  # This runs in parallel, isolated from main thread
  result = heavy_computation
  result
end

Concurrent Ruby with Ractors and Async

ruby concurrency parallelism
by Sarah Mitchell 2 tabs
java
package com.example.orders;

public final class Order {

    public static final Order POISON_PILL = new Order(-1L, 0.0);

Producer/Consumer Order Processing With a Bounded BlockingQueue in Java

java concurrency blockingqueue
by codesnips 3 tabs
go
package main

import (
  "context"
  "log"
  "net/http"

Graceful shutdown: draining HTTP + background workers

go http shutdown
by Leah Thompson 1 tab
lua
-- KEYS[1] = current window key, KEYS[2] = previous window key
-- ARGV: limit, window_ms, elapsed_ms
local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local elapsed = tonumber(ARGV[3])

Sliding-Window Rate Limiting for Express Routes Backed by Redis

express redis rate-limiting
by codesnips 4 tabs
go
package service

import (
  "context"

  "golang.org/x/sync/errgroup"

Fan-out with errgroup and shared cancellation

go concurrency context
by Leah Thompson 1 tab
python
import asyncio
import time
from dataclasses import dataclass, field


@dataclass

Per-Client Rate Limiting in FastAPI with a Token Bucket Dependency

python fastapi rate-limiting
by codesnips 3 tabs
rust
use std::os::raw::c_int;

pub const LOCK_EX: c_int = 2;
pub const LOCK_NB: c_int = 4;
pub const LOCK_UN: c_int = 8;

RAII File Lock Guard in Rust With Drop-Based Release

rust raii drop
by codesnips 3 tabs
javascript
class TokenBucket {
  constructor(capacity, refillPerSecond) {
    this.capacity = capacity;
    this.refillPerMs = refillPerSecond / 1000;
    this.tokens = capacity;
    this.lastRefill = Date.now();

Token Bucket Rate Limiter Middleware for Express with Per-Key Buckets

nodejs express rate-limiting
by codesnips 4 tabs
sql
CREATE TABLE daily_metrics (
    id          BIGGENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    tenant_id   BIGINT      NOT NULL,
    metric      TEXT        NOT NULL,
    day         DATE        NOT NULL,
    count       BIGINT      NOT NULL DEFAULT 0,

SQL upsert for counters (ON CONFLICT DO UPDATE)

postgres sql concurrency
by codesnips 3 tabs