Skip to content

Log the state of the connection pool once per minute #1606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/middleware/log_connection_pool_status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Log the current state of the database connection pool at most once per minute

use super::prelude::*;
use crate::app::App;

use conduit::Request;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex,
};
use std::time::{Duration, Instant};

#[derive(Clone)]
pub(crate) struct LogConnectionPoolStatus {
app: Arc<App>,
last_log_time: Arc<Mutex<Instant>>,
in_flight_requests: Arc<AtomicUsize>,
}

impl LogConnectionPoolStatus {
pub(crate) fn new(app: &Arc<App>) -> Self {
Self {
app: app.clone(),
last_log_time: Arc::new(Mutex::new(Instant::now())),
in_flight_requests: Arc::new(AtomicUsize::new(0)),
}
}
}

impl Middleware for LogConnectionPoolStatus {
fn before(&self, _: &mut dyn Request) -> Result<(), Box<dyn Error + Send>> {
let mut last_log_time = self.last_log_time.lock().unwrap_or_else(|e| e.into_inner());
let in_flight_requests = self.in_flight_requests.fetch_add(1, Ordering::SeqCst);
if last_log_time.elapsed() >= Duration::from_secs(60) {
*last_log_time = Instant::now();
println!(
"connection_pool_status=\"{:?}\" in_flight_requests={}",
self.app.diesel_database.state(),
in_flight_requests
);
}
Ok(())
}

fn after(
&self,
_: &mut dyn Request,
res: Result<Response, Box<dyn Error + Send>>,
) -> Result<Response, Box<dyn Error + Send>> {
self.in_flight_requests.fetch_sub(1, Ordering::SeqCst);
res
}
}
6 changes: 6 additions & 0 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use self::current_user::CurrentUser;
pub use self::debug::*;
pub use self::ember_index_rewrite::EmberIndexRewrite;
pub use self::head::Head;
use self::log_connection_pool_status::LogConnectionPoolStatus;
pub use self::security_headers::SecurityHeaders;
pub use self::static_or_continue::StaticOrContinue;

Expand All @@ -19,6 +20,7 @@ mod debug;
mod ember_index_rewrite;
mod ensure_well_formed_500;
mod head;
mod log_connection_pool_status;
mod log_request;
mod require_user_agent;
mod security_headers;
Expand Down Expand Up @@ -53,6 +55,10 @@ pub fn build_middleware(app: Arc<App>, endpoints: R404) -> MiddlewareBuilder {
m.add(DebugRequest);
}

if env::var_os("LOG_CONNECTION_POOL_STATUS").is_some() {
m.add(LogConnectionPoolStatus::new(&app));
}

m.add(ConditionalGet);

m.add(Cookie::new());
Expand Down