tokio

rust
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let fast = sleep(Duration::from_millis(50));
    let slow = sleep(Duration::from_millis(200));

tokio::select! for racing multiple async operations

rust async tokio
by Marcus Chen 1 tab
rust
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tokio::time::sleep;

#[derive(Debug)]

Rate-Limiting Outbound HTTP with a Token Bucket in Rust (Tokio)

rust tokio rate-limiting
by codesnips 3 tabs
rust
use std::collections::HashSet;
use std::hash::Hash;

pub struct DedupByKey<I, K, F> {
    inner: I,
    key_fn: F,

Order-Preserving Stream Deduplication by Key in Rust with a HashSet Guard

rust streams deduplication
by codesnips 3 tabs
rust
use async_trait::async_trait;
use serde::Serialize;

#[derive(Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Status {

Aggregating Subsystem Health Checks Behind an Axum /healthz Endpoint

axum tokio health-check
by codesnips 3 tabs
rust
use async_trait::async_trait;
use std::io;

#[async_trait]
pub trait ConnectionFactory: Send + Sync + 'static {
    type Connection: Send + 'static;

Building a Bounded Async Database Connection Pool With Tokio Semaphore

rust tokio async
by codesnips 3 tabs
rust
use std::sync::Arc;
use std::time::Duration;

use futures::stream::{FuturesUnordered, StreamExt};
use reqwest::Client;
use tokio::sync::Semaphore;

Bounded Concurrent HTTP Fan-Out With FuturesUnordered and Semaphore in Rust

rust async tokio
by codesnips 3 tabs
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
rust
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        sleep(Duration::from_millis(100)).await;

tokio::spawn for concurrent task execution

rust async tokio
by Marcus Chen 1 tab
rust
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]

Cursor-Paginated Streaming REST Endpoint in Axum with Keyset Pagination

axum rust pagination
by codesnips 3 tabs
rust
use sqlx::PgPool;
use std::time::Duration;
use tokio::sync::mpsc;

#[derive(Debug, Clone)]
pub struct MetricPoint {

Batching Database Writes in Rust with a Size- and Interval-Triggered Flush Buffer

rust tokio sqlx
by codesnips 3 tabs
rust
use std::time::Duration;
use rand::Rng;

#[derive(Clone, Debug)]
pub struct BackoffPolicy {
    pub base_delay: Duration,

Exponential Backoff With Jitter for Retrying Fallible Async Operations in Rust

rust tokio async
by codesnips 3 tabs
rust
use std::time::Duration;
use tokio_util::sync::CancellationToken;

pub struct Worker {
    id: usize,
    token: CancellationToken,

Graceful Task Shutdown in Tokio Using CancellationToken

tokio async cancellation
by codesnips 3 tabs