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
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Serve Purchased Downloads Behind Signed Temporary URLs in Laravel

laravel eloquent signed-urls
by codesnips 4 tabs
ruby
require 'digest'
require 'json'

module CacheHelpers
  def cache_control_public(max_age = 60)
    cache_control :public, :must_revalidate, max_age: max_age

Conditional GET in Sinatra with ETag and Last-Modified for Cacheable JSON Endpoints

sinatra http-caching etag
by codesnips 3 tabs
python
from flask import jsonify
from marshmallow import ValidationError as MarshmallowValidationError
from werkzeug.exceptions import HTTPException


class ApiError(Exception):

Structured JSON Error Handling for Flask API Validation Failures

flask rest-api error-handling
by codesnips 3 tabs
java
@Configuration
@EnableAsync
public class AsyncConfig {

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

Aggregating Parallel Downstream Calls With CompletableFuture in a Spring @Async Service

spring-boot completablefuture async
by codesnips 4 tabs
lua
local key = KEYS[1]
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local ttl = tonumber(ARGV[4])

Redis-Backed Token Bucket Rate Limiter with Lua Atomic Refill in Go

rate-limiting token-bucket redis
by codesnips 3 tabs
typescript
import { z } from "zod";

const flagValueSchema = z.union([
  z.boolean(),
  z.object({
    rollout: z.number().min(0).max(100),

Typed Feature-Flag Gate with a Zod Config Loader and useFeatureFlag Hook in React

react feature-flags typescript-hooks
by codesnips 3 tabs
rust
use std::io::{self, Read, Write};

pub const HEADER_LEN: usize = 8; // 4-byte length + 4-byte crc

#[derive(Debug)]
pub enum DecodeError {

Crash-Safe Write-Ahead Log With CRC-Checked Replay in Rust

wal durability crc32
by codesnips 3 tabs
php
<?php

namespace App\Feature;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Service\ResetInterface;

Request-Scoped Feature Flags in Symfony with a Twig Extension and Controller Gate

symfony feature-flags twig
by codesnips 4 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Aggregate Daily Sales into a Summary Table with a Laravel Console Command

laravel php console-command
by codesnips 4 tabs
sql
CREATE TABLE outbox_deliveries (
    id              BIGSERIAL PRIMARY KEY,
    dedupe_key      TEXT        NOT NULL,
    target_url      TEXT        NOT NULL,
    payload         JSONB       NOT NULL,
    status          TEXT        NOT NULL DEFAULT 'PENDING',

Reliable Webhook Delivery with a Transactional Outbox and Dedupe Key in Spring Boot

spring-boot webhooks outbox-pattern
by codesnips 3 tabs
javascript
const rolePermissions = {
  guest: ['article:read'],
  author: ['article:read', 'article:create', 'article:update:own'],
  editor: ['article:publish', 'article:update:any'],
  admin: ['user:manage', 'role:assign']
};

Role-Based Access Control in Express with Permission Middleware and Protected Routes

express rbac authorization
by codesnips 3 tabs