Kotlin-rate-limiter

Looking for feedback and users!

I built a small library in this space that may be useful as a reference implementation: https://github.com/delder004/kotlin-rate-limiter

It’s a coroutine-native rate limiter for outbound/client-side throttling, with no runtime dependencies beyond kotlinx-coroutines.

It currently includes:

  • bursty/token-bucket limiting

  • smooth/evenly paced limiting

  • composition for layered limits like 5/sec + 100/min

A few design choices that worked well:

  • suspending acquire() plus non-suspending tryAcquire()

  • Denied(retryAfter: Duration) for immediate backoff decisions

  • lock-free AtomicReference + CAS state management, with lazy refill and no background coroutine

  • cancellation-safe acquire() that refunds reserved permits

  • injectable TimeSource, so tests work with runTest and virtual time

Live demo: https://kotlin-rate-limiter-demo.dev/

1 Like