Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retry infinite loops. Fixes #1964 #1967

Merged
merged 2 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/apub/src/fetcher/webfinger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use lemmy_apub_lib::{
};
use lemmy_db_schema::newtypes::DbUrl;
use lemmy_utils::{
request::{retry, RecvError},
request::{retry, RecvError, RETRY_LIMIT},
LemmyError,
};
use lemmy_websocket::LemmyContext;
Expand Down Expand Up @@ -81,6 +81,11 @@ where
);
debug!("Fetching webfinger url: {}", &fetch_url);

*request_counter += 1;
if *request_counter > RETRY_LIMIT {
return Err(LemmyError::from(anyhow!("Request retry limit reached")));
}

let response = retry(|| context.client().get(&fetch_url).send()).await?;

let res: WebfingerResponse = response
Expand Down
10 changes: 3 additions & 7 deletions crates/apub_lib/src/object_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use activitystreams::chrono::{Duration as ChronoDuration, NaiveDateTime, Utc};
use anyhow::anyhow;
use diesel::NotFound;
use lemmy_utils::{
request::{build_user_agent, retry},
request::{build_user_agent, retry, RETRY_LIMIT},
settings::structs::Settings,
LemmyError,
};
Expand All @@ -18,10 +18,6 @@ use std::{
use tracing::info;
use url::Url;

/// Maximum number of HTTP requests allowed to handle a single incoming activity (or a single object
/// fetch through the search). This should be configurable.
static REQUEST_LIMIT: i32 = 25;

static CLIENT: Lazy<Client> = Lazy::new(|| {
Client::builder()
.user_agent(build_user_agent(&Settings::get()))
Expand Down Expand Up @@ -119,8 +115,8 @@ where
info!("Fetching remote object {}", self.to_string());

*request_counter += 1;
if *request_counter > REQUEST_LIMIT {
return Err(LemmyError::from(anyhow!("Request limit reached")));
if *request_counter > RETRY_LIMIT {
return Err(LemmyError::from(anyhow!("Request retry limit reached")));
}

let res = retry(|| {
Expand Down
12 changes: 8 additions & 4 deletions crates/utils/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ struct SendError(pub String);
#[error("Error receiving response, {0}")]
pub struct RecvError(pub String);

/// Maximum number of HTTP requests allowed to handle a single incoming activity (or a single object
/// fetch through the search). This should be configurable.
pub static RETRY_LIMIT: i32 = 25;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might as well move this directly into the config.


pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest::Error>
where
F: Fn() -> Fut,
Expand Down Expand Up @@ -58,7 +62,7 @@ pub struct SiteMetadata {

/// Fetches the post link html tags (like title, description, image, etc)
pub async fn fetch_site_metadata(client: &Client, url: &Url) -> Result<SiteMetadata, LemmyError> {
let response = retry(|| client.get(url.as_str()).send()).await?;
let response = client.get(url.as_str()).send().await?;

let html = response
.text()
Expand Down Expand Up @@ -132,7 +136,7 @@ pub(crate) async fn fetch_pictrs(
utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
);

let response = retry(|| client.get(&fetch_url).send()).await?;
let response = client.get(&fetch_url).send().await?;

let response: PictrsResponse = response
.json()
Expand Down Expand Up @@ -201,8 +205,8 @@ pub async fn fetch_site_data(
}
}

async fn is_image_content_type(client: &Client, test: &Url) -> Result<(), LemmyError> {
let response = retry(|| client.get(test.to_owned()).send()).await?;
async fn is_image_content_type(client: &Client, url: &Url) -> Result<(), LemmyError> {
let response = client.get(url.as_str()).send().await?;
if response
.headers()
.get("Content-Type")
Expand Down