Make client timeout configurable

This commit is contained in:
asonix 2023-07-25 16:06:56 -05:00
parent dfbd5c9035
commit 970672a392
5 changed files with 23 additions and 4 deletions

View file

@ -105,6 +105,7 @@ LOCAL_DOMAINS=masto.asonix.dog
LOCAL_BLURB="<p>Welcome to my cool relay where I have cool relay things happening. I hope you enjoy your stay!</p>" LOCAL_BLURB="<p>Welcome to my cool relay where I have cool relay things happening. I hope you enjoy your stay!</p>"
PROMETHEUS_ADDR=0.0.0.0 PROMETHEUS_ADDR=0.0.0.0
PROMETHEUS_PORT=9000 PROMETHEUS_PORT=9000
CLIENT_TIMEOUT=10
CLIENT_POOL_SIZE=20 CLIENT_POOL_SIZE=20
DELIVER_CONCURRENCY=8 DELIVER_CONCURRENCY=8
``` ```
@ -156,6 +157,9 @@ Optional - description for the relay
Optional - Address to bind to for serving the prometheus scrape endpoint Optional - Address to bind to for serving the prometheus scrape endpoint
##### `PROMETHEUS_PORT` ##### `PROMETHEUS_PORT`
Optional - Port to bind to for serving the prometheus scrape endpoint Optional - Port to bind to for serving the prometheus scrape endpoint
##### `CLIENT_TIMEOUT`
Optional - How long the relay will hold open a connection (in seconds) to a remote server during
fetches and deliveries. This defaults to 10
##### `CLIENT_POOL_SIZE` ##### `CLIENT_POOL_SIZE`
Optional - How many connections the relay should maintain per thread. This value will be multiplied Optional - How many connections the relay should maintain per thread. This value will be multiplied
by the number of cores available to the relay. This defaults to 20, so a 4-core machine will have a by the number of cores available to the relay. This defaults to 20, so a 4-core machine will have a

View file

@ -46,6 +46,7 @@ pub(crate) struct ParsedConfig {
prometheus_addr: Option<IpAddr>, prometheus_addr: Option<IpAddr>,
prometheus_port: Option<u16>, prometheus_port: Option<u16>,
deliver_concurrency: u64, deliver_concurrency: u64,
client_timeout: u64,
client_pool_size: usize, client_pool_size: usize,
} }
@ -71,6 +72,7 @@ pub struct Config {
local_blurb: Option<String>, local_blurb: Option<String>,
prometheus_config: Option<PrometheusConfig>, prometheus_config: Option<PrometheusConfig>,
deliver_concurrency: u64, deliver_concurrency: u64,
client_timeout: u64,
client_pool_size: usize, client_pool_size: usize,
} }
@ -140,6 +142,7 @@ impl std::fmt::Debug for Config {
.field("local_blurb", &self.local_blurb) .field("local_blurb", &self.local_blurb)
.field("prometheus_config", &self.prometheus_config) .field("prometheus_config", &self.prometheus_config)
.field("deliver_concurrency", &self.deliver_concurrency) .field("deliver_concurrency", &self.deliver_concurrency)
.field("client_timeout", &self.client_timeout)
.field("client_pool_size", &self.client_pool_size) .field("client_pool_size", &self.client_pool_size)
.finish() .finish()
} }
@ -171,6 +174,7 @@ impl Config {
.set_default("prometheus_addr", None as Option<&str>)? .set_default("prometheus_addr", None as Option<&str>)?
.set_default("prometheus_port", None as Option<u16>)? .set_default("prometheus_port", None as Option<u16>)?
.set_default("deliver_concurrency", 8u64)? .set_default("deliver_concurrency", 8u64)?
.set_default("client_timeout", 10u64)?
.set_default("client_pool_size", 20u64)? .set_default("client_pool_size", 20u64)?
.add_source(Environment::default()) .add_source(Environment::default())
.build()?; .build()?;
@ -244,10 +248,15 @@ impl Config {
local_blurb: config.local_blurb, local_blurb: config.local_blurb,
prometheus_config, prometheus_config,
deliver_concurrency: config.deliver_concurrency, deliver_concurrency: config.deliver_concurrency,
client_timeout: config.client_timeout,
client_pool_size: config.client_pool_size, client_pool_size: config.client_pool_size,
}) })
} }
pub(crate) fn client_timeout(&self) -> u64 {
self.client_timeout
}
pub(crate) fn deliver_concurrency(&self) -> u64 { pub(crate) fn deliver_concurrency(&self) -> u64 {
self.deliver_concurrency self.deliver_concurrency
} }

View file

@ -48,6 +48,7 @@ impl State {
self.breakers.clone(), self.breakers.clone(),
self.last_online.clone(), self.last_online.clone(),
config.client_pool_size(), config.client_pool_size(),
config.client_timeout(),
) )
} }

View file

@ -148,7 +148,11 @@ fn client_main(config: Config, args: Args) -> JoinHandle<Result<(), anyhow::Erro
} }
async fn do_client_main(config: Config, args: Args) -> Result<(), anyhow::Error> { async fn do_client_main(config: Config, args: Args) -> Result<(), anyhow::Error> {
let client = requests::build_client(&config.user_agent(), config.client_pool_size()); let client = requests::build_client(
&config.user_agent(),
config.client_pool_size(),
config.client_timeout(),
);
if !args.blocks().is_empty() || !args.allowed().is_empty() { if !args.blocks().is_empty() || !args.allowed().is_empty() {
if args.undo() { if args.undo() {

View file

@ -166,7 +166,7 @@ thread_local! {
static CLIENT: std::cell::OnceCell<Client> = std::cell::OnceCell::new(); static CLIENT: std::cell::OnceCell<Client> = std::cell::OnceCell::new();
} }
pub(crate) fn build_client(user_agent: &str, pool_size: usize) -> Client { pub(crate) fn build_client(user_agent: &str, pool_size: usize, timeout_seconds: u64) -> Client {
CLIENT.with(|client| { CLIENT.with(|client| {
client client
.get_or_init(|| { .get_or_init(|| {
@ -176,7 +176,7 @@ pub(crate) fn build_client(user_agent: &str, pool_size: usize) -> Client {
.connector(connector) .connector(connector)
.wrap(Tracing) .wrap(Tracing)
.add_default_header(("User-Agent", user_agent.to_string())) .add_default_header(("User-Agent", user_agent.to_string()))
.timeout(Duration::from_secs(15)) .timeout(Duration::from_secs(timeout_seconds))
.finish() .finish()
}) })
.clone() .clone()
@ -191,10 +191,11 @@ impl Requests {
breakers: Breakers, breakers: Breakers,
last_online: Arc<LastOnline>, last_online: Arc<LastOnline>,
pool_size: usize, pool_size: usize,
timeout_seconds: u64,
) -> Self { ) -> Self {
Requests { Requests {
pool_size, pool_size,
client: build_client(&user_agent, pool_size), client: build_client(&user_agent, pool_size, timeout_seconds),
key_id, key_id,
user_agent, user_agent,
private_key, private_key,