1
- use std:: panic:: AssertUnwindSafe ;
1
+ use std:: { panic:: AssertUnwindSafe , sync :: atomic :: Ordering , time :: Duration } ;
2
2
3
3
use actix_web:: { post, web, Responder } ;
4
4
use hodu_core:: { mark, MarkParams } ;
5
5
6
6
use futures:: FutureExt ;
7
7
8
- use crate :: api:: judge:: {
9
- error:: JudgeError ,
10
- schema:: { CodeSubmission , MarkResponse } ,
8
+ use crate :: {
9
+ api:: judge:: {
10
+ error:: JudgeError ,
11
+ schema:: { CodeSubmission , MarkResponse } ,
12
+ } ,
13
+ MarkCounter ,
11
14
} ;
12
15
16
+ const MAX_SUBMISSIONS : u32 = 20 ;
17
+
13
18
#[ post( "/submit" ) ]
14
19
async fn submit_code (
15
20
submission : Result < web:: Json < CodeSubmission > , actix_web:: Error > ,
21
+ counter : web:: Data < MarkCounter > ,
16
22
) -> Result < impl Responder , JudgeError > {
17
23
let submission = submission. map_err ( |e| JudgeError :: PayloadParseError ( e) ) ?;
18
24
tracing:: info!(
@@ -21,6 +27,14 @@ async fn submit_code(
21
27
submission. language
22
28
) ;
23
29
30
+ let mut current_count = counter. count . load ( Ordering :: SeqCst ) ;
31
+ while current_count >= MAX_SUBMISSIONS {
32
+ tracing:: info!( "Max submissions reached, waiting for a slot..." ) ;
33
+ tokio:: time:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
34
+ current_count = counter. count . load ( Ordering :: SeqCst ) ;
35
+ }
36
+ let previous_count = counter. count . fetch_add ( 1 , Ordering :: SeqCst ) ;
37
+ tracing:: info!( "Current count: {}" , previous_count + 1 ) ;
24
38
let output = AssertUnwindSafe ( mark ( MarkParams {
25
39
language : & submission. language . clone ( ) . into ( ) ,
26
40
code : & submission. code ,
@@ -31,7 +45,11 @@ async fn submit_code(
31
45
} ) )
32
46
. catch_unwind ( )
33
47
. await
34
- . map_err ( |_| JudgeError :: HoduCoreError ) ?;
48
+ . map_err ( |_| {
49
+ counter. count . fetch_sub ( 1 , Ordering :: SeqCst ) ;
50
+ JudgeError :: HoduCoreError
51
+ } ) ?;
52
+ counter. count . fetch_sub ( 1 , Ordering :: SeqCst ) ;
35
53
36
54
Ok ( web:: Json (
37
55
serde_json:: to_value ( MarkResponse :: new ( & output, & submission. fields ) ) . unwrap ( ) ,
0 commit comments