Tweak rate limit delay logic, log skipped files

This commit is contained in:
Tyler Hallada 2021-07-12 12:15:07 -04:00
parent 81fcdfa381
commit af5c11acc7
4 changed files with 35 additions and 23 deletions

View File

@ -152,16 +152,24 @@ pub async fn main() -> Result<()> {
let _mod_span = mod_span.enter(); let _mod_span = mod_span.enter();
let files_resp = nexus_api::files::get(&client, db_mod.nexus_mod_id).await?; let files_resp = nexus_api::files::get(&client, db_mod.nexus_mod_id).await?;
if let Some(duration) = files_resp.wait { debug!(duration = ?files_resp.wait, "sleeping");
debug!(?duration, "sleeping"); sleep(files_resp.wait).await;
sleep(duration).await;
}
// Filter out replaced/deleted files (indicated by null category) // Filter out replaced/deleted files (indicated by null category)
let files = files_resp let files = files_resp
.files()? .files()?
.into_iter() .into_iter()
.filter(|file| file.category.is_some()); .filter(|file| match file.category {
Some(_) => true,
None => {
info!(
name = file.file_name,
id = file.file_id,
"skipping file with no category"
);
false
}
});
for api_file in files { for api_file in files {
let file_span = let file_span =
@ -326,16 +334,14 @@ pub async fn main() -> Result<()> {
} }
plugins_archive.finish()?; plugins_archive.finish()?;
if let Some(duration) = download_link_resp.wait { debug!(duration = ?download_link_resp.wait, "sleeping");
debug!(?duration, "sleeping"); sleep(download_link_resp.wait).await;
sleep(duration).await;
}
} }
} }
page += 1; page += 1;
debug!(?page, ?has_next_page, "sleeping 1 second"); debug!(?page, ?has_next_page, "sleeping 1 second");
sleep(Duration::new(1, 0)).await; sleep(Duration::from_secs(1)).await;
} }
Ok(()) Ok(())

View File

@ -11,7 +11,7 @@ use tracing::{info, instrument};
use super::{rate_limit_wait_duration, GAME_NAME, USER_AGENT}; use super::{rate_limit_wait_duration, GAME_NAME, USER_AGENT};
pub struct DownloadLinkResponse { pub struct DownloadLinkResponse {
pub wait: Option<Duration>, pub wait: Duration,
json: Value, json: Value,
} }

View File

@ -8,7 +8,7 @@ use tracing::{info, instrument};
use super::{rate_limit_wait_duration, GAME_NAME, USER_AGENT}; use super::{rate_limit_wait_duration, GAME_NAME, USER_AGENT};
pub struct FilesResponse { pub struct FilesResponse {
pub wait: Option<Duration>, pub wait: Duration,
json: Value, json: Value,
} }

View File

@ -12,22 +12,28 @@ pub static GAME_NAME: &str = "skyrimspecialedition";
pub const GAME_ID: u32 = 1704; pub const GAME_ID: u32 = 1704;
pub static USER_AGENT: &str = "mod-mapper/0.1"; pub static USER_AGENT: &str = "mod-mapper/0.1";
pub fn rate_limit_wait_duration(res: &Response) -> Result<Option<std::time::Duration>> { pub fn rate_limit_wait_duration(res: &Response) -> Result<std::time::Duration> {
let daily_remaining = res let daily_remaining: i32 = res
.headers() .headers()
.get("x-rl-daily-remaining") .get("x-rl-daily-remaining")
.expect("No daily remaining in response headers"); .expect("daily remaining in response headers")
let hourly_remaining = res .to_str()?
.parse()
.expect("daily remaining in response headers to be a number");
let hourly_remaining: i32 = res
.headers() .headers()
.get("x-rl-hourly-remaining") .get("x-rl-hourly-remaining")
.expect("No hourly limit in response headers"); .expect("hourly remaining in response headers")
.to_str()?
.parse()
.expect("hourly remaining in response headers to be a number");
let hourly_reset = res let hourly_reset = res
.headers() .headers()
.get("x-rl-hourly-reset") .get("x-rl-hourly-reset")
.expect("No hourly reset in response headers"); .expect("hourly reset in response headers");
info!(daily_remaining = ?daily_remaining, hourly_remaining = ?hourly_remaining, "rate limit check"); info!(daily_remaining, hourly_remaining, "rate limit check");
if hourly_remaining == "0" { if daily_remaining == 0 && hourly_remaining == 0 {
let hourly_reset = hourly_reset.to_str()?.trim(); let hourly_reset = hourly_reset.to_str()?.trim();
let hourly_reset: DateTime<Utc> = let hourly_reset: DateTime<Utc> =
(DateTime::parse_from_str(hourly_reset, "%Y-%m-%d %H:%M:%S %z")? (DateTime::parse_from_str(hourly_reset, "%Y-%m-%d %H:%M:%S %z")?
@ -39,8 +45,8 @@ pub fn rate_limit_wait_duration(res: &Response) -> Result<Option<std::time::Dura
duration = ?duration, "need to wait until rate-limit hourly reset" duration = ?duration, "need to wait until rate-limit hourly reset"
); );
return Ok(Some(duration)); Ok(duration)
} else {
Ok(std::time::Duration::from_secs(1))
} }
Ok(None)
} }