go
package worker

import (
	"context"
	"errors"
	"log"

Fan-Out Worker Pool With Context Cancellation and Graceful Shutdown in Go

go concurrency goroutines
by codesnips 2 tabs
ruby
class OnboardingForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  STEPS = %i[account profile preferences].freeze

Multi-Step Wizard Form in Rails with a Form Object and Session-Backed State

rails form-objects activemodel
by codesnips 3 tabs
java
package com.example.payments;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.springframework.stereotype.Service;

Instrumenting a Spring Service with Micrometer Counters, Timers, and @Timed

spring-boot micrometer metrics
by codesnips 3 tabs
python
from django.conf import settings
from django.db import models
from django.utils import timezone


class InvoiceQuerySet(models.QuerySet):

Scope a Django DetailView to the Request User with get_queryset

django authorization class-based-views
by codesnips 3 tabs
javascript
import { useCallback, useEffect, useRef } from "react";

export function useRafThrottle(callback) {
  const callbackRef = useRef(callback);
  const frameRef = useRef(null);
  const latestArgs = useRef([]);

Throttle a Scroll-Driven Reading Progress Bar with a useScrollProgress Hook

react hooks scroll
by codesnips 3 tabs
php
<?php

namespace App\Http\Resources\V1;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

Versioning a Laravel API with Route Groups and Resource Transformers

laravel api versioning
by codesnips 4 tabs
rust
use std::time::Duration;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
    pub endpoint: String,
    pub timeout: Duration,

Lock-Free Config Reads With Arc-Swap and Copy-on-Write Snapshots in Rust

rust arc copy-on-write
by codesnips 3 tabs
typescript
import { Injectable, Scope } from '@nestjs/common';
import { DataSource, EntityManager, QueryRunner } from 'typeorm';

@Injectable({ scope: Scope.REQUEST })
export class TransactionContext {
  private queryRunner?: QueryRunner;

Request-Scoped TypeORM QueryRunner Provider for Transactional Writes in NestJS

nestjs typeorm transactions
by codesnips 3 tabs
ruby
class Cart < ApplicationRecord
  TTL = 30.minutes

  has_many :line_items, dependent: :destroy

  enum status: { active: 0, expired: 1, checked_out: 2 }

Expiring Idle Shopping Carts with a TTL Check and a Sweeper Job in Rails

rails background-jobs sidekiq
by codesnips 3 tabs
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