concurrency

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
python
import time
import threading
from dataclasses import dataclass, field
from typing import Callable, Dict, List

Size-and-Time Batching Metrics Buffer With a Background Flush Loop

metrics batching buffering
by codesnips 4 tabs
python
import time
from dataclasses import dataclass
from typing import Optional

import requests

Bounded Concurrent HTTP Fan-Out With ThreadPoolExecutor and Retries

python concurrency threadpool
by codesnips 2 tabs
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
rust
#[derive(Debug, Clone)]
pub struct Order {
    pub id: u64,
    pub amount_cents: i64,
    pub currency: [u8; 3],
}

Partition a Slice of Orders into Shards for Parallel Processing with Rayon

rust rayon parallelism
by codesnips 2 tabs
rust
use std::time::{Duration, Instant};

pub struct LeakyBucket {
    level: f64,
    capacity: f64,
    rate_per_sec: f64,

Leaky-Bucket Rate Shaper for a Tokio Message Consumer

rate-limiting leaky-bucket backpressure
by codesnips 3 tabs
rust
use std::sync::mpsc::{self, SyncSender};
use std::sync::{Arc, Mutex};
use std::thread;

mod worker;
use worker::{Message, Worker};

Bounded Thread Pool With Backpressure Using Rust Channels

threads concurrency channels
by codesnips 3 tabs
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
sql
CREATE TABLE idempotency_keys (
    id              BIGSERIAL PRIMARY KEY,
    idempotency_key TEXT        NOT NULL,
    request_path    TEXT        NOT NULL,
    request_fingerprint TEXT    NOT NULL,
    status          TEXT        NOT NULL DEFAULT 'in_progress',

Idempotent POST Requests with an Idempotency-Key Middleware in Express

express idempotency middleware
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