Minor improvements on redis subscription management.

This commit is contained in:
Andrey Antukh 2020-04-28 08:12:04 +02:00 committed by Alonso Torres
parent 8b6f72a09b
commit 9b3879b3cf
2 changed files with 19 additions and 21 deletions

View file

@ -50,23 +50,19 @@
(let [cmd (.async conn) (let [cmd (.async conn)
output (a/chan 1 (filter string?)) output (a/chan 1 (filter string?))
buffer (a/chan (a/sliding-buffer 64)) buffer (a/chan (a/sliding-buffer 64))
listener (reify RedisPubSubListener sub (reify RedisPubSubListener
(message [it pattern channel message]) (message [it pattern channel message])
(message [it channel message] (message [it channel message]
;; There are no back pressure, so we use a ;; There are no back pressure, so we use a slidding
;; slidding buffer for cases when the pubsub ;; buffer for cases when the pubsub broker sends
;; broker sends more messages that we can ;; more messages that we can process.
;; process.
(a/put! buffer message)) (a/put! buffer message))
(psubscribed [it pattern count] (psubscribed [it pattern count])
#_(prn "psubscribed" pattern count)) (punsubscribed [it pattern count])
(punsubscribed [it pattern count] (subscribed [it channel count])
#_(prn "punsubscribed" pattern count)) (unsubscribed [it channel count]))]
(subscribed [it channel count] (.addListener conn sub)
#_(prn "subscribed" channel count))
(unsubscribed [it channel count]
#_(prn "unsubscribed" channel count)))]
(.addListener conn listener)
(a/go-loop [] (a/go-loop []
(let [[val port] (a/alts! [buffer (a/timeout 5000)]) (let [[val port] (a/alts! [buffer (a/timeout 5000)])
message (if (= port buffer) val ::keepalive)] message (if (= port buffer) val ::keepalive)]
@ -74,8 +70,10 @@
(recur) (recur)
(do (do
(a/close! buffer) (a/close! buffer)
(.removeListener conn sub)
(when (.isOpen conn) (when (.isOpen conn)
(.close conn)))))) (.close conn))))))
(-> (.subscribe ^RedisPubSubAsyncCommands cmd (into-array String [topic])) (-> (.subscribe ^RedisPubSubAsyncCommands cmd (into-array String [topic]))
(p/then' (constantly output))))) (p/then' (constantly output)))))

View file

@ -2,7 +2,7 @@
{org.clojure/tools.logging {:mvn/version "0.5.0"} {org.clojure/tools.logging {:mvn/version "0.5.0"}
funcool/promesa {:mvn/version "5.0.0"} funcool/promesa {:mvn/version "5.0.0"}
metosin/reitit-core {:mvn/version "0.3.10"} metosin/reitit-core {:mvn/version "0.3.10"}
org.clojure/core.async {:mvn/version "0.7.559"} org.clojure/core.async {:mvn/version "1.1.587"}
io.vertx/vertx-core {:mvn/version "4.0.0-milestone4"} io.vertx/vertx-core {:mvn/version "4.0.0-milestone4"}
io.vertx/vertx-web {:mvn/version "4.0.0-milestone4"} io.vertx/vertx-web {:mvn/version "4.0.0-milestone4"}
io.vertx/vertx-web-client {:mvn/version "4.0.0-milestone4"}} io.vertx/vertx-web-client {:mvn/version "4.0.0-milestone4"}}