Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use tight timeouts for empty queues #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions app/src/io/pedestal/app/queue.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,36 @@
(seq (get-in queue [:queues :low]))))

(defn- process-next-item [queue f]
(if (not-empty? @queue)
(when (not-empty? @queue)
(when-let [item (:item (swap! queue pop-message-internal))]
(platform/log-exceptions f item))
(platform/create-timeout 0 (fn [] (process-next-item queue f)))))
(platform/log-exceptions f item))))

(defn- count-queues [queues]
(+ (count (:high queues))
(count (:low queues))))

(comment
;; We record the functions interested in messages, so that we can
;; restart the message processing again once a message arrives in
;; the queue.
)

(defrecord AppMessageQueue [state]
p/PutMessage
(put-message [this message]
(let [priority (if (= (msg/priority message) :high) :high :low)
queues (:queues (swap! state update-in [:queues priority] conj message))]
(count-queues queues)))
queues (:queues (swap! state update-in [:queues priority] conj message))
new-count (count-queues queues)]
(when (= new-count 1)
(when-let [fns (:take-fns @state)]
(platform/create-timeout 0 #(doseq [f fns]
(process-next-item state f)))))
new-count))
p/TakeMessage
(pop-message [this]
(:item (swap! state pop-message-internal)))
(take-message [this f]
(swap! state update-in [:take-fns] conj f)
(process-next-item state f)))

(defn queue-length [app-message-queue]
Expand All @@ -68,4 +79,3 @@
(fn [message]
(f message)
(consume-queue queue f))))