# 来源
- [https://github.com/facebook/rocksdb/wiki/Rate-Limiter](https://github.com/facebook/rocksdb/wiki/Rate-Limiter)
# How to use
```cpp
RateLimiter* rate_limiter = NewGenericRateLimiter(
rate_bytes_per_sec /* int64_t */,
refill_period_us /* int64_t */,
fairness /* int32_t */);
```
## 参数
- `rate_bytes_per_sec`:大多数时候只需要设置此值。控制每秒 flush 和 compaction 的写入量。
- 当前只限制 compaction 和 flush 的写入速度(例如 WAL 的写入不受限)
- `refill_period_us`:控制 tokens 填充的频率。
- 例如:rate_bytes_per_sec 设置为 10MB/s,refill_period_us 设置为 100 ms,则每 100 ms 填充 1 MB。
- 值越大会导致 burst writes,太小会引入过多的 CPU 开销。
- 默认值是 100ms = 100,1000 us 可以适合大多数场景
- `fairness`:
- 写入请求有多个优先级:
- `rocksdb::Env::IO_USER`
- `rocksdb::Env::IO_HIGH` // flush IO
- `rocksdb::Env::IO_MID`
- `rocksdb::Env::IO_LOW` // compaction 相关 IO
- `rocksdb::Env::IO_USER` 优先级最高,一旦出现就首先被处理
- 剩下的优先级受控于 fairness 参数,优先级越高就可能越早被处理
## Maximum bytes of a single request
- 单次请求不能超过 `GetSingleBurstBytes()`,否则需要分多次申请
- 默认为 `refill_bytes_per_period_`
## Statistic
- `NUMBER_RATE_LIMITER_DRAINS` 对应 `rocksdb.number.rate_limiter.drains`
- 表示token被消耗完的 refill interval 的数量
- Number of refill intervals where rate limiter's bytes are fully consumed.