rust
use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError};

#[derive(Default)]
struct Metrics {
    per_endpoint: HashMap<String, u64>,

Thread-Safe Request Metrics with Arc, Mutex, and a Poison-Recovery Guard

rust concurrency arc
by codesnips 3 tabs
php
<?php

namespace App\Models;

use App\Notifications\VerifyEmailNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;

Signed Email-Verification Links with Custom Laravel Notification and Signed Routes

laravel signed-urls email-verification
by codesnips 4 tabs
java
@RestController
@RequestMapping("/api/exports")
public class ExportController {

    private final CsvStreamWriter csvStreamWriter;

Streaming a Large CSV Export in Spring Boot with StreamingResponseBody

spring-boot streaming csv
by codesnips 4 tabs
javascript
const { z } = require('zod');

const signupSchema = z
  .object({
    email: z
      .string()

Reusable Zod Schema Validation Middleware for Express Signup Routes

express zod validation
by codesnips 3 tabs
python
import threading
from contextlib import contextmanager

_state = threading.local()

Per-Tenant Data Isolation in Django with a Thread-Local Current Tenant Manager

django multi-tenancy orm
by codesnips 4 tabs
go
package main

import (
	"log"
	"net/http"
	"time"

Instrumenting Go HTTP Handlers with Prometheus Counters and Histograms

prometheus metrics observability
by codesnips 3 tabs
ruby
class CommentsChannel < ApplicationCable::Channel
  def subscribed
    post = find_post
    if post
      stream_for post
    else

Broadcasting New Comments to Subscribers over an ActionCable Channel

rails actioncable websockets
by codesnips 4 tabs
ruby
class PriceBreakdown < Struct.new(:subtotal_cents, :discount_cents, :tax_cents, keyword_init: true)
  def initialize(*)
    super
    freeze
  end

Memoizing a Pricing Breakdown as an Immutable Value Object in Rails

rails memoization value-object
by codesnips 3 tabs
php
<?php

namespace App\Dto;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

Symfony Avatar Upload: Constrained Form Type, Image Validation, and an Uploader Service

symfony forms validation
by codesnips 4 tabs
rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Interval {
    pub start: i64,
    pub end: i64,
}

Merging Overlapping Booking Intervals with a Sweep-Line in Rust

rust algorithms intervals
by codesnips 3 tabs
typescript
import { useEffect, useState } from 'react';

export function useLocalStorage<T>(
  key: string,
  initialValue: T
): [T, (value: T) => void] {

Persisting a Dark-Mode Theme Toggle with a React ThemeProvider and localStorage

react context hooks
by codesnips 4 tabs
javascript
class BatchLoader {
  constructor(batchFn, { cacheKeyFn = (k) => k } = {}) {
    this.batchFn = batchFn;
    this.cacheKeyFn = cacheKeyFn;
    this.cache = new Map();
    this.queue = [];

Coalescing Concurrent Reads with a DataLoader-Style Batch Loader in Node

dataloader batching graphql
by codesnips 3 tabs