Skip to content

Instantly share code, notes, and snippets.

View sergera's full-sized avatar

Sergio Luciano de Souza Joselli sergera

View GitHub Profile
@sergera
sergera / rate_limiter.rs
Last active October 4, 2023 22:56
Abstract rate limiter with channel communication, to be used in a separate thread, makes request throttling easy
use std::collections::VecDeque;
use std::future::Future;
use std::pin::Pin;
use eyre::*;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::time::{Duration, Instant};
use tracing::*;
pub enum ThrottledQueueCallbackType<T> {
@sergera
sergera / snowflake.rs
Created October 4, 2023 20:28
Fast, concurrent, i64 snowflake ids (inspired by Twitter's implementation) for up to 4 services
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
/*
bit anatomy (i64):
_
1 bit: signing bit, should always be positive (zero)