chiark / gitweb /
response plumbing
[hippotat.git] / server / suser.rs
index 48c8be5b8af186f89321994bd04b076c39c341de..98ee84cb23f1b293fb087d977ab74ae7751f02b5 100644 (file)
@@ -39,7 +39,7 @@ pub async fn run(global: Arc<Global>,
   };
 
   loop {
-    if let Some(ret) = {
+    if let Some(req) = {
       if ! downbound.is_empty() {
         outstanding.pop_front()
       } else if let Some((i,_)) = outstanding.iter().enumerate().find({
@@ -50,19 +50,45 @@ pub async fn run(global: Arc<Global>,
         None
       }
     } {
+      let mut build: FrameQueueBuf = default();
+
+      loop {
+        let next = if let Some(n) = downbound.peek_front() { n }
+                   else { break };
+        // Don't add 1 for the ESC since we will strip one
+        if build.len() + next.len() >= ic.max_batch_down.sat() { break }
+        build.esc_push(downbound.pop_front().unwrap());
+      }
+      if ! build.is_empty() {
+        // skip leading ESC
+        build.advance(1);
+      }
+
       let response = WebResponse {
-        data: Ok(default()),
+        data: Ok(build),
         warnings: default(),
       };
 
-      try_send_response(ret.reply_to, response);
+      try_send_response(req.reply_to, response);
+    }
+
+    let max = usize::saturating_mul(
+      ic.max_requests_outstanding.sat(),
+      ic.max_batch_down.sat(),
+    ).saturating_add(1 /* one boundary SLIP_ESC which we'll trim */);
+
+    while downbound.total_len() > max {
+      let _ = downbound.pop_front();
     }
 
     select!{
       biased;
 
-      
-      // xxx something something routed something
+      data = routed.recv() =>
+      {
+        let data = data.ok_or_else(|| anyhow!("routers shut down!"))?;
+        downbound.push_back(data.data);
+      },
 
       req = web.recv() =>
       {