Skip to content

Commit

Permalink
refactor(backend):use trait for rate limiting and thus simplify repre…
Browse files Browse the repository at this point in the history
…sentation layer by moving logic to utils(DA
  • Loading branch information
Eason0729 committed Jul 17, 2024
1 parent 5a1270d commit 80bcef0
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 320 deletions.
14 changes: 14 additions & 0 deletions 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 @@ -31,7 +31,7 @@ ip_network = { version = "0.4.1", features = ["serde"] }
opentelemetry = { version = "0.23.0", features = ["metrics"] }
opentelemetry_sdk = { version = "0.23.0", features = ["rt-tokio", "metrics"] }
opentelemetry-stdout = { version = "0.4.0", features = ["metrics"] }
opentelemetry-otlp = { version = "0.16.0", features = ["metrics"] }
opentelemetry-otlp = { version = "0.16.0", features = ["metrics", "tls-roots"] }
opentelemetry-semantic-conventions = "0.16.0"
tracing-opentelemetry = { version = "0.24.0", features = ["metrics"] }
tracing-core = "0.1.32"
Expand Down
6 changes: 5 additions & 1 deletion backend/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ entity-codegen:
sea-orm-cli migrate -u sqlite://database/backend.sqlite?mode=rwc
sea-orm-cli generate entity -u sqlite://database/backend.sqlite?mode=rwc -o src/pending

run:
dev:
just prepare
cargo run

run-release:
Expand All @@ -17,3 +18,6 @@ run-release:
ci-test:
just prepare
cargo test

setup-judger:
cd ../docker/dev && sudo docker compose --profile backend-dev up
1 change: 1 addition & 0 deletions backend/src/controller/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl TokenController {
token
}
None => {
// FIXME: this is cold branch!
let token: CachedToken = (token::Entity::find()
.filter(token::Column::Rand.eq(rand.to_vec()))
.one(self.db.deref())
Expand Down
61 changes: 15 additions & 46 deletions backend/src/endpoint/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ impl Announcement for ArcServer {
&self,
req: Request<ListAnnouncementRequest>,
) -> Result<Response<ListAnnouncementResponse>, Status> {
let (auth, req) = self
.parse_request_fn(req, |req| {
(req.size + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

req.bound_check()?;

Expand All @@ -84,8 +78,11 @@ impl Announcement for ArcServer {
};
let mut paginator = paginator.with_auth(&auth).with_db(&self.db);

let list = paginator.fetch(req.size, req.offset).await?;
let remain = paginator.remain().await?;
let list = paginator
.fetch(req.size, req.offset)
.in_current_span()
.await?;
let remain = paginator.remain().in_current_span().await?;

let paginator = paginator.into_inner();

Expand All @@ -97,10 +94,7 @@ impl Announcement for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn full_info(&self, req: Request<Id>) -> Result<Response<AnnouncementFullInfo>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

tracing::debug!(announcement_id = req.id);

Expand All @@ -119,10 +113,7 @@ impl Announcement for ArcServer {
&self,
req: Request<CreateAnnouncementRequest>,
) -> Result<Response<Id>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, perm) = auth.auth_or_guest()?;

req.bound_check()?;
Expand Down Expand Up @@ -159,10 +150,7 @@ impl Announcement for ArcServer {
&self,
req: Request<UpdateAnnouncementRequest>,
) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, _perm) = auth.auth_or_guest()?;

req.bound_check()?;
Expand Down Expand Up @@ -196,10 +184,7 @@ impl Announcement for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn remove(&self, req: Request<Id>) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

let result = Entity::write_filter(Entity::delete_by_id(Into::<i32>::into(req.id)), &auth)?
.exec(self.db.deref())
Expand All @@ -220,10 +205,7 @@ impl Announcement for ArcServer {
&self,
req: Request<AddAnnouncementToContestRequest>,
) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, perm) = auth.auth_or_guest()?;

if !perm.super_user() {
Expand Down Expand Up @@ -267,11 +249,7 @@ impl Announcement for ArcServer {
&self,
req: Request<AddAnnouncementToContestRequest>,
) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;

let (auth, req) = self.rate_limit(req).in_current_span().await?;
let mut announcement = Entity::write_by_id(req.announcement_id, &auth)?
.columns([Column::Id, Column::ContestId])
.one(self.db.deref())
Expand All @@ -293,10 +271,7 @@ impl Announcement for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn publish(&self, req: Request<Id>) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let perm = auth.user_perm();

tracing::debug!(id = req.id);
Expand Down Expand Up @@ -326,10 +301,7 @@ impl Announcement for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn unpublish(&self, req: Request<Id>) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let perm = auth.user_perm();

tracing::debug!(id = req.id);
Expand Down Expand Up @@ -362,10 +334,7 @@ impl Announcement for ArcServer {
&self,
req: Request<AddAnnouncementToContestRequest>,
) -> Result<Response<AnnouncementFullInfo>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

let parent: contest::IdModel =
contest::Entity::related_read_by_id(&auth, Into::<i32>::into(req.contest_id), &self.db)
Expand Down
33 changes: 6 additions & 27 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ impl Contest for ArcServer {
&self,
req: Request<ListContestRequest>,
) -> Result<Response<ListContestResponse>, Status> {
let (auth, req) = self
.parse_request_fn(req, |req| {
(req.size + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

req.bound_check()?;

Expand Down Expand Up @@ -101,10 +95,7 @@ impl Contest for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn full_info(&self, req: Request<Id>) -> Result<Response<ContestFullInfo>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

let query = Entity::find_by_id::<i32>(req.into()).filter(Column::Public.eq(true));
let model = query
Expand All @@ -118,10 +109,7 @@ impl Contest for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn create(&self, req: Request<CreateContestRequest>) -> Result<Response<Id>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, perm) = auth.auth_or_guest()?;

req.bound_check()?;
Expand Down Expand Up @@ -165,10 +153,7 @@ impl Contest for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn update(&self, req: Request<UpdateContestRequest>) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, perm) = auth.auth_or_guest()?;

req.bound_check()?;
Expand Down Expand Up @@ -224,10 +209,7 @@ impl Contest for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn remove(&self, req: Request<Id>) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

let result = Entity::write_filter(Entity::delete_by_id(Into::<i32>::into(req.id)), &auth)?
.exec(self.db.deref())
Expand All @@ -245,10 +227,7 @@ impl Contest for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn join(&self, req: Request<JoinContestRequest>) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, perm) = auth.auth_or_guest()?;

let model = Entity::read_filter(Entity::find_by_id(req.id), &auth)?
Expand Down
38 changes: 7 additions & 31 deletions backend/src/endpoint/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ impl Education for ArcServer {
&self,
req: Request<ListEducationRequest>,
) -> Result<Response<ListEducationResponse>, Status> {
let (auth, req) = self
.parse_request_fn(req, |req| {
(req.size + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

req.bound_check()?;

Expand Down Expand Up @@ -76,10 +70,7 @@ impl Education for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn create(&self, req: Request<CreateEducationRequest>) -> Result<Response<Id>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, perm) = auth.auth_or_guest()?;

req.bound_check()?;
Expand Down Expand Up @@ -113,10 +104,7 @@ impl Education for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn update(&self, req: Request<UpdateEducationRequest>) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, _perm) = auth.auth_or_guest()?;

req.bound_check()?;
Expand Down Expand Up @@ -150,10 +138,7 @@ impl Education for ArcServer {
}
#[instrument(skip_all, level = "debug")]
async fn remove(&self, req: Request<Id>) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

let result = Entity::write_filter(Entity::delete_by_id(Into::<i32>::into(req.id)), &auth)?
.exec(self.db.deref())
Expand All @@ -174,10 +159,7 @@ impl Education for ArcServer {
&self,
req: Request<AddEducationToProblemRequest>,
) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, perm) = auth.auth_or_guest()?;

let (problem, model) = tokio::try_join!(
Expand Down Expand Up @@ -217,10 +199,7 @@ impl Education for ArcServer {
&self,
req: Request<AddEducationToProblemRequest>,
) -> Result<Response<()>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

let mut model = Entity::write_by_id(req.problem_id, &auth)?
.columns([Column::Id, Column::ProblemId])
Expand All @@ -247,10 +226,7 @@ impl Education for ArcServer {
&self,
req: Request<AddEducationToProblemRequest>,
) -> Result<Response<EducationFullInfo>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (auth, req) = self.rate_limit(req).in_current_span().await?;

let parent: problem::IdModel =
problem::Entity::related_read_by_id(&auth, Into::<i32>::into(req.problem_id), &self.db)
Expand Down
Loading

0 comments on commit 80bcef0

Please sign in to comment.