mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-22 00:59:07 -05:00
improvement: log slow requests that return zero results
This commit is contained in:
parent
b24d2f9aaa
commit
677b7d39e9
25
src/db.rs
25
src/db.rs
|
@ -636,9 +636,11 @@ pub async fn db_query(
|
|||
query_tx: tokio::sync::mpsc::Sender<QueryResult>,
|
||||
mut abandon_query_rx: tokio::sync::oneshot::Receiver<()>,
|
||||
) {
|
||||
task::spawn_blocking(move || {
|
||||
let mut row_count: usize = 0;
|
||||
let start = Instant::now();
|
||||
task::spawn_blocking(move || {
|
||||
debug!("moved DB query to thread in {:?}", start.elapsed());
|
||||
let start = Instant::now();
|
||||
let mut row_count: usize = 0;
|
||||
// generate SQL query
|
||||
let (q, p) = query_from_sub(&sub);
|
||||
debug!("SQL generated in {:?}", start.elapsed());
|
||||
|
@ -647,16 +649,25 @@ pub async fn db_query(
|
|||
// cutoff for displaying slow queries
|
||||
let slow_cutoff = Duration::from_millis(1000);
|
||||
let start = Instant::now();
|
||||
let mut slow_first_event;
|
||||
if let Ok(conn) = pool.get() {
|
||||
// execute the query. Don't cache, since queries vary so much.
|
||||
let mut stmt = conn.prepare(&q)?;
|
||||
let mut event_rows = stmt.query(rusqlite::params_from_iter(p))?;
|
||||
let mut first_result = true;
|
||||
while let Some(row) = event_rows.next()? {
|
||||
let first_event_elapsed = start.elapsed();
|
||||
slow_first_event = first_event_elapsed >= slow_cutoff;
|
||||
if first_result {
|
||||
let first_result_elapsed = start.elapsed();
|
||||
debug!(
|
||||
"first result in {:?} (cid: {}, sub: {:?})",
|
||||
first_event_elapsed, client_id, sub.id
|
||||
);
|
||||
first_result = false;
|
||||
}
|
||||
// logging for slow queries; show sub and SQL
|
||||
if first_result_elapsed >= slow_cutoff {
|
||||
//
|
||||
if slow_first_event {
|
||||
info!(
|
||||
"query req (slow): {:?} (cid: {}, sub: {:?})",
|
||||
sub, client_id, sub.id
|
||||
|
@ -679,12 +690,6 @@ pub async fn db_query(
|
|||
sub.id
|
||||
);
|
||||
}
|
||||
debug!(
|
||||
"first result in {:?} (cid: {}, sub: {:?})",
|
||||
first_result_elapsed, client_id, sub.id
|
||||
);
|
||||
first_result = false;
|
||||
}
|
||||
// check if this is still active
|
||||
// TODO: check every N rows
|
||||
if abandon_query_rx.try_recv().is_ok() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user