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 |
#25 in #limiter
524 downloads per month
Used in 67 crates
(2 directly)
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
~848K SLoC