Skip to content
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

speedup: add indexes on api key hash #1275

Merged
merged 2 commits into from
Apr 11, 2024
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
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
4 changes: 2 additions & 2 deletions server/src/operators/organization_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use actix_identity::Identity;
use actix_web::{web, HttpMessage, HttpRequest};
use diesel::prelude::*;
use diesel::{
result::DatabaseErrorKind, upsert::on_constraint, ExpressionMethods, JoinOnDsl,
NullableExpressionMethods, SelectableHelper, Table,
result::DatabaseErrorKind, ExpressionMethods, JoinOnDsl, NullableExpressionMethods,
SelectableHelper, Table,
};
use diesel_async::RunQueryDsl;
use itertools::Itertools;
Expand Down
Loading