13 releases

0.6.0 Jul 12, 2023
0.5.1 Nov 29, 2022
0.5.0 Sep 19, 2022
0.4.2 Feb 9, 2022
0.1.2 Nov 17, 2019

#29 in Network programming

Download history 86655/week @ 2023-08-14 75241/week @ 2023-08-21 90244/week @ 2023-08-28 84522/week @ 2023-09-04 77900/week @ 2023-09-11 80127/week @ 2023-09-18 90850/week @ 2023-09-25 98701/week @ 2023-10-02 112842/week @ 2023-10-09 125307/week @ 2023-10-16 100956/week @ 2023-10-23 125325/week @ 2023-10-30 102621/week @ 2023-11-06 112188/week @ 2023-11-13 91321/week @ 2023-11-20 119511/week @ 2023-11-27

428,488 downloads per month
Used in 116 crates (63 directly)

MIT license


Build status codecov Docs crates.io

governor - a library for regulating the flow of data

This library is an implementation of the Generic Cell Rate Algorithm for rate limiting in Rust programs.

It is intended to help your program know how much strain it is supposed to put on external services (and, to some extent, to allow your services to regulate how much strain they take on from their users). In a way, it functions like the iconic steam governor from which this library takes its name:

a centrifugal governor

Implementation and constraints

The rate-limiting algorithms in this crate are implemented using the Generic Cell Rate Algorithm (GCRA). The GCRA is functionally equivalent to a leaky bucket, but has a few advantages over most leaky bucket implementations:

  • No background "drip" process is necessary to keep up maintenance on the bucket,
  • it updates its state, whenever a request comes in, continuously on a nanosecond scale, and
  • it keeps its state in a single AtomicU64 integer.

The rate-limiting state used here does not take up much memory (only 64 bits!) and is updated thread-safely with a compare-and-swap operation. Compared to ratelimit_meter's implementation using Mutexes, it is on average 10x faster when used on multiple threads.


The speed comes at a slight cost: Each rate-limiter and its state is only useful for 584 years after creation. If you are trying to power the Long Now Foundation's computers with this library, please get in touch.

How does this relate to ratelimit_meter?

This project is a fork/rebranding/continuation of ratelimit_futures, based on a few key insights and advancements in the ecosystem:

  • The 2018 edition is now both available and extremely useful.
  • Futures and async/await are stable.
  • ratelimit_meter was too generic for its own good, implementing two suboptimal variants of the same rate-limiting algorithm.

Let's go through these in order:

Rust 2018

The code in this crate targets Rust's 2018 edition. This has allowed making the code less complicated and more idiomatic.


Before Rust 1.39, the only way to use Futures in stable was to make combinators or poll them manually. There is a crate for ratelimit_meter that implements a rate-limiting future in about 80 lines. The equivalent functionality with async/await takes about three lines in this crate.

No more two algorithms that do the same

ratelimit_meter shipped "two" algorithm classes, LeakyBucket and GCRA. These behaved exactly the same (modulo a glitch where the first cell in a GCRA was free), forcing every user to make a decision that ultimately meant nothing.

This crate implements only the GCRA algorithm (minus the "first cell is free" glitch) and does so in a more optimal way than ratelimit_meter could have.

The return values here are mostly concrete types, and the only type parameter most things accept is the clock implementation, in order to compile on no_std.

So, why make a new crate?

There are a few reasons, but mostly these: One, I was unhappy with the name, which I found to not fit very well anymore; and two, I felt such a radically new interface would be burdensome on users in addition to being hard to implement incrementally in the old repo. These reasons may not be very good, but here we are.


~57K SLoC