Skip to content

Commit

Permalink
test: 🧪 find cause of UB
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Dec 7, 2023
1 parent 76e811a commit b59d49b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 27 deletions.
80 changes: 70 additions & 10 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ version = "0.1.40"
features =["async-await", "log"]

[dependencies.tokio]
version = "1.28.0"
version = "1.34.0"
features =["macros", "rt-multi-thread", "full","time"]

[dependencies.sea-orm]
Expand Down
31 changes: 18 additions & 13 deletions backend/src/controller/judger/route/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// TODO!: health check, circular reference counter
pub mod direct;
pub mod swarm;

use super::Error;
use std::{
collections::VecDeque,
mem::MaybeUninit,
ops::DerefMut,
sync::{
atomic::{AtomicIsize, Ordering},
Expand All @@ -26,19 +26,19 @@ use crate::{
// introduce routing layer error

const HEALTHY_THRESHOLD: isize = 100;
type JudgerIntercept =
JudgerClient<service::interceptor::InterceptedService<transport::Channel, AuthInterceptor>>;
type JudgerIntercept = JudgerClient<
service::interceptor::InterceptedService<transport::Channel, BasicAuthInterceptor>,
>;

pub struct AuthInterceptor {
pub struct BasicAuthInterceptor {
secret: Option<String>,
}

impl Interceptor for AuthInterceptor {
impl Interceptor for BasicAuthInterceptor {
fn call(&mut self, mut req: tonic::Request<()>) -> Result<tonic::Request<()>, Status> {
match &self.secret {
Some(secret) => {
let token: metadata::MetadataValue<_> =
format!("basic {}", secret).parse().unwrap();
let token = secret.parse().unwrap();
req.metadata_mut().insert("Authorization", token);
Ok(req)
}
Expand All @@ -60,8 +60,8 @@ impl ConnectionDetail {
.connect()
.await?;

let interceptor = AuthInterceptor {
secret: self.secret.clone(),
let interceptor = BasicAuthInterceptor {
secret: self.secret.as_ref().map(|x| ["basic ", x].concat()),
};

Ok(JudgerClient::with_interceptor(channel, interceptor))
Expand Down Expand Up @@ -145,7 +145,7 @@ pub struct Router {

impl Router {
// skip because config contain basic auth secret
#[tracing::instrument(level = "debug",skip_all)]
#[tracing::instrument(level = "debug", skip_all)]
pub async fn new(config: Vec<JudgerConfig>) -> Result<Arc<Self>, Error> {
let self_ = Arc::new(Self {
routing_table: Map::new(),
Expand Down Expand Up @@ -206,10 +206,11 @@ pub struct Upstream {
impl Upstream {
async fn new(detail: ConnectionDetail) -> Result<(Arc<Self>, Vec<(Uuid, LangInfo)>), Error> {
let mut client = detail.connect().await?;
let info = client.judger_info(()).await?.into_inner();
let info = client.judger_info(()).await?;
let langs = info.into_inner().langs.list;

let mut result = Vec::new();
for lang in info.langs.list.into_iter() {
for lang in langs.into_iter() {
let uuid = match Uuid::parse_str(&lang.lang_uid) {
Ok(x) => x,
Err(err) => {
Expand All @@ -219,10 +220,14 @@ impl Upstream {
};
result.push((uuid, lang));
}

let clients = Queue::new();
clients.push(client);

Ok((
Arc::new(Self {
healthy: AtomicIsize::new(HEALTHY_THRESHOLD),
clients: Queue::new(),
clients,
connection: detail,
}),
result,
Expand Down
8 changes: 6 additions & 2 deletions backend/src/endpoint/util/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ where
let sort = match pager.sort {
RawSearchDep::Text(x) => SearchDep::Text(x),
RawSearchDep::Column(sort_by, reverse) => {
let sort_by = sort_by.try_into() .map_err(|_|Error::PaginationError("Pager reconstruction failed"))?;
let sort_by = sort_by
.try_into()
.map_err(|_| Error::PaginationError("Pager reconstruction failed"))?;
SearchDep::Column(sort_by, reverse)
}
RawSearchDep::Parent(x) => SearchDep::Parent(x),
Expand Down Expand Up @@ -284,7 +286,9 @@ where
let sort = match pager.sort {
RawSearchDep::Text(x) => SearchDep::Text(x),
RawSearchDep::Column(sort_by, reverse) => {
let sort_by = sort_by.try_into() .map_err(|_|Error::PaginationError("Pager reconstruction failed"))?;
let sort_by = sort_by
.try_into()
.map_err(|_| Error::PaginationError("Pager reconstruction failed"))?;
SearchDep::Column(sort_by, reverse)
}
RawSearchDep::Parent(_) => {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/init/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::Arc};
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use tokio::{fs, io::AsyncReadExt};
Expand Down

0 comments on commit b59d49b

Please sign in to comment.