11use std:: { collections:: HashMap , io:: Write , sync:: Arc , time:: Duration } ;
22
3+ use crate :: api:: utils:: CountedBody ;
34use axum:: { http:: StatusCode , response:: IntoResponse , Extension } ;
45use bytes:: { BufMut , Bytes , BytesMut } ;
56use compact_str:: { format_compact, ToCompactString } ;
7+ use corro_types:: persistent_gauge;
68use corro_types:: updates:: Handle ;
79use corro_types:: {
810 agent:: Agent ,
@@ -50,7 +52,7 @@ async fn sub_by_id(
5052 params : SubParams ,
5153 bcast_cache : & SharedMatcherBroadcastCache ,
5254 tripwire : Tripwire ,
53- ) -> hyper :: Response < hyper :: Body > {
55+ ) -> impl IntoResponse {
5456 let matcher_rx = bcast_cache. read ( ) . await . get ( & id) . and_then ( |tx| {
5557 subs. get ( & id) . map ( |matcher| {
5658 debug ! ( "found matcher by id {id}" ) ;
@@ -99,7 +101,9 @@ async fn sub_by_id(
99101 let query_hash = matcher. hash ( ) . to_owned ( ) ;
100102 tokio:: spawn ( catch_up_sub ( matcher, params, rx, evt_tx) ) ;
101103
102- let ( tx, body) = hyper:: Body :: channel ( ) ;
104+ let ( tx, body) = CountedBody :: channel (
105+ persistent_gauge ! ( "corro.api.active.streams" , "source" => "subscriptions" , "protocol" => "http" ) ,
106+ ) ;
103107
104108 tokio:: spawn ( forward_bytes_to_body_sender ( id, evt_rx, tx, tripwire) ) ;
105109
@@ -302,7 +306,7 @@ impl MatcherUpsertError {
302306 }
303307}
304308
305- impl From < MatcherUpsertError > for hyper:: Response < hyper:: Body > {
309+ impl From < MatcherUpsertError > for hyper:: Response < CountedBody < hyper:: Body > > {
306310 fn from ( value : MatcherUpsertError ) -> Self {
307311 hyper:: Response :: builder ( )
308312 . status ( value. status_code ( ) )
@@ -676,7 +680,7 @@ pub async fn api_v1_subs(
676680) -> impl IntoResponse {
677681 let stmt = match expand_sql ( & agent, & stmt) . await {
678682 Ok ( stmt) => stmt,
679- Err ( e) => return hyper:: Response :: < hyper:: Body > :: from ( e) ,
683+ Err ( e) => return hyper:: Response :: < CountedBody < hyper:: Body > > :: from ( e) ,
680684 } ;
681685
682686 info ! ( "Received subscription request for query: {stmt}" ) ;
@@ -695,10 +699,14 @@ pub async fn api_v1_subs(
695699
696700 let ( handle, maybe_created) = match upsert_res {
697701 Ok ( res) => res,
698- Err ( e) => return hyper:: Response :: < hyper:: Body > :: from ( MatcherUpsertError :: from ( e) ) ,
702+ Err ( e) => {
703+ return hyper:: Response :: < CountedBody < hyper:: Body > > :: from ( MatcherUpsertError :: from ( e) )
704+ }
699705 } ;
700706
701- let ( tx, body) = hyper:: Body :: channel ( ) ;
707+ let ( tx, body) = CountedBody :: channel (
708+ persistent_gauge ! ( "corro.api.active.streams" , "source" => "subscriptions" , "protocol" => "http" ) ,
709+ ) ;
702710 let ( forward_tx, forward_rx) = mpsc:: channel ( 10240 ) ;
703711
704712 tokio:: spawn ( forward_bytes_to_body_sender (
@@ -720,7 +728,7 @@ pub async fn api_v1_subs(
720728 . await
721729 {
722730 Ok ( id) => id,
723- Err ( e) => return hyper:: Response :: < hyper:: Body > :: from ( e) ,
731+ Err ( e) => return hyper:: Response :: < CountedBody < hyper:: Body > > :: from ( e) ,
724732 } ;
725733
726734 hyper:: Response :: builder ( )
0 commit comments