chore: 支持使用 leaky-bucket 限制请求频率 (#211)

* chore: 移除之前引入的 delay

* feat: 支持为 b 站请求配置频率限制
This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ
2025-01-11 23:24:01 +08:00
committed by GitHub
parent 66a7b1394e
commit 0113bf704d
13 changed files with 57 additions and 65 deletions
+8 -19
View File
@@ -16,23 +16,6 @@ pub struct WatchLaterConfig {
pub path: PathBuf,
}
/// 每次执行操作后的延迟配置
#[derive(Serialize, Deserialize, Default)]
pub struct DelayConfig {
pub refresh_video_list: Option<Delay>,
pub fetch_video_detail: Option<Delay>,
pub download_video: Option<Delay>,
pub download_page: Option<Delay>,
}
/// 延迟的定义,支持固定时间和随机时间
#[derive(Serialize, Deserialize)]
#[serde(untagged, rename_all = "lowercase")]
pub enum Delay {
Random { min: u64, max: u64 },
Fixed(u64),
}
/// NFO 文件使用的时间类型
#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
@@ -47,7 +30,13 @@ pub enum NFOTimeType {
pub struct ConcurrentLimit {
pub video: usize,
pub page: usize,
pub delay: DelayConfig,
pub rate_limit: Option<RateLimit>,
}
#[derive(Serialize, Deserialize)]
pub struct RateLimit {
pub limit: usize,
pub duration: u64,
}
impl Default for ConcurrentLimit {
@@ -55,7 +44,7 @@ impl Default for ConcurrentLimit {
Self {
video: 3,
page: 2,
delay: DelayConfig::default(),
rate_limit: None,
}
}
}
+1 -17
View File
@@ -14,7 +14,7 @@ mod item;
use crate::bilibili::{CollectionItem, Credential, DanmakuOption, FilterOption};
pub use crate::config::global::{ARGS, CONFIG, CONFIG_DIR, TEMPLATE};
use crate::config::item::{deserialize_collection_list, serialize_collection_list, ConcurrentLimit};
pub use crate::config::item::{Delay, NFOTimeType, PathSafeTemplate, WatchLaterConfig};
pub use crate::config::item::{NFOTimeType, PathSafeTemplate, RateLimit, WatchLaterConfig};
fn default_time_format() -> String {
"%Y-%m-%d".to_string()
@@ -136,22 +136,6 @@ impl Config {
ok = false;
error!("允许的并发数必须大于 0");
}
for delay_config in [
&self.concurrent_limit.delay.refresh_video_list,
&self.concurrent_limit.delay.fetch_video_detail,
&self.concurrent_limit.delay.download_video,
&self.concurrent_limit.delay.download_page,
]
.iter()
.filter_map(|x| x.as_ref())
{
if let Delay::Random { min, max } = delay_config {
if min >= max {
ok = false;
error!("随机延迟的最小值应小于最大值");
}
}
}
if !ok {
panic!(
"位于 {} 的配置文件不合法,请参考提示信息修复后继续运行",