Skip to content

Commit

Permalink
feat: 🎨 improve log
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Dec 8, 2023
1 parent abce3eb commit 441f60e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
8 changes: 2 additions & 6 deletions backend/src/controller/judger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
backend::{submit_status, JudgeResult as BackendResult, PlaygroundResult, SubmitStatus},
judger::*,
},
init::{db::DB},
init::db::DB,
};

use self::{
Expand Down Expand Up @@ -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::<Vec<_>>();
let scores = testcases.iter().rev().map(|x| x.score).collect::<Vec<_>>();

let tests = testcases
.into_iter()
Expand Down
21 changes: 14 additions & 7 deletions backend/src/controller/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -149,25 +149,32 @@ impl TokenController {
#[cfg(not(feature = "single-instance"))]
let cache_result: Option<CachedToken> = 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)))
Expand Down
7 changes: 7 additions & 0 deletions backend/src/endpoint/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use crate::grpc::into_prost;

use entity::token::*;
use entity::*;
use tracing::Level;

const TOKEN_LIMIT: u64 = 32;

impl From<String> for Token {
fn from(value: String) -> Self {
Expand Down Expand Up @@ -37,6 +40,7 @@ impl TokenSet for Arc<Server> {

let tokens = Entity::find()
.filter(Column::UserId.eq(user_id))
.limit(TOKEN_LIMIT)
.all(db)
.await
.map_err(Into::<Error>::into)?;
Expand Down Expand Up @@ -111,6 +115,9 @@ impl TokenSet for Arc<Server> {
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())
Expand Down
16 changes: 11 additions & 5 deletions backend/src/endpoint/util/controller.rs
Original file line number Diff line number Diff line change
@@ -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<T: Send>(
&self,
request: tonic::Request<T>,
) -> 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))
}
}
Expand Down
6 changes: 1 addition & 5 deletions backend/src/init/db.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 441f60e

Please sign in to comment.