chiark / gitweb /
response plumbing
[hippotat.git] / server / suser.rs
index dd580b6ebe09368a09d705e8b2fa67c9b07e3907..98ee84cb23f1b293fb087d977ab74ae7751f02b5 100644 (file)
@@ -28,7 +28,7 @@ pub async fn run(global: Arc<Global>,
     target_requests_outstanding: u32,
   }
   let mut outstanding: VecDeque<Outstanding> = default();
-  let  downbound: VecDeque<(/*xxx*/)> = default();
+  let mut downbound: PacketQueue<RoutedPacketData> = default();
 
   let try_send_response = |
     reply_to: oneshot::Sender<WebResponse>,
@@ -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,18 +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(vec![ /* xxx */ ]),
+        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() =>
       {
@@ -108,27 +135,22 @@ pub async fn run(global: Arc<Global>,
               //dbg!(&$v);
             }
           }
-
           meta!{
             target_requests_outstanding, ( != ), client,
             let server, client: u32 = meta.need_parse()?;
           }
-
           meta!{
             http_timeout, ( > ), client,
             let server, client = Duration::from_secs(meta.need_parse()?);
           }
-
           meta!{
             mtu, ( != ), client,
             let server, client: u32 = meta.parse()?.unwrap_or(server);
           }
-
           meta!{
             max_batch_down, (), min(client, server),
             let server, client: u32 = meta.parse()?.unwrap_or(server);
           }
-
           meta!{
             max_batch_up, ( > ), client,
             let server, client = meta.parse()?.unwrap_or(server);