codesnips

852 code snips · on codesnips 3 months
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
python
from flask import jsonify
from marshmallow import ValidationError as MarshmallowValidationError
from werkzeug.exceptions import HTTPException


class ApiError(Exception):

Structured JSON Error Handling for Flask API Validation Failures

flask rest-api error-handling
by codesnips 3 tabs
java
@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean(name = "downstreamExecutor")
    public ThreadPoolTaskExecutor downstreamExecutor() {

Aggregating Parallel Downstream Calls With CompletableFuture in a Spring @Async Service

spring-boot completablefuture async
by codesnips 4 tabs
lua
local key = KEYS[1]
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local ttl = tonumber(ARGV[4])

Redis-Backed Token Bucket Rate Limiter with Lua Atomic Refill in Go

rate-limiting token-bucket redis
by codesnips 3 tabs
typescript
import { z } from "zod";

const flagValueSchema = z.union([
  z.boolean(),
  z.object({
    rollout: z.number().min(0).max(100),

Typed Feature-Flag Gate with a Zod Config Loader and useFeatureFlag Hook in React

react feature-flags typescript-hooks
by codesnips 3 tabs
rust
use std::io::{self, Read, Write};

pub const HEADER_LEN: usize = 8; // 4-byte length + 4-byte crc

#[derive(Debug)]
pub enum DecodeError {

Crash-Safe Write-Ahead Log With CRC-Checked Replay in Rust

wal durability crc32
by codesnips 3 tabs
php
<?php

namespace App\Feature;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Service\ResetInterface;

Request-Scoped Feature Flags in Symfony with a Twig Extension and Controller Gate

symfony feature-flags twig
by codesnips 4 tabs