feat: add timeout for http client

This commit is contained in:
ihciah 2022-12-28 19:24:25 +08:00
parent 406b2580f0
commit bf5acd966d
No known key found for this signature in database
GPG Key ID: A72018B6522333D3
9 changed files with 39 additions and 36 deletions

32
Cargo.lock generated
View File

@ -80,7 +80,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bot"
version = "0.1.9"
version = "0.1.10"
dependencies = [
"anyhow",
"chrono",
@ -178,12 +178,12 @@ dependencies = [
[[package]]
name = "cloudflare-kv-proxy"
version = "0.1.3"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1896e84b3eba9e6957cdb2b8bc4bc43e1ee56ce688377b3f0570d8926e955b7"
checksum = "4d737ecb4574408dc3b5d75c1624e1967ba64af063f0b509b7f3859cfef14eb8"
dependencies = [
"coarsetime",
"hashlink 0.7.0",
"hashlink",
"parking_lot 0.12.1",
"reqwest",
"serde",
@ -347,7 +347,7 @@ dependencies = [
"cloudflare-kv-proxy",
"derive_more",
"futures",
"hashlink 0.8.1",
"hashlink",
"ipnet",
"lazy_static",
"once_cell",
@ -613,15 +613,6 @@ dependencies = [
"tracing",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
dependencies = [
"ahash",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
@ -631,22 +622,13 @@ dependencies = [
"ahash",
]
[[package]]
name = "hashlink"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf"
dependencies = [
"hashbrown 0.11.2",
]
[[package]]
name = "hashlink"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69fe1fcf8b4278d860ad0548329f892a3631fb63f82574df68275f34cdbe0ffa"
dependencies = [
"hashbrown 0.12.3",
"hashbrown",
]
[[package]]
@ -795,7 +777,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown 0.12.3",
"hashbrown",
]
[[package]]

View File

@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "bot"
version = "0.1.9"
version = "0.1.10"
[dependencies]
eh2telegraph = {path = "../eh2telegraph"}

View File

@ -155,11 +155,13 @@ where
) -> ControlFlow<()> {
match command {
AdminCommand::Delete(key) => {
let _ = self.synchronizer.delete_cache(&key).await;
let _ = bot
.send_message(msg.chat.id, escape(&format!("Key {key} deleted.")))
.reply_to_message_id(msg.id)
.await;
tokio::spawn(async move {
let _ = self.synchronizer.delete_cache(&key).await;
let _ = bot
.send_message(msg.chat.id, escape(&format!("Key {key} deleted.")))
.reply_to_message_id(msg.id)
.await;
});
ControlFlow::BREAK
}
}

View File

@ -7,7 +7,7 @@ version = "0.1.0"
again = {version = "0.1", default_features = false, features = ["rand"]}
anyhow = "1"
bytes = "1"
cloudflare-kv-proxy = "0.1"
cloudflare-kv-proxy = "0.2"
derive_more = {version = "0.99", features = ["from_str"]}
futures = "0.3"
hashlink = "0.8"

View File

@ -27,6 +27,7 @@ lazy_static::lazy_static! {
.with_max_retries(5)
.with_jitter(true);
}
const TIMEOUT: Duration = Duration::from_secs(30);
#[derive(Debug, Clone, Default)]
pub struct EHCollector {
@ -47,7 +48,11 @@ impl EHCollector {
.with_default_headers(request_headers)
.with_cf_resolve(&["e-hentai.org"])
.build(prefix),
raw_client: reqwest::Client::builder().user_agent(UA).build().unwrap(),
raw_client: reqwest::Client::builder()
.timeout(TIMEOUT)
.user_agent(UA)
.build()
.unwrap(),
}
}
@ -63,7 +68,11 @@ impl EHCollector {
.with_default_headers(request_headers)
.with_cf_resolve(&["e-hentai.org"])
.build_from_config()?,
raw_client: reqwest::Client::builder().user_agent(UA).build().unwrap(),
raw_client: reqwest::Client::builder()
.timeout(TIMEOUT)
.user_agent(UA)
.build()
.unwrap(),
})
}
}

View File

@ -29,6 +29,7 @@ lazy_static::lazy_static! {
.with_jitter(true);
}
const CONFIG_KEY: &str = "exhentai";
const TIMEOUT: Duration = Duration::from_secs(30);
#[derive(Debug, Clone)]
pub struct EXCollector {
@ -61,6 +62,7 @@ impl EXCollector {
reqwest::Client::builder()
.user_agent(UA)
.default_headers(request_headers.clone())
.timeout(TIMEOUT)
.build()
.expect("build reqwest client failed")
},

View File

@ -8,11 +8,13 @@
pub const UA: &str = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36";
const CONFIG_KEY: &str = "http";
const TIMTOUT: Duration = Duration::from_secs(30);
use std::{
net::{IpAddr, Ipv6Addr, SocketAddr},
ops::{Deref, DerefMut},
sync::Arc,
time::Duration,
};
use ipnet::Ipv6Net;
@ -139,7 +141,7 @@ impl GhostClient {
mapping: &[(&'static str, SocketAddr)],
headers: Option<header::HeaderMap>,
) -> reqwest::Client {
let mut builder = reqwest::Client::builder().user_agent(UA);
let mut builder = reqwest::Client::builder().timeout(TIMTOUT).user_agent(UA);
if let Some(headers) = headers {
builder = builder.default_headers(headers);

View File

@ -1,3 +1,5 @@
use std::time::Duration;
use reqwest::header::HeaderValue;
use crate::{
@ -6,6 +8,7 @@ use crate::{
};
const CONFIG_KEY: &str = "proxy";
const TIMEOUT: Duration = Duration::from_secs(30);
#[derive(serde::Deserialize, Clone, Debug, Default)]
struct ProxyConfig {
@ -39,6 +42,7 @@ impl ProxiedClient {
proxy,
inner: reqwest::Client::builder()
.user_agent(UA)
.timeout(TIMEOUT)
.build()
.expect("unable to build reqwest client"),
}
@ -60,6 +64,7 @@ impl ProxiedClient {
Self {
inner: reqwest::Client::builder()
.user_agent(UA)
.timeout(TIMEOUT)
.default_headers(headers)
.build()
.expect("unable to build reqwest client"),

View File

@ -9,6 +9,7 @@ use crate::config;
use super::KVStorage;
const CONFIG_KEY: &str = "worker_kv";
const TIMEOUT: Duration = Duration::from_secs(3);
#[derive(Debug, Deserialize)]
pub struct CFConfig {
@ -29,7 +30,7 @@ impl CFStorage {
expire: Duration,
) -> Result<Self, ClientError> {
Ok(Self(Arc::new(Client::new(
endpoint, token, cache_size, expire,
endpoint, token, TIMEOUT, cache_size, expire,
)?)))
}