remove duplicate calls to jail

This commit is contained in:
Roman Volosovskyi 2017-09-28 20:07:37 +02:00
parent f8f13e21cc
commit bc9e65774c
1 changed files with 47 additions and 2 deletions

View File

@ -147,7 +147,7 @@
(when status (when status
(call-module #(.parseJail status chat-id file callback)))) (call-module #(.parseJail status chat-id file callback))))
(defn call-jail [{:keys [jail-id path params callback]}] (defn execute-call [{:keys [jail-id path params callback]}]
(when status (when status
(call-module (call-module
#(do #(do
@ -167,6 +167,51 @@
(callback r')))] (callback r')))]
(.callJail status jail-id (types/clj->json path) (types/clj->json params') cb)))))) (.callJail status jail-id (types/clj->json path) (types/clj->json params') cb))))))
;; TODO(rasom): temporal solution, should be fixed on status-go side
(def check-raw-calls-interval 400)
(def interval-between-calls 100)
;; contains all calls to jail before with duplicates
(def raw-jail-calls (atom '()))
;; contains only calls that passed duplication check
(def jail-calls (atom '()))
(defn remove-duplicate-calls
"Removes duplicates by [jail path] keys, remains the last one."
[[all-keys calls] {:keys [jail-id path] :as call}]
(if (contains? all-keys [jail-id path])
[all-keys calls]
[(conj all-keys [jail-id path])
(conj calls call)]))
(defn check-raw-calls-loop!
"Only the last call with [jail path] key is added to jail-calls list
from raw-jail-calls"
[]
(go-loop [_ nil]
(let [[_ new-calls] (reduce remove-duplicate-calls [#{} '()] @raw-jail-calls)]
(reset! raw-jail-calls '())
(swap! jail-calls (fn [old-calls]
(concat new-calls old-calls))))
(recur (<! (timeout check-raw-calls-interval)))))
(defn execute-calls-loop!
"Calls to jail are executed ne by one with interval-between-calls,
which reduces chances of response shuffling"
[]
(go-loop [_ nil]
(let [next-call (last @jail-calls)]
(swap! jail-calls butlast)
(when next-call
(execute-call next-call)))
(recur (<! (timeout interval-between-calls)))))
(check-raw-calls-loop!)
(execute-calls-loop!)
(defn call-jail [call]
(swap! raw-jail-calls conj call))
;; TODO(rasom): end of sick magic, should be removed ^
(defn call-function! (defn call-function!
[{:keys [chat-id function callback] :as opts}] [{:keys [chat-id function callback] :as opts}]
(let [path [:functions function] (let [path [:functions function]