Pub Sub not working without interim sleep
Closed this issue · 2 comments
The following sample doesnt work without the sleep statement, i have excluded jzmq and using jeromq. moreover i dont see any error.
(ns exp.distributed
(:use [clojure.tools.logging :only (warn info error)])
(:require [zeromq.zmq :as zmq]
[clojure.core.async :as async :refer :all]))
(defn start-subs []
(future
(let [ctx (zmq/context 1)]
(with-open [sub (doto (zmq/socket ctx :sub)
(zmq/connect "tcp://127.0.0.1:5556")
(zmq/subscribe "topic"))]
(dotimes [i 10]
(info (zmq/receive-str sub)))))
(info "listeners quits.")))
(defn start-pubs []
(future
(let [ctx (zmq/context 1)]
(with-open [pub (doto (zmq/socket ctx :pub)
(zmq/bind "tcp://*:5556"))]
(Thread/sleep 1000);removing this doesnt works
(dotimes [i 10]
(zmq/send-str pub (format "topic %d" i)))))
(info "publisher quits.")))
(do
(start-subs)
(start-pubs))
This is the "slow joiner" problem. If you search in The Guide (http://zguide.zeromq.org) they have a detailed explanation of what causes this issue. The TL;DR is that subscribing is not a 0 time operation and the subscriber will miss any messages sent by the publisher until this process is finished. Adding the sleep is a good quick work around when testing but shouldn't be used in production. In terms of a real solution the guide suggests you view the pub-sub message stream as infinite thus having no beginning or end. This way you will never be expecting specific sequences of messages.
This issue represent the new face of "slow joiner".
Thanks!