Browse Source

enhance(sync): refine network checking (#10147)

Andelf 2 years ago
parent
commit
cd24919716
2 changed files with 26 additions and 10 deletions
  1. 2 0
      src/main/frontend/components/plugins.cljs
  2. 24 10
      src/main/frontend/fs/sync.cljs

+ 2 - 0
src/main/frontend/components/plugins.cljs

@@ -433,6 +433,8 @@
                           (assoc opts :test (util/trim-safe (util/evalue %))))
           :value       (:test opts)}]
         [:datalist#proxy-test-url-datalist
+         [:option "https://api.logseq.com/logseq/version"]
+         [:option "https://logseq-connectivity-testing-prod.s3.us-east-1.amazonaws.com/logseq-connectivity-testing"]
          [:option "https://www.google.com"]
          [:option "https://s3.amazonaws.com"]
          [:option "https://clients3.google.com/generate_204"]]]

+ 24 - 10
src/main/frontend/fs/sync.cljs

@@ -237,17 +237,25 @@
     (when-let [ws (:ws @*ws)]
       (.close ws))))
 
+(def *ws-connect-retries (atom 0))
+(declare <sync-stop)
 (defn- ws-listen!*
   [graph-uuid *ws remote-changes-chan]
   (reset! *ws {:ws (js/WebSocket. (util/format ws-addr graph-uuid)) :stop false})
   (ws-ping-loop (:ws @*ws))
-  ;; (set! (.-onopen (:ws @*ws)) #(println (util/format "ws opened: graph '%s'" graph-uuid %)))
+  (set! (.-onopen (:ws @*ws)) #(reset! *ws-connect-retries 0))
   (set! (.-onclose (:ws @*ws)) (fn [_e]
-                                 (when-not (true? (:stop @*ws))
-                                   (go
-                                     (timeout 1000)
-                                     (println "re-connecting graph" graph-uuid)
-                                     (ws-listen!* graph-uuid *ws remote-changes-chan)))))
+                                 (if (> @*ws-connect-retries 3)
+                                   (do (println "ws-listen! retry count > 3, stop retry")
+                                       (reset! *ws-connect-retries 0)
+                                       (swap! *ws (fn [o] (assoc o :stop true)))
+                                       (<sync-stop))
+                                   (when-not (true? (:stop @*ws))
+                                     (go
+                                       (timeout 1000)
+                                       (println "re-connecting graph" graph-uuid)
+                                       (swap! *ws-connect-retries inc)
+                                       (ws-listen!* graph-uuid *ws remote-changes-chan))))))
   (set! (.-onmessage (:ws @*ws)) (fn [e]
                                    (let [data (js->clj (js/JSON.parse (.-data e)) :keywordize-keys true)]
                                      (when (some? (:txid data))
@@ -3357,9 +3365,13 @@
                    (= 200 (:status r2*))
                    (= "OK" (:body r2*)))]
       (if ok?
-        (println :connectivity-testing-succ)
+        (do (println :connectivity-testing-succ)
+            (notification/clear! :sync-connection-failed))
         (notification/show! (str (t :file-sync/connectivity-testing-failed)
-                                 (print-str [config/CONNECTIVITY-TESTING-S3-URL api-url])) :warning false))
+                                 (print-str [config/CONNECTIVITY-TESTING-S3-URL api-url]))
+                            :warning
+                            false
+                            :sync-connection-failed))
       ok?)))
 
 (declare network-online-cursor)
@@ -3492,8 +3504,10 @@
 ;; try to re-start sync when state=stopped every 1min
 (go-loop []
   (<! (timeout 60000))
-  (when (and @network-online-cursor       ; is online
-             (= ::stop (:state (state/get-file-sync-state))))
+  (when (and (state/enable-sync?)
+             @network-online-cursor       ; is online
+             (or (= ::stop (:state (state/get-file-sync-state))) ;; state=stopped
+                 (nil? (state/get-file-sync-state)))) ;; the whole sync state not inited yet, happens when app starts without network
     (println "trying to restart sync...")
     (<sync-start))
   (recur))