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
go
package webhook

import (
	"encoding/json"
	"io"
	"net/http"

Verifying and Dispatching Stripe-Style Webhooks by Event Type in Go

webhooks go hmac
by codesnips 3 tabs
java
package com.example.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

Custom Spring Boot HealthIndicator for Queue Depth on /actuator/health

spring-boot actuator health-check
by codesnips 4 tabs
javascript
const withTimeout = (promise, timeoutMs, name) => {
  let timer;
  const timeout = new Promise((_, reject) => {
    timer = setTimeout(
      () => reject(new Error(`check '${name}' timed out after ${timeoutMs}ms`)),
      timeoutMs

Express Health-Check Router With a Pluggable Dependency Checks Registry

express health-check observability
by codesnips 3 tabs
java
import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import java.time.Instant;

@MappedSuperclass
public abstract class SoftDeletable {

Soft-Delete JPA Entities with Hibernate @Where and a Restore-Capable Repository

spring spring-data hibernate
by codesnips 4 tabs
typescript
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

Global ValidationPipe with class-validator DTOs and Transform in NestJS

nestjs validation dto
by codesnips 3 tabs
java
@Configuration
public class TxConfig {

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager tm = new DataSourceTransactionManager(dataSource);

Multi-Step Order Flow With Spring TransactionTemplate and Savepoint Rollback

spring-boot transactions transactiontemplate
by codesnips 3 tabs
python
import time
from collections import namedtuple

from sqlalchemy import text

CheckResult = namedtuple("CheckResult", ["name", "healthy", "latency_ms", "detail"])

Flask Health-Check Blueprint Reporting Database and Redis Status as JSON

flask health-check observability
by codesnips 3 tabs
python
import hmac
import hashlib
import time
from fastapi import Request, HTTPException, status

Verifying Stripe Webhook Signatures With a Reusable FastAPI Dependency

fastapi webhooks security
by codesnips 3 tabs
ruby
class DebouncedReindexJob
  include Sidekiq::Job

  sidekiq_options queue: :indexing, retry: 5

  DEBOUNCE_DELAY = 5 # seconds

Debouncing Sidekiq Jobs Per-Record With Redis So Rapid Updates Coalesce Into One Run

rails sidekiq redis
by codesnips 3 tabs
javascript
import React, { createContext, useCallback, useMemo, useRef, useState } from 'react';
import { ToastViewport } from './ToastViewport';

export const ToastContext = createContext(undefined);

let counter = 0;

Build a React Toast Notification System with Context Provider, Hook, and Portal

react toast context-api
by codesnips 4 tabs
typescript
const RETRYABLE_STATUS = new Set([408, 429, 500, 502, 503, 504]);

export function isRetryable(error: unknown, response?: Response): boolean {
  if (response) {
    return RETRYABLE_STATUS.has(response.status);
  }

Retrying Fetch With Exponential Backoff and Full Jitter in TypeScript

retry backoff jitter
by codesnips 3 tabs