[fractal] Backend: enhancements to backend/sync.rs



commit 46ac06576f736f7fda6899a6f7a4b28d25f512dc
Author: Alejandro Domínguez <adomu net-c com>
Date:   Mon Jan 21 14:50:10 2019 +0100

    Backend: enhancements to backend/sync.rs
    
    Removed a mut variable, replaced a match with an if let and used time::Duration
    for storing time instead of plain integers.

 fractal-matrix-api/src/backend/sync.rs | 37 ++++++++++++++--------------------
 1 file changed, 15 insertions(+), 22 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/sync.rs b/fractal-matrix-api/src/backend/sync.rs
index d19284a5..f73163f6 100644
--- a/fractal-matrix-api/src/backend/sync.rs
+++ b/fractal-matrix-api/src/backend/sync.rs
@@ -23,18 +23,14 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
     let since = bk.data.lock().unwrap().since.clone().or(new_since);
     let userid = bk.data.lock().unwrap().user_id.clone();
 
-    let mut params: Vec<(&str, String)> = vec![];
-    params.push(("full_state", String::from("false")));
-
-    let timeout;
+    let mut params = vec![("full_state", String::from("false"))];
 
     if let Some(since) = since.clone() {
         params.push(("since", since));
     }
 
-    if !initial {
-        params.push(("timeout", String::from("30000")));
-        timeout = 30;
+    let timeout = if !initial {
+        time::Duration::from_secs(30)
     } else {
         let filter = format!(r#"{{
             "room": {{
@@ -52,11 +48,12 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
             "event_format": "client",
             "event_fields": ["type", "content", "sender", "origin_server_ts", "event_id", "unsigned"]
         }}"#, globals::PAGE_LIMIT);
-
         params.push(("filter", filter));
-        params.push(("timeout", String::from("0")));
-        timeout = 0;
-    }
+
+        Default::default()
+    };
+
+    params.push(("timeout", timeout.as_secs().to_string()));
 
     let baseu = bk.get_base_url();
     let url = bk.url("sync", params)?;
@@ -84,14 +81,11 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
                     Err(err) => tx.send(BKResponse::RoomMessagesError(err)).unwrap(),
                 };
                 // Room notifications
-                match get_rooms_notifies_from_json(&r) {
-                    Ok(notifies) => {
-                        for (r, n, h) in notifies {
-                            tx.send(BKResponse::RoomNotifications(r.clone(), n, h))
-                                .unwrap();
-                        }
+                if let Ok(notifies) = get_rooms_notifies_from_json(&r) {
+                    for (r, n, h) in notifies {
+                        tx.send(BKResponse::RoomNotifications(r.clone(), n, h))
+                            .unwrap();
                     }
-                    Err(_) => {}
                 };
                 // Other events
                 match parse_sync_events(&r) {
@@ -137,7 +131,7 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
                     Ok(rs) => rs,
                     Err(err) => {
                         tx.send(BKResponse::SyncError(err)).unwrap();
-                        vec![]
+                        Default::default()
                     }
                 };
 
@@ -161,12 +155,11 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
         |err| {
             // we wait if there's an error to avoid 100% CPU
             error!("Sync Error, waiting 10 seconds to respond for the next sync");
-            let ten_seconds = time::Duration::from_millis(10000);
-            thread::sleep(ten_seconds);
+            thread::sleep(time::Duration::from_secs(10));
 
             tx.send(BKResponse::SyncError(err)).unwrap();
         },
-        timeout
+        timeout.as_secs()
     );
 
     Ok(())


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]