javascript
class DeadlineError extends Error {
  constructor(message = 'request deadline exceeded') {
    super(message);
    this.name = 'DeadlineError';
    this.code = 'DEADLINE_EXCEEDED';
  }

Enforce Per-Request Timeouts by Aborting Downstream Fetches with AbortSignal

node fetch abortcontroller
by codesnips 3 tabs
java
package com.example.caching.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;

Conditional GET with ETag ShallowEtagHeaderFilter and Cache-Control in Spring Boot

spring-boot http-caching etag
by codesnips 3 tabs
python
import asyncio
import time
from dataclasses import dataclass
from typing import Any, Dict

In-Memory TTL Cache for FastAPI Endpoints With a Cache-Key Builder Dependency

fastapi caching ttl
by codesnips 3 tabs
java
package com.example.http;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;

Attaching Bearer Tokens to Spring RestClient Calls with a ClientHttpRequestInterceptor

spring-boot restclient interceptor
by codesnips 3 tabs
javascript
const DEFAULTS = {
  baseDelay: 300,
  maxDelay: 8000,
  factor: 2,
  jitter: 0.5,
};

Retry Failed React Mutations with Exponential Backoff and a Status Component

react hooks retry
by codesnips 4 tabs
ruby
class DiscountCode < ApplicationRecord
  has_many :redemptions, dependent: :destroy

  enum discount_type: { percentage: 0, fixed: 1 }

  validates :code, presence: true, uniqueness: { case_sensitive: false }

Redeem Discount Codes in Rails with a Locking Service and Row-Level Guards

rails activerecord discounts
by codesnips 4 tabs
java
public class PayloadLimitFilter extends OncePerRequestFilter {

    private final long maxBytes;
    private final Set<String> allowedTypes;

    public PayloadLimitFilter(long maxBytes, Set<String> allowedTypes) {

Enforce Request Payload Size and Content-Type with a Servlet Filter and Controller Guard

spring-boot servlet-filter validation
by codesnips 4 tabs
javascript
const { AsyncLocalStorage } = require('async_hooks');

const storage = new AsyncLocalStorage();

function runWithTenant(tenantId, userId, callback) {
  return storage.run({ tenantId, userId }, callback);

Per-Tenant Request Context in Express With AsyncLocalStorage and a Scoped Repository

express multi-tenancy middleware
by codesnips 4 tabs
php
<?php

namespace App\Policies;

use App\Models\Article;
use App\Models\User;

Authorizing Article Edits with a Laravel Policy in Controller and Blade

laravel authorization policies
by codesnips 4 tabs
java
@RestController
@RequestMapping("/api/products")
public class ProductController {

    private final ProductRepository repository;
    private final JsonMergePatchService patchService;

Spring Boot PATCH Endpoint With JSON Merge Patch (RFC 7386)

spring-boot rest-api json-merge-patch
by codesnips 4 tabs
go
package webhooksign

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"

Sign Outgoing Webhook Requests With an HMAC-SHA256 Signature and Timestamp

hmac webhooks security
by codesnips 3 tabs
typescript
export interface UseClipboardResult {
  copied: boolean;
  error: Error | null;
  copy: (value: string) => Promise<boolean>;
}

Copy-to-Clipboard Button With a useClipboard Hook and Timed Success Feedback

react hooks clipboard
by codesnips 3 tabs