class TokenBucket {
constructor(capacity, refillRatePerSec) {
this.capacity = capacity;
this.refillRate = refillRatePerSec;
this.tokens = capacity;
this.lastRefill = Date.now();
package scheduler
import (
"context"
"log"
"sync"
require "securerandom"
require "digest"
class SlidingWindowLimiter
Result = Struct.new(:allowed, :count, :remaining)
-- Step 1: add the column nullable, no default.
-- Catalog-only change in Postgres 11+, returns instantly.
ALTER TABLE orders
ADD COLUMN currency text;
-- Optional: keep the lock attempt bounded so a long-running
package middleware
import (
"net/http"
"sync/atomic"
)
class LeaderboardCache
TOP_KEY = "leaderboard:top".freeze
STATS_KEY = "leaderboard:stats".freeze
def top_players
Rails.cache.fetch(TOP_KEY, expires_in: 5.minutes, race_condition_ttl: 15.seconds) do
class CreateOutboxEvents < ActiveRecord::Migration[7.1]
def change
create_table :outbox_events do |t|
t.string :event_type, null: false
t.string :aggregate_type, null: false
t.string :aggregate_id, null: false
import { Prisma, PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
type TxClient = Prisma.TransactionClient;
class LastSeenTracker
THROTTLE = 5.minutes
PENDING_KEY = "pending:last_seen".freeze
class << self
def touch(user_id, at: Time.current)
class CreateReservations < ActiveRecord::Migration[7.1]
def up
enable_extension "btree_gist" unless extension_enabled?("btree_gist")
create_table :reservations do |t|
t.references :room, null: false, foreign_key: true
package bank
import "errors"
var ErrVersionConflict = errors.New("optimistic lock: version conflict")
var ErrInsufficientFunds = errors.New("insufficient funds")
import queue
import time
from dataclasses import dataclass, field, replace
from typing import Any, Dict, Tuple