#rate-limiting #limiter #limit #resources #bitcoin #helper #bitcoinleveldb

bitcoinleveldb-limiter

helper abstractions used to limit resource usage to avoid exhaustion

3 releases

0.1.16-alpha.0 Apr 1, 2023
0.1.12-alpha.0 Jan 19, 2023
0.1.10-alpha.0 Jan 18, 2023

#34 in #limiter

Download history 96/week @ 2024-08-24 137/week @ 2024-08-31 104/week @ 2024-09-07 71/week @ 2024-09-14 132/week @ 2024-09-21 103/week @ 2024-09-28 1/week @ 2024-10-05 71/week @ 2024-10-12 62/week @ 2024-10-19 83/week @ 2024-10-26 82/week @ 2024-11-02 33/week @ 2024-11-09 40/week @ 2024-11-16 106/week @ 2024-11-23 139/week @ 2024-11-30 201/week @ 2024-12-07

513 downloads per month
Used in 67 crates (2 directly)

MIT license

265KB
635 lines

bitcoinleveldb-limiter

The bitcoinleveldb-limiter crate provides a simple rate limiter implementation for use in the Bitcoin system.

The Limiter type implements a simple token bucket algorithm, where tokens are added to the bucket at a constant rate and can be consumed by calling acquire(). If there are not enough tokens in the bucket, the acquire() method will block until there are enough tokens. Tokens can also be manually released to the bucket by calling release().

This crate does not contain any mathematical ideas or equations.

Notice: This crate is part of a direct translation from C++ to Rust of the Bitcoin Core. As such, some of the function bodies may still be in the process of translation. Please note that this system will become testable once the translation process is complete.

36> how do we use the rate limiter in leveldb?

The rate limiter in LevelDB is used to limit the rate of file system operations. This can be useful in scenarios where disk I/O needs to be controlled to prevent resource contention or to regulate performance.

The rate limiter can be enabled by passing a pointer to a Limiter object to the Options object used to open a database. The Limiter object can be created using the Limiter::new() method, which takes two arguments: the rate at which bytes should be read or written (in bytes per second) and the size of the burst in bytes.

Once the Limiter object is created, it can be used to regulate the rate of file system operations using the acquire() and release() methods. The acquire() method should be called before each file system operation to ensure that the rate limit is not exceeded. The release() method should be called after each file system operation to release any acquired resources.

Here is an example of how the rate limiter can be used in Rust:

use bitcoinleveldb_limiter::{Limiter, acquire, release};
use bitcoinleveldb::{Options, DB};

let mut opts = Options::new();
let limiter = Limiter::new(1024 * 1024, 1024 * 1024 * 10); // 1 MB/s, burst of 10 MB
opts.set_rate_limiter(limiter);

let db = DB::open(opts, "mydb").unwrap();

let key = "hello".as_bytes();
let value = "world".as_bytes();

acquire(limiter, value.len() as u64);
db.put(key, value);
release(limiter, value.len() as u64);

In this example, the rate limiter is created with a rate of 1 MB/s and a burst of 10 MB. Before calling db.put(), the acquire() method is called to acquire the necessary resources to write the value to the database. After the write is complete, the release() method is called to release the acquired resources.

Note that the rate limiter is only applied to file system operations performed by LevelDB, such as reading and writing data to disk. It does not regulate CPU usage or other system resources.

Dependencies

~89MB
~852K SLoC