Set fetcher timeout to 5 seconds when processing search queries

This commit is contained in:
silverpill 2023-03-26 00:31:33 +00:00
parent dd0c53c5e9
commit 348149bbaa
2 changed files with 14 additions and 12 deletions

View file

@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Allow emoji names containing hyphens. - Allow emoji names containing hyphens.
- Increased remote emoji size limit to 500 kB. - Increased remote emoji size limit to 500 kB.
- Set fetcher timeout to 5 seconds when processing search queries.
### Fixed ### Fixed

View file

@ -41,6 +41,8 @@ use crate::models::{
}; };
use crate::webfinger::types::ActorAddress; use crate::webfinger::types::ActorAddress;
const SEARCH_FETCHER_TIMEOUT: u64 = 5;
enum SearchQuery { enum SearchQuery {
ProfileQuery(String, Option<String>), ProfileQuery(String, Option<String>),
TagQuery(String), TagQuery(String),
@ -107,8 +109,9 @@ async fn search_profiles_or_import(
mut maybe_hostname: Option<String>, mut maybe_hostname: Option<String>,
limit: u16, limit: u16,
) -> Result<Vec<DbActorProfile>, DatabaseError> { ) -> Result<Vec<DbActorProfile>, DatabaseError> {
let mut instance = config.instance();
if let Some(ref hostname) = maybe_hostname { if let Some(ref hostname) = maybe_hostname {
if hostname == &config.instance().hostname() { if hostname == &instance.hostname() {
// This is a local profile // This is a local profile
maybe_hostname = None; maybe_hostname = None;
}; };
@ -122,9 +125,10 @@ async fn search_profiles_or_import(
if profiles.is_empty() { if profiles.is_empty() {
if let Some(hostname) = maybe_hostname { if let Some(hostname) = maybe_hostname {
let actor_address = ActorAddress { username, hostname }; let actor_address = ActorAddress { username, hostname };
instance.fetcher_timeout = SEARCH_FETCHER_TIMEOUT;
match import_profile_by_actor_address( match import_profile_by_actor_address(
db_client, db_client,
&config.instance(), &instance,
&MediaStorage::from(config), &MediaStorage::from(config),
&actor_address, &actor_address,
).await { ).await {
@ -154,12 +158,9 @@ async fn find_post_by_url(
db_client: &mut impl DatabaseClient, db_client: &mut impl DatabaseClient,
url: &str, url: &str,
) -> Result<Option<Post>, DatabaseError> { ) -> Result<Option<Post>, DatabaseError> {
let instance = config.instance(); let mut instance = config.instance();
let storage = MediaStorage::from(config); let storage = MediaStorage::from(config);
let maybe_post = match parse_local_object_id( let maybe_post = match parse_local_object_id(&instance.url(), url) {
&instance.url(),
url,
) {
Ok(post_id) => { Ok(post_id) => {
// Local URL // Local URL
match get_local_post_by_id(db_client, &post_id).await { match get_local_post_by_id(db_client, &post_id).await {
@ -169,6 +170,7 @@ async fn find_post_by_url(
} }
}, },
Err(_) => { Err(_) => {
instance.fetcher_timeout = SEARCH_FETCHER_TIMEOUT;
match import_post( match import_post(
db_client, db_client,
&instance, &instance,
@ -192,10 +194,8 @@ async fn find_profile_by_url(
db_client: &mut impl DatabaseClient, db_client: &mut impl DatabaseClient,
url: &str, url: &str,
) -> Result<Option<DbActorProfile>, DatabaseError> { ) -> Result<Option<DbActorProfile>, DatabaseError> {
let profile = match parse_local_actor_id( let mut instance = config.instance();
&config.instance_url(), let profile = match parse_local_actor_id(&instance.url(), url) {
url,
) {
Ok(username) => { Ok(username) => {
// Local URL // Local URL
match get_user_by_name(db_client, &username).await { match get_user_by_name(db_client, &username).await {
@ -205,9 +205,10 @@ async fn find_profile_by_url(
} }
}, },
Err(_) => { Err(_) => {
instance.fetcher_timeout = SEARCH_FETCHER_TIMEOUT;
get_or_import_profile_by_actor_id( get_or_import_profile_by_actor_id(
db_client, db_client,
&config.instance(), &instance,
&MediaStorage::from(config), &MediaStorage::from(config),
url, url,
).await ).await