Skip to content

Commit

Permalink
speedup: add indexes on api key hash
Browse files Browse the repository at this point in the history
  • Loading branch information
densumesh committed Apr 11, 2024
1 parent 3250169 commit 1c1b915
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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);
11 changes: 9 additions & 2 deletions server/src/af_middleware/auth_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
};
Expand Down Expand Up @@ -138,14 +139,17 @@ where
}
}

async fn get_user(req: &HttpRequest, pl: &mut Payload) -> Option<LoggedUser> {
async fn get_user(req: &HttpRequest, pl: &mut Payload, tx: Transaction) -> Option<LoggedUser> {
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::<LoggedUser>(&user_json) {
return Some(user);
}
}
}
get_user_from_identity_span.finish();

if let Some(authen_header) = req.headers().get("Authorization") {
if let Ok(authen_header) = authen_header.to_str() {
Expand Down Expand Up @@ -182,12 +186,15 @@ async fn get_user(req: &HttpRequest, pl: &mut Payload) -> Option<LoggedUser> {
}
}

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::<web::Data<Pool>>() {
if let Ok(user) = get_user_from_api_key_query(authen_header, pool).await {
return Some(user);
}
}
get_user_from_api_key_span.finish();
}
}

Expand Down

0 comments on commit 1c1b915

Please sign in to comment.