[polari] accountsMonitor: Improve reachable check



commit 450082f4ce88ac519227136ab1af098de24d64aa
Author: Florian Müllner <fmuellner gnome org>
Date:   Wed Aug 7 01:35:13 2019 +0200

    accountsMonitor: Improve reachable check
    
    While we don't block when determining whether a network is reachable
    or not, we check servers sequentially until we find one that works,
    which is less effective than starting all checks in parallel.
    
    Do that via some Promise black magic that resolves successfully when
    the first address can be reached, and rejects when all addresses fail.
    
    https://gitlab.gnome.org/GNOME/polari/merge_requests/132

 src/accountsMonitor.js | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
---
diff --git a/src/accountsMonitor.js b/src/accountsMonitor.js
index eefa46c..344644d 100644
--- a/src/accountsMonitor.js
+++ b/src/accountsMonitor.js
@@ -170,27 +170,37 @@ var AccountsMonitor = class {
         if (!this._networkMonitor.state_valid)
             return;
 
-        let servers = account.getServers();
-        for (let s of servers) {
-            let addr = new Gio.NetworkAddress({
+        let servers = account.getServers().map(s => {
+            return new Gio.NetworkAddress({
                 hostname: s.address,
                 port: s.port
             });
+        });
 
-            account._setReachable(await this._canReach(addr));
-
-            if (account.reachable)
-                break;
+        try {
+            let reachable = await this._canReachAny(servers);
+            account._setReachable(reachable);
+        } catch (e) {
+            account._setReachable(false);
         }
     }
 
+    _canReachAny(servers) {
+        let reverse = promise => {
+            return new Promise((resolve, reject) => {
+                return Promise.resolve(promise).then(reject, resolve);
+            });
+        };
+        return reverse(Promise.all(servers.map(s => reverse(this._canReach(s)))));
+    }
+
     _canReach(addr) {
-        return new Promise((resolve) => {
+        return new Promise((resolve, reject) => {
             this._networkMonitor.can_reach_async(addr, null, (mon, res) => {
                 try {
                     resolve(this._networkMonitor.can_reach_finish(res));
                 } catch (e) {
-                    resolve(false);
+                    reject(e);
                 }
             });
         });


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