typescript
import { AxiosError } from 'axios';

export const RETRY_CONFIG = {
  maxRetries: 4,
  baseDelayMs: 200,
  maxDelayMs: 5_000,

Exponential-Backoff Retry Interceptor Around NestJS HttpService

nestjs http retry
by codesnips 3 tabs
python
from marshmallow import (
    Schema, fields, validates, validates_schema,
    ValidationError, RAISE,
)
from marshmallow.validate import Length, Email, Equal

Field-Level Signup Validation in Flask with Marshmallow Schemas

flask marshmallow validation
by codesnips 3 tabs
java
package com.example.ratelimit;

public class TokenBucket {

    private final long capacity;
    private final long refillTokens;

Per-Client Token Bucket Rate Limiting with a Spring Boot HandlerInterceptor

spring-boot rate-limiting token-bucket
by codesnips 4 tabs
javascript
import { useState, useRef, useEffect, useCallback } from 'react';

export function useOnScreen(options = {}) {
  const [node, setNode] = useState(null);
  const [isIntersecting, setIsIntersecting] = useState(false);

Infinite Scroll in React with an IntersectionObserver useOnScreen Hook

react hooks intersection-observer
by codesnips 3 tabs
typescript
export interface QueryCodec<T> {
  parse: (raw: string | null) => T;
  serialize: (value: T) => string | null;
}

export function stringParam(fallback = ""): QueryCodec<string> {

Sync Typed Form State to the URL Query String with a useQueryState Hook

react hooks url-state
by codesnips 3 tabs
python
from datetime import date, datetime
from decimal import Decimal, InvalidOperation
from enum import Enum

from pydantic import BaseModel, field_validator

Parsing and Normalizing a Transactions CSV into Typed Records with Pydantic

csv pydantic data-parsing
by codesnips 2 tabs
rust
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    #[serde(default)]
    pub server: ServerConfig,

Layered Config Loading From TOML File and Environment Variables in Rust

config serde toml
by codesnips 3 tabs
ruby
class Cart < ApplicationRecord
  has_many :cart_items, dependent: :destroy

  EXPIRY_WINDOW = 2.hours

  scope :active, -> { where(state: :active) }

Expire Stale Shopping Carts With a Rails Scope and a Recurring Reaper Job

rails activerecord background-jobs
by codesnips 3 tabs
java
@MappedSuperclass
@FilterDef(
    name = "tenantFilter",
    parameters = @ParamDef(name = "tenantId", type = String.class)
)
@Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")

Enforce Multi-Tenant Row Isolation With Hibernate @Filter Enabled Per Request in Spring Boot

spring-boot hibernate jpa
by codesnips 4 tabs
javascript
function createReadiness() {
  let ready = true;

  function markUnready() {
    ready = false;
  }

Graceful HTTP Server Shutdown on SIGTERM With In-Flight Request Draining in Node.js

nodejs http graceful-shutdown
by codesnips 3 tabs
typescript
import type { Redis } from "ioredis";

export interface RawEntry {
  member: string;
  score: number;
}

Redis-Backed Paginated Leaderboard With a Repository and Ranking Service

redis leaderboard pagination
by codesnips 3 tabs
rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Page<T> {
    pub items: Vec<T>,
    #[serde(default)]

Streaming a Paginated HTTP API as a Rust Iterator

rust iterator pagination
by codesnips 3 tabs