Skip to content

Commit

Permalink
Makes max connection configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
cuducos committed Oct 7, 2024
1 parent 7115c56 commit 0b6f91a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ use std::{env, time::Duration};

pub const DEFAULT_MAX_CONNECTIONS: u32 = 2;

pub fn max_connections() -> u32 {
if let Ok(txt) = env::var("MAX_CONNECTIONS") {
if let Ok(num) = txt.parse() {
return num;
}
}
DEFAULT_MAX_CONNECTIONS
}

pub fn from_env() -> Result<Pool<ConnectionManager<PgConnection>>> {
let url = env::var("DATABASE_URL").context("Missing `DATABASE_URL` environment variable")?;
let manager = ConnectionManager::<PgConnection>::new(url);
let pool = Pool::builder()
.max_size(DEFAULT_MAX_CONNECTIONS)
.max_size(max_connections())
.max_lifetime(Some(Duration::from_secs(300)))
.idle_timeout(Some(Duration::from_secs(60)))
.build(manager)
Expand Down
2 changes: 1 addition & 1 deletion src/telegram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async fn webhook(
) -> Result<()> {
let url = Url::parse(format!("https://{host}/webhook").as_str())?;
let opts = webhooks::Options::new((DEFAULT_HOST_IP, port).into(), url.clone())
.max_connections(db::DEFAULT_MAX_CONNECTIONS as u8);
.max_connections(db::max_connections() as u8);
dispatcher
.dispatch_with_listener(
webhooks::axum(bot, opts).await?,
Expand Down

0 comments on commit 0b6f91a

Please sign in to comment.