Skip to content

web search done right

disable http redirect following in reqwest and handle it directly in crawler instead.

Mikkel Denkerbd8f69707b02
diff --git a/crates/core/src/config/defaults.rs b/crates/core/src/config/defaults.rs
index 8c1211e7..ab52f037 100644
--- a/crates/core/src/config/defaults.rs
+++ b/crates/core/src/config/defaults.rs
@@ -125,10 +125,6 @@ impl Crawler {
3
}
- pub fn max_redirects() -> usize {
- 5
- }
-
pub fn timeout_seconds() -> u64 {
60
}
diff --git a/crates/core/src/config/mod.rs b/crates/core/src/config/mod.rs
index e16c7257..6b3d2324 100644
--- a/crates/core/src/config/mod.rs
+++ b/crates/core/src/config/mod.rs
@@ -363,9 +363,6 @@ pub struct CrawlerConfig {
#[serde(default = "defaults::Crawler::max_url_slowdown_retry")]
pub max_url_slowdown_retry: u8,
- #[serde(default = "defaults::Crawler::max_redirects")]
- pub max_redirects: usize,
-
pub timeout_seconds: u64,
pub s3: S3Config,
pub router_hosts: Vec<String>,
@@ -486,8 +483,6 @@ pub struct LiveIndexConfig {
pub max_politeness_factor: u32,
#[serde(default = "defaults::Crawler::max_url_slowdown_retry")]
pub max_url_slowdown_retry: u8,
- #[serde(default = "defaults::Crawler::max_redirects")]
- pub max_redirects: usize,
#[serde(default = "defaults::Crawler::timeout_seconds")]
pub timeout_seconds: u64,
diff --git a/crates/core/src/crawler/mod.rs b/crates/core/src/crawler/mod.rs
index ff536468..2aee88f3 100644
--- a/crates/core/src/crawler/mod.rs
+++ b/crates/core/src/crawler/mod.rs
@@ -314,7 +314,7 @@ pub fn reqwest_client(config: &CrawlerConfig) -> Result<reqwest::Client> {
.connect_timeout(timeout)
.http2_keep_alive_interval(None)
.default_headers(headers)
- .redirect(reqwest::redirect::Policy::limited(config.max_redirects))
+ .redirect(reqwest::redirect::Policy::limited(0))
.user_agent(&config.user_agent.full)
.build()
.map_err(|e| Error::from(anyhow!(e)))
diff --git a/crates/core/src/crawler/worker.rs b/crates/core/src/crawler/worker.rs
index 82b28255..9ee134e6 100644
--- a/crates/core/src/crawler/worker.rs
+++ b/crates/core/src/crawler/worker.rs
@@ -356,6 +356,23 @@ impl<S: DatumStream> JobExecutor<S> {
retryable_url.retries += 1;
urls.push_back(retryable_url);
}
+ Err(Error::FetchFailed {
+ status_code,
+ headers,
+ }) if status_code == 301 || status_code == 302 => {
+ if let Some(new_url) = headers
+ .get("location")
+ .and_then(|location| std::str::from_utf8(location.as_bytes()).ok())
+ .and_then(|location| {
+ Url::parse_with_base_url(&retryable_url.weighted_url.url, location).ok()
+ })
+ {
+ let mut retryable_url = retryable_url;
+ retryable_url.weighted_url.url = new_url;
+ retryable_url.retries += 1;
+ urls.push_back(retryable_url);
+ }
+ }
Err(_) => {}
}
}
diff --git a/crates/core/src/live_index/mod.rs b/crates/core/src/live_index/mod.rs
index 221155f7..bee8d976 100644
--- a/crates/core/src/live_index/mod.rs
+++ b/crates/core/src/live_index/mod.rs
@@ -15,9 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::time::Duration;
-use crate::{
- config::{CrawlerConfig, LiveIndexConfig},
-};
+use crate::config::{CrawlerConfig, LiveIndexConfig};
pub use self::index::LiveIndex;
pub use self::index_manager::IndexManager;
@@ -29,7 +27,7 @@ mod index_manager;
const TTL: Duration = Duration::from_secs(60 * 60 * 24 * 60); // 60 days
const PRUNE_INTERVAL: Duration = Duration::from_secs(60 * 60); // 1 hour
const COMPACT_INTERVAL: Duration = Duration::from_secs(60 * 60); // 1 hour
-// const FEED_CHECK_INTERVAL: Duration = Duration::from_secs(60 * 10); // 10 minutes
+ // const FEED_CHECK_INTERVAL: Duration = Duration::from_secs(60 * 10); // 10 minutes
const AUTO_COMMIT_INTERVAL: Duration = Duration::from_secs(60 * 5); // 5 minutes
const EVENT_LOOP_INTERVAL: Duration = Duration::from_secs(5);
const BATCH_SIZE: usize = 512;
@@ -65,7 +63,6 @@ impl From<&LiveIndexConfig> for CrawlerConfig {
min_crawl_delay_ms: live.min_crawl_delay_ms,
max_crawl_delay_ms: live.max_crawl_delay_ms,
max_url_slowdown_retry: live.max_url_slowdown_retry,
- max_redirects: live.max_redirects,
timeout_seconds: live.timeout_seconds,
// no impact
s3: crate::config::S3Config {