concurrency

go
package ratelimit

import (
	"sync"
	"time"
)

Token Bucket Rate-Limiting Middleware for a Go net/http API

go net-http middleware
by codesnips 4 tabs
go
package watch

import (
	"sync"
	"time"
)

Debounce Filesystem Change Events Before Triggering a Rebuild in Go

fsnotify debounce filewatcher
by codesnips 3 tabs
ruby
module RequestStore
  def self.store
    Thread.current[:request_store] ||= {}
  end

  def self.fetch(key)

Hot Path Memoization (within request only)

rails performance memoization
by codesnips 4 tabs
rust
use std::time::Duration;
use tokio_util::sync::CancellationToken;

pub struct Worker {
    id: usize,
    token: CancellationToken,

Graceful Task Shutdown in Tokio Using CancellationToken

tokio async cancellation
by codesnips 3 tabs
java
@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean(name = "orderExecutor")
    public ThreadPoolTaskExecutor orderExecutor() {

Debouncing Order Recalculations with Spring @Async and a Configured ThreadPoolTaskExecutor

spring-boot async thread-pool
by codesnips 3 tabs
sql
CREATE TYPE outbox_status AS ENUM ('pending', 'retry', 'processing', 'done', 'dead');

CREATE TABLE outbox_events (
    id           BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    dedupe_key   TEXT        NOT NULL,
    topic        TEXT        NOT NULL,

Atomic “Read + Mark Processed” with UPDATE … RETURNING

postgres concurrency reliability
by codesnips 3 tabs
go
package main

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

Graceful HTTP Server Shutdown in Go with SIGINT and Context Cancellation

go http graceful-shutdown
by codesnips 2 tabs
ruby
class AddSlugToPosts < ActiveRecord::Migration[7.1]
  def change
    add_column :posts, :slug, :string, null: false
    add_index :posts, :slug, unique: true
  end
end

Database-Backed Unique Slugs with Retry

rails postgres slugs
by codesnips 4 tabs
python
from django.db import transaction
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from .models import Product

Django select_for_update for database locking

django python database
by Priya Sharma 1 tab
lua
-- KEYS[1] = bucket key
-- ARGV[1] = capacity, ARGV[2] = refill_per_sec, ARGV[3] = now (float seconds)
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])

Redis-Backed Token Bucket Rate Limiter as FastAPI Middleware

fastapi redis rate-limiting
by codesnips 4 tabs
ruby
require "securerandom"

class RedisMutex
  class LockError < StandardError; end

  UNLOCK_SCRIPT = <<~LUA.freeze

Redis-Based Distributed Mutex (with TTL)

rails redis concurrency
by codesnips 2 tabs
go
package pricecache

import (
	"context"
	"sync"

Collapse Concurrent Identical Requests in Go with singleflight

go singleflight concurrency
by codesnips 3 tabs