1- use std:: collections:: VecDeque ;
21use std:: mem;
32use std:: pin:: { pin, Pin } ;
43use std:: time:: Duration ;
@@ -16,7 +15,8 @@ use scopeguard::ScopeGuard;
1615use serde:: Deserialize ;
1716use spacetimedb:: client:: messages:: { serialize, IdentityTokenMessage , SerializableMessage , SerializeBuffer } ;
1817use spacetimedb:: client:: {
19- ClientActorId , ClientConfig , ClientConnection , DataMessage , MessageHandleError , MeteredReceiver , Protocol ,
18+ ClientActorId , ClientConfig , ClientConnection , DataMessage , MessageHandleError , MeteredDeque , MeteredReceiver ,
19+ Protocol ,
2020} ;
2121use spacetimedb:: execution_context:: WorkloadType ;
2222use spacetimedb:: host:: module_host:: ClientConnectedError ;
@@ -210,6 +210,8 @@ async fn ws_client_actor_inner(
210210 let mut liveness_check_interval = tokio:: time:: interval ( LIVELINESS_TIMEOUT ) ;
211211 let mut got_pong = true ;
212212
213+ let addr = client. module . info ( ) . database_identity ;
214+
213215 // Build a queue of incoming messages to handle, to be processed one at a time,
214216 // in the order they're received.
215217 //
@@ -223,29 +225,14 @@ async fn ws_client_actor_inner(
223225 // `select!` for examples of how to do this.
224226 //
225227 // TODO: do we want this to have a fixed capacity? or should it be unbounded
226- let mut message_queue = VecDeque :: < ( DataMessage , Instant ) > :: new ( ) ;
228+ let mut message_queue = MeteredDeque :: < ( DataMessage , Instant ) > :: new (
229+ WORKER_METRICS . total_incoming_queue_length . with_label_values ( & addr) ,
230+ ) ;
227231 let mut current_message = pin ! ( MaybeDone :: Gone ) ;
228232
229233 let mut closed = false ;
230234 let mut rx_buf = Vec :: new ( ) ;
231235
232- let addr = client. module . info ( ) . database_identity ;
233-
234- // Grab handles on the total incoming and outgoing queue length metrics,
235- // which we'll increment and decrement as we push into and pull out of those queues.
236- // Note that `total_outgoing_queue_length` is incremented separately,
237- // by `ClientConnectionSender::send` in core/src/client/client_connection.rs;
238- // we're only responsible for decrementing that one.
239- // Also note that much care must be taken to clean up these metrics when the connection closes!
240- // Any path which exits this function must decrement each of these metrics
241- // by the number of messages still waiting in this client's queue,
242- // or else they will grow without bound as clients disconnect, and be useless.
243- let incoming_queue_length_metric = WORKER_METRICS . total_incoming_queue_length . with_label_values ( & addr) ;
244-
245- let clean_up_metrics = |message_queue : & VecDeque < ( DataMessage , Instant ) > | {
246- incoming_queue_length_metric. sub ( message_queue. len ( ) as _ ) ;
247- } ;
248-
249236 let mut msg_buffer = SerializeBuffer :: new ( client. config ) ;
250237 loop {
251238 rx_buf. clear ( ) ;
@@ -255,7 +242,6 @@ async fn ws_client_actor_inner(
255242 }
256243 if let MaybeDone :: Gone = * current_message {
257244 if let Some ( ( message, timer) ) = message_queue. pop_front ( ) {
258- incoming_queue_length_metric. dec ( ) ;
259245 let client = client. clone ( ) ;
260246 let fut = async move { client. handle_message ( message, timer) . await } ;
261247 current_message. set ( MaybeDone :: Future ( fut) ) ;
@@ -284,7 +270,6 @@ async fn ws_client_actor_inner(
284270 }
285271 // the client sent us a close frame
286272 None => {
287- clean_up_metrics( & message_queue) ;
288273 break
289274 } ,
290275 } ,
@@ -376,7 +361,6 @@ async fn ws_client_actor_inner(
376361 } else {
377362 // the client never responded to our ping; drop them without trying to send them a Close
378363 log:: warn!( "client {} timed out" , client. id) ;
379- clean_up_metrics( & message_queue) ;
380364 break ;
381365 }
382366 }
@@ -391,7 +375,6 @@ async fn ws_client_actor_inner(
391375 match message {
392376 Item :: Message ( ClientMessage :: Message ( message) ) => {
393377 let timer = Instant :: now ( ) ;
394- incoming_queue_length_metric. inc ( ) ;
395378 message_queue. push_back ( ( message, timer) )
396379 }
397380 Item :: HandleResult ( res) => {
0 commit comments