concurrency

rust
use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
use axum::extract::State;
use axum::response::Response;
use axum::routing::get;
use axum::Router;

Fan-Out Domain Events to WebSocket Clients With a Tokio Broadcast Channel

tokio async broadcast
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
module BoundedFanOut
  Result = Struct.new(:value, :error) do
    def ok?
      error.nil?
    end
  end

Parallelize Independent External Calls (in a bounded way)

concurrency threads http
by codesnips 3 tabs
python
import errno
import fcntl
import os
import time

File-Based Locking to Prevent Concurrent Cron Job Runs in Python

locking concurrency fcntl
by codesnips 3 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
java
@Configuration
@EnableScheduling
@EnableConfigurationProperties(CleanupProperties.class)
public class SchedulingConfig {

    @Bean(destroyMethod = "shutdown")

Recurring Cleanup Job with Spring @Scheduled and a Shutdown-Aware Executor

spring spring-boot scheduling
by codesnips 3 tabs
python
import functools
import threading


def debounce(wait):
    def decorator(func):

Debounce Repeated Function Calls With a Thread-Safe Python Decorator

python decorators debounce
by codesnips 2 tabs
python
import asyncio
from collections import defaultdict

from fastapi import WebSocket

FastAPI WebSocket Connection Manager for Broadcasting to Room Subscribers

fastapi websockets asyncio
by codesnips 3 tabs
rust
use std::cmp::{Ordering, Reverse};

#[derive(Debug, Clone)]
pub struct ScheduledTask<T> {
    pub priority: u8,
    pub seq: u64,

Priority Task Scheduler in Rust with BinaryHeap and Reverse Ordering

rust binary-heap scheduler
by codesnips 3 tabs
rust
use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError};

#[derive(Default)]
struct Metrics {
    per_endpoint: HashMap<String, u64>,

Thread-Safe Request Metrics with Arc, Mutex, and a Poison-Recovery Guard

rust concurrency arc
by codesnips 3 tabs
java
package com.example.notifications;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;

Live Browser Notifications with Spring Boot SseEmitter and a Broadcaster Service

spring-boot sse server-sent-events
by codesnips 3 tabs
go
package pqueue

type Job struct {
	ID       string
	Payload  interface{}
	Priority int

Priority Job Queue in Go Backed by container/heap

go container-heap priority-queue
by codesnips 3 tabs