ruby
require 'sinatra/base'
require_relative 'upload_store'

class UploadApp < Sinatra::Base
  MAX_BYTES = 50 * 1024 * 1024

Streaming Multipart File Uploads to Disk in Sinatra Without Buffering

sinatra rack file-upload
by codesnips 3 tabs
php
<?php

namespace App\Event;

use Symfony\Contracts\EventDispatcher\Event;

Symfony: Send a Welcome Email Asynchronously with Messenger and an Event Subscriber

symfony messenger async
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
rust
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub enum LogLevel {
    Info,
    Warn,

Streaming Log File Aggregation With Buffered Line Iteration in Rust

rust streaming file-io
by codesnips 3 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
ruby
Rails.application.routes.draw do
  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :articles, only: [:index, :show, :create, :update, :destroy]
      resources :sessions, only: [:create]
    end

Versioned JSON API Namespace in Rails With a Shared Base Controller

rails api versioning
by codesnips 3 tabs
python
from sqlalchemy import select
from sqlalchemy.orm import Session

from .models import User

BATCH_SIZE = 1000

Stream a Large CSV Export in FastAPI With StreamingResponse and a Generator

fastapi streaming csv
by codesnips 3 tabs
rust
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use uuid::Uuid;

pub struct Id<T> {

Type-Safe Entity IDs in Rust with a Zero-Cost Id<T> Newtype

rust newtype type-safety
by codesnips 3 tabs
lua
-- KEYS[1] = bucket key
-- ARGV[1] = capacity, ARGV[2] = refill_rate (tokens/sec)
-- ARGV[3] = now_ms, ARGV[4] = requested tokens
local capacity    = tonumber(ARGV[1])
local refill_rate = tonumber(ARGV[2])
local now_ms      = tonumber(ARGV[3])

Token Bucket Rate Limiting in a Servlet Filter with Redis and Lua

rate-limiting token-bucket servlet
by codesnips 3 tabs
javascript
const BASE_URL = "/api/search";

export async function searchProducts(query, { signal } = {}) {
  const params = new URLSearchParams({ q: query, limit: "10" });
  const res = await fetch(`${BASE_URL}?${params}`, {
    signal,

Cancel Stale Autocomplete Requests with AbortController in a React Hook

react hooks abortcontroller
by codesnips 3 tabs
php
<?php

namespace App\Jobs;

use App\Models\Invoice;
use App\Services\StripeGateway;

Preventing Double-Charges in Laravel with WithoutOverlapping Job Middleware

laravel queues background-jobs
by codesnips 3 tabs
go
package logctx

import (
	"context"
	"log/slog"
)

Request-Scoped Structured Logging with slog and Context in Go

go logging slog
by codesnips 3 tabs