@@ -2,7 +2,7 @@ use crate::{
22 gist,
33 metrics:: {
44 track_metric_async, track_metric_force_endpoint_async, track_metric_no_request_async,
5- Endpoint , GenerateLabels , SuccessDetails ,
5+ Endpoint , GenerateLabels , SuccessDetails , DURATION_WS , LIVE_WS , UNAVAILABLE_WS ,
66 } ,
77 sandbox:: { self , Channel , Sandbox } ,
88 CachingSnafu , ClippyRequest , ClippyResponse , CompilationSnafu , CompileRequest , CompileResponse ,
@@ -15,7 +15,11 @@ use crate::{
1515} ;
1616use async_trait:: async_trait;
1717use axum:: {
18- extract:: { self , Extension , Path , TypedHeader } ,
18+ extract:: {
19+ self ,
20+ ws:: { WebSocket , WebSocketUpgrade } ,
21+ Extension , Path , TypedHeader ,
22+ } ,
1923 handler:: Handler ,
2024 headers:: { authorization:: Bearer , Authorization , CacheControl , ETag , IfNoneMatch } ,
2125 http:: { header, uri:: PathAndQuery , HeaderValue , Method , Request , StatusCode , Uri } ,
@@ -78,6 +82,8 @@ pub(crate) async fn serve(config: Config) {
7882 . route ( "/meta/gist" , post ( meta_gist_create) )
7983 . route ( "/meta/gist/:id" , get ( meta_gist_get) )
8084 . route ( "/metrics" , get ( metrics) )
85+ . route ( "/websocket" , get ( websocket) )
86+ . route ( "/nowebsocket" , post ( nowebsocket) )
8187 . layer ( Extension ( Arc :: new ( SandboxCache :: default ( ) ) ) )
8288 . layer ( Extension ( config. github_token ( ) ) ) ;
8389
@@ -386,6 +392,23 @@ async fn metrics(_: MetricsAuthorization) -> Result<Vec<u8>, StatusCode> {
386392 . map_err ( |_| StatusCode :: INTERNAL_SERVER_ERROR )
387393}
388394
395+ async fn websocket ( ws : WebSocketUpgrade ) -> impl IntoResponse {
396+ ws. on_upgrade ( handle_socket)
397+ }
398+
399+ async fn handle_socket ( mut socket : WebSocket ) {
400+ LIVE_WS . inc ( ) ;
401+ let start = Instant :: now ( ) ;
402+ while let Some ( Ok ( _msg) ) = socket. recv ( ) . await { }
403+ LIVE_WS . dec ( ) ;
404+ let elapsed = start. elapsed ( ) ;
405+ DURATION_WS . observe ( elapsed. as_secs_f64 ( ) ) ;
406+ }
407+
408+ async fn nowebsocket ( ) {
409+ UNAVAILABLE_WS . inc ( ) ;
410+ }
411+
389412#[ derive( Debug ) ]
390413struct MetricsAuthorization ;
391414
0 commit comments