From 1c1b91500bc3b57de7703d41421d23a96d58801e Mon Sep 17 00:00:00 2001 From: Dens Sumesh Date: Thu, 11 Apr 2024 00:44:14 -0700 Subject: [PATCH] speedup: add indexes on api key hash --- .../down.sql | 3 +++ .../2024-04-11-074046_add-indexes-for-api-key/up.sql | 3 +++ server/src/af_middleware/auth_middleware.rs | 11 +++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 server/migrations/2024-04-11-074046_add-indexes-for-api-key/down.sql create mode 100644 server/migrations/2024-04-11-074046_add-indexes-for-api-key/up.sql diff --git a/server/migrations/2024-04-11-074046_add-indexes-for-api-key/down.sql b/server/migrations/2024-04-11-074046_add-indexes-for-api-key/down.sql new file mode 100644 index 0000000000..c573a9571f --- /dev/null +++ b/server/migrations/2024-04-11-074046_add-indexes-for-api-key/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` +DROP INDEX IF EXISTS user_api_key_api_key_hash_index; +DROP INDEX IF EXISTS user_api_key_blake3_hash_index; \ No newline at end of file diff --git a/server/migrations/2024-04-11-074046_add-indexes-for-api-key/up.sql b/server/migrations/2024-04-11-074046_add-indexes-for-api-key/up.sql new file mode 100644 index 0000000000..d58b7aaac0 --- /dev/null +++ b/server/migrations/2024-04-11-074046_add-indexes-for-api-key/up.sql @@ -0,0 +1,3 @@ +-- Your SQL goes here +CREATE INDEX IF NOT EXISTS user_api_key_api_key_hash_index ON user_api_key (api_key_hash); +CREATE INDEX IF NOT EXISTS user_api_key_blake3_hash_index ON user_api_key (blake3_hash); diff --git a/server/src/af_middleware/auth_middleware.rs b/server/src/af_middleware/auth_middleware.rs index e08af96969..9466acbd88 100644 --- a/server/src/af_middleware/auth_middleware.rs +++ b/server/src/af_middleware/auth_middleware.rs @@ -16,6 +16,7 @@ use actix_web::{ web, Error, FromRequest, HttpMessage, HttpRequest, }; use futures_util::future::LocalBoxFuture; +use sentry::Transaction; use std::{ future::{ready, Ready}, rc::Rc, @@ -48,7 +49,7 @@ where let get_user_span = transaction.start_child("get_user", "Getting user"); let (http_req, pl) = req.parts_mut(); - let user = get_user(http_req, pl).await; + let user = get_user(http_req, pl, transaction.clone()).await; if let Some(ref user) = user { req.extensions_mut().insert(user.clone()); }; @@ -138,7 +139,9 @@ where } } -async fn get_user(req: &HttpRequest, pl: &mut Payload) -> Option { +async fn get_user(req: &HttpRequest, pl: &mut Payload, tx: Transaction) -> Option { + let get_user_from_identity_span = + tx.start_child("get_user_from_identity", "Getting user from identity"); if let Ok(identity) = Identity::from_request(req, pl).into_inner() { if let Ok(user_json) = identity.id() { if let Ok(user) = serde_json::from_str::(&user_json) { @@ -146,6 +149,7 @@ async fn get_user(req: &HttpRequest, pl: &mut Payload) -> Option { } } } + get_user_from_identity_span.finish(); if let Some(authen_header) = req.headers().get("Authorization") { if let Ok(authen_header) = authen_header.to_str() { @@ -182,12 +186,15 @@ async fn get_user(req: &HttpRequest, pl: &mut Payload) -> Option { } } + let get_user_from_api_key_span = + tx.start_child("get_user_from_api_key", "Getting user from api key"); //TODO: Cache the api key in redis if let Some(pool) = req.app_data::>() { if let Ok(user) = get_user_from_api_key_query(authen_header, pool).await { return Some(user); } } + get_user_from_api_key_span.finish(); } }