diff --git a/backend/src/controller/judger/mod.rs b/backend/src/controller/judger/mod.rs index 7cfe34d1..e962c648 100644 --- a/backend/src/controller/judger/mod.rs +++ b/backend/src/controller/judger/mod.rs @@ -17,7 +17,7 @@ use crate::{ backend::{submit_status, JudgeResult as BackendResult, PlaygroundResult, SubmitStatus}, judger::*, }, - init::{db::DB}, + init::db::DB, }; use self::{ @@ -228,11 +228,7 @@ impl JudgerController { let submit_id = submit_model.id.as_ref().to_owned(); let tx = self.pubsub.publish(submit_id); - let scores = testcases - .iter() - .rev() - .map(|x| x.score) - .collect::>(); + let scores = testcases.iter().rev().map(|x| x.score).collect::>(); let tests = testcases .into_iter() diff --git a/backend/src/controller/token.rs b/backend/src/controller/token.rs index 1e99f68b..d715fcce 100644 --- a/backend/src/controller/token.rs +++ b/backend/src/controller/token.rs @@ -7,7 +7,7 @@ use sea_orm::{ }; use std::sync::Arc; use tokio::time; -use tracing::{instrument, Span}; +use tracing::{instrument, Instrument, Span}; use crate::{init::db::DB, report_internal}; @@ -149,25 +149,32 @@ impl TokenController { #[cfg(not(feature = "single-instance"))] let cache_result: Option = None; - match cache_result { - Some(token_) => { - token = token_; + let token = match cache_result { + Some(token) => { + tracing::trace!(user_id = token.user_id, "cache_hit"); + token } None => { token = (token::Entity::find() .filter(token::Column::Rand.eq(rand.to_vec())) .one(db) + .in_current_span() .await? .ok_or(Error::NonExist)?) .into(); - if token.expiry < now { - return Err(Error::Expired); - } + tracing::trace!(user_id = token.user_id, "cache_missed"); #[cfg(feature = "single-instance")] self.cache.insert(rand, token.clone()); + + token } + }; + + if token.expiry < now { + tracing::debug!(user_id = token.user_id, "token expired"); + return Err(Error::Expired); } Ok((token.user_id, UserPermBytes(token.permission))) diff --git a/backend/src/endpoint/token.rs b/backend/src/endpoint/token.rs index f450692a..6efef3eb 100644 --- a/backend/src/endpoint/token.rs +++ b/backend/src/endpoint/token.rs @@ -9,6 +9,9 @@ use crate::grpc::into_prost; use entity::token::*; use entity::*; +use tracing::Level; + +const TOKEN_LIMIT: u64 = 32; impl From for Token { fn from(value: String) -> Self { @@ -37,6 +40,7 @@ impl TokenSet for Arc { let tokens = Entity::find() .filter(Column::UserId.eq(user_id)) + .limit(TOKEN_LIMIT) .all(db) .await .map_err(Into::::into)?; @@ -111,6 +115,9 @@ impl TokenSet for Arc { let token = x.to_str().unwrap(); self.token.remove(token.to_string()).await?; + tracing::event!(Level::TRACE, token = token); + + return Ok(Response::new(())); } Err(Error::Unauthenticated.into()) diff --git a/backend/src/endpoint/util/controller.rs b/backend/src/endpoint/util/controller.rs index 69a4a5bd..da453a1e 100644 --- a/backend/src/endpoint/util/controller.rs +++ b/backend/src/endpoint/util/controller.rs @@ -1,24 +1,30 @@ -use tracing::{span, Level}; +use tracing::{instrument, span, Level}; use crate::server::Server; use super::auth::Auth; impl Server { + #[instrument(skip_all, level = "debug")] pub async fn parse_request( &self, request: tonic::Request, ) -> Result<(Auth, T), tonic::Status> { - let span = span!(Level::INFO,"token_verify",addr=?request.remote_addr()); - let _ = span.enter(); - + if let Some(addr) = request.remote_addr() { + tracing::event!(Level::DEBUG, addr = addr.to_string()); + } let (meta, _, payload) = request.into_parts(); if let Some(x) = meta.get("token") { let token = x.to_str().unwrap(); - Ok((Auth::User(self.token.verify(token).await?), payload)) + let user = self.token.verify(token).await?; + + tracing::event!(Level::DEBUG, user_id = user.0); + + Ok((Auth::User(user), payload)) } else { + tracing::trace!("token not found in metadata"); Ok((Auth::Guest, payload)) } } diff --git a/backend/src/init/db.rs b/backend/src/init/db.rs index bfd3ba3f..4523c15e 100644 --- a/backend/src/init/db.rs +++ b/backend/src/init/db.rs @@ -1,10 +1,6 @@ - - - use ring::digest; use sea_orm::{ - ActiveModelTrait, ActiveValue, Database, DatabaseConnection, EntityTrait, - PaginatorTrait, + ActiveModelTrait, ActiveValue, Database, DatabaseConnection, EntityTrait, PaginatorTrait, }; use tokio::sync::OnceCell;