changeset 21:643e00f58060

Enable notifying multiple listeners for the same client
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 02 Dec 2020 21:08:30 +0100
parents b60eb381eec8
children 65065070a4b4
files src/main.rs
diffstat 1 files changed, 9 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.rs	Wed Dec 02 21:04:16 2020 +0100
+++ b/src/main.rs	Wed Dec 02 21:08:30 2020 +0100
@@ -331,7 +331,7 @@
 fn live_notifier_thread(rx: mpsc::Receiver<NotifyRequest>, db: postgres::Connection) {
     const TICK_MILLIS: u32 = 500;
 
-    let mut clients: HashMap<String, NotifyRequest> = HashMap::new();
+    let mut clients: HashMap<String, Vec<NotifyRequest>> = HashMap::new();
 
     fn listen(db: &postgres::Connection, client: &str) -> postgres::Result<u64> {
         db.execute(&format!("LISTEN geohubclient_update_{}", client), &[])
@@ -350,7 +350,10 @@
                 if !clients.contains_key(&nrq.client) {
                     listen(&db, &nrq.client).ok();
                 }
-                clients.insert(nrq.client.clone(), nrq);
+                clients
+                    .entry(nrq.client.clone())
+                    .or_insert(vec![])
+                    .push(nrq);
             } else {
                 break;
             }
@@ -364,10 +367,12 @@
             let payload = notification.payload;
             unlisten(&db, &payload).ok();
 
-            if let Some(request) = clients.remove(&payload) {
+            for request in clients.remove(&payload).unwrap_or(vec![]) {
                 request
                     .respond
-                    .send(NotifyResponse { client: payload })
+                    .send(NotifyResponse {
+                        client: payload.clone(),
+                    })
                     .ok();
             }