Fix endless spinner on Fetch messages

This commit is contained in:
Roman Volosovskyi 2019-04-13 07:08:22 +03:00
parent 2753b0ef60
commit 5bd25515e4
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
1 changed files with 55 additions and 40 deletions

View File

@ -227,6 +227,13 @@
(when-not (or sym-key-id generating-sym-key?) (when-not (or sym-key-id generating-sym-key?)
(generate-mailserver-symkey mailserver))))) (generate-mailserver-symkey mailserver)))))
(defn executing-gap-request?
[{:mailserver/keys [current-request fetching-gaps-in-progress]}]
(= (get fetching-gaps-in-progress (:chat-id current-request))
(select-keys
current-request
[:from :to :force-to? :topics :chat-id])))
(fx/defn connect-to-mailserver (fx/defn connect-to-mailserver
"Add mailserver as a peer using `::add-peer` cofx and generate sym-key when "Add mailserver as a peer using `::add-peer` cofx and generate sym-key when
it doesn't exists it doesn't exists
@ -235,12 +242,14 @@
A connection-check is made after `connection timeout` is reached and A connection-check is made after `connection timeout` is reached and
mailserver-state is changed to error if it is not connected by then" mailserver-state is changed to error if it is not connected by then"
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [{:keys [address] :as mailserver} (fetch-current cofx) (let [{:keys [address]} (fetch-current cofx)
{:keys [peers-summary]} db {:keys [peers-summary]} db
added? (registered-peer? peers-summary added? (registered-peer? peers-summary address)
address)] gap-request? (executing-gap-request? db)]
(fx/merge cofx (fx/merge cofx
{:db (dissoc db :mailserver/current-request)} {:db (cond-> (dissoc db :mailserver/current-request)
gap-request?
(assoc :mailserver/fetching-gaps-in-progress {}))}
(if added? (if added?
(mark-trusted-peer) (mark-trusted-peer)
(add-peer))))) (add-peer)))))
@ -627,31 +636,25 @@
:mailserver/request-messages {:web3 (:web3 db) :mailserver/request-messages {:web3 (:web3 db)
:mailserver mailserver :mailserver mailserver
:request request-with-cursor}})) :request request-with-cursor}}))
(let [{:keys [chat-id] :as current-request} (db :mailserver/current-request) (fx/merge cofx
gaps (db :mailserver/fetching-gaps-in-progress) {:db (-> db
fetching-gap-completed? (= (get gaps chat-id) (dissoc :mailserver/current-request)
(select-keys (update :mailserver/requests-from
current-request #(apply dissoc % topics))
[:from :to :force-to? :topics :chat-id]))] (update :mailserver/requests-to
(fx/merge cofx #(apply dissoc % topics))
{:db (-> db (update :mailserver/topics merge mailserver-topics)
(dissoc :mailserver/current-request) (update :mailserver/fetching-gaps-in-progress
(update :mailserver/requests-from (fn [gaps]
#(apply dissoc % topics)) (if (executing-gap-request? db)
(update :mailserver/requests-to (dissoc gaps (:chat-id request))
#(apply dissoc % topics)) gaps))))
(update :mailserver/topics merge mailserver-topics) :data-store/tx (mapv (fn [[topic mailserver-topic]]
(update :mailserver/fetching-gaps-in-progress (data-store.mailservers/save-mailserver-topic-tx
(fn [gaps] {:topic topic
(if fetching-gap-completed? :mailserver-topic mailserver-topic}))
(dissoc gaps chat-id) mailserver-topics)}
gaps)))) (process-next-messages-request)))))))
:data-store/tx (mapv (fn [[topic mailserver-topic]]
(data-store.mailservers/save-mailserver-topic-tx
{:topic topic
:mailserver-topic mailserver-topic}))
mailserver-topics)}
(process-next-messages-request))))))))
(fx/defn retry-next-messages-request (fx/defn retry-next-messages-request
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
@ -786,7 +789,8 @@
(fx/defn resend-request (fx/defn resend-request
[{:keys [db] :as cofx} {:keys [request-id]}] [{:keys [db] :as cofx} {:keys [request-id]}]
(let [current-request (:mailserver/current-request db)] (let [current-request (:mailserver/current-request db)
gap-request? (executing-gap-request? db)]
;; no inflight request, do nothing ;; no inflight request, do nothing
(when (and current-request (when (and current-request
;; the request was never successful ;; the request was never successful
@ -803,16 +807,27 @@
(fx/merge cofx (fx/merge cofx
{:db (update db :mailserver/current-request dissoc :attempts)} {:db (update db :mailserver/current-request dissoc :attempts)}
(change-mailserver)) (change-mailserver))
(if-let [mailserver (get-mailserver-when-ready cofx)] (let [mailserver (get-mailserver-when-ready cofx)
(let [{:keys [topics from to cursor limit] :as request} current-request offline? (= :offline (:network-status db))]
web3 (:web3 db)] (cond
(log/info "mailserver: message request " request-id "expired for mailserver topic" topics "from" from "to" to "cursor" cursor "limit" (decrease-limit)) (and gap-request? offline?)
{:db (update-in db [:mailserver/current-request :attempts] inc) {:db (-> db
:mailserver/decrease-limit [] (dissoc :mailserver/current-request)
:mailserver/request-messages {:web3 web3 (update :mailserver/fetching-gaps-in-progress
:mailserver mailserver dissoc (:chat-id current-request)))}
:request (assoc request :limit (decrease-limit))}})
{:mailserver/decrease-limit []}))))) mailserver
(let [{:keys [topics from to cursor limit] :as request} current-request
web3 (:web3 db)]
(log/info "mailserver: message request " request-id "expired for mailserver topic" topics "from" from "to" to "cursor" cursor "limit" (decrease-limit))
{:db (update-in db [:mailserver/current-request :attempts] inc)
:mailserver/decrease-limit []
:mailserver/request-messages {:web3 web3
:mailserver mailserver
:request (assoc request :limit (decrease-limit))}})
:else
{:mailserver/decrease-limit []}))))))
(fx/defn initialize-mailserver (fx/defn initialize-mailserver
[cofx custom-mailservers] [cofx custom-mailservers]