go
package pricecache

import (
	"context"
	"sync"

Collapse Concurrent Identical Requests in Go with singleflight

go singleflight concurrency
by codesnips 3 tabs
rust
#[derive(Debug, Clone)]
pub enum Outcome {
    Approve,
    Review,
    Reject(String),
}

Modeling and Evaluating a Decision Tree with Recursive Rust Enums

rust enums recursion
by codesnips 3 tabs
php
<?php

namespace App\Http\Controllers\Auth;

use App\Events\UserRegistered;
use App\Http\Controllers\Controller;

Dispatch a Queued Welcome Email via Laravel Event Listeners on Registration

laravel events listeners
by codesnips 4 tabs
python
import enum
import datetime as dt

from sqlalchemy import Column, Integer, String, BigInteger, Enum, DateTime
from sqlalchemy.orm import declarative_base

Streaming FastAPI File Uploads with a Background Validation Task

fastapi uploads background-tasks
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
ruby
class Article < ApplicationRecord
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings

  scope :published, -> { where.not(published_at: nil) }

Filtering a Listing by Tags with a has_many :through Scope and a Query Object

rails activerecord has-many-through
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
javascript
import { useState, useEffect, useCallback } from 'react';

export function useLocalStorage(key, initialValue) {
  const readValue = useCallback(() => {
    if (typeof window === 'undefined') return initialValue;
    try {

Persist a React Shopping Cart to localStorage with a Context Provider

javascript react context
by codesnips 3 tabs
php
<?php

declare(strict_types=1);

namespace App\Security;

PHP Login Rate Limiting with a Sliding-Window Throttle Middleware

php rate-limiting middleware
by codesnips 3 tabs
go
package upload

import (
	"errors"
	"io"
	"log"

Streaming Multipart File Uploads to Disk in Go net/http

go net-http multipart
by codesnips 3 tabs
rust
use std::error::Error;
use std::fmt;
use std::io;
use std::num::ParseIntError;

#[derive(Debug)]

Custom Error Enum With From Conversions for the ? Operator in Rust

rust error-handling traits
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