Hi, 

There are two fn, one is to spawn the channel and the other is to send the 
message to the channel, 

The problem is that if the time to handle the message(or command) is more than 
the specified timeout time, and then another messages are emitted to the 
channel before the channel finish handling the previous message, such messages 
will be lost. those messages will not be seen by the channel.

firstly I thought it is caused by the buffer size of channel, but it seems it 
not the truth.

I am not sure what I missed.

Thanks

(defn register-channel
  "register a channel, the channel listens to event/command
   and responsible for handling them.
   the channels are mapped as {$:type {$:from channel}}"
  [channel-map type from handler]
  (func/put-if-absence!
   channel-map [type from]
   (fn []
     (let [ch (chan)]
       (log/debug "register channel" type from ch)
       (go (while true
             (let [cmd (<! ch)]
               (log/debug "receiving " cmd ch)
               (try
                 (handler cmd)
                 (catch Exception e
                   (log/error e))))))
       ch))))


(defn emit
  "emit event/command to the listening channel, type is to find channels 
related to the type"
  [channel-map event event-type options]
  (let [chs (get @channel-map  event-type)]
    (if (nil? chs)
      (do (throw
           (Exception.
            (str "no any handler for event " event " type " event-type))))
      (let [timeout-ms (:timeout options)]
        (log/debug "emitting " event "with options " options chs)
        (let [ch-seq (vals chs)]
          (go (doseq [ch ch-seq]
                    (>! ch event)))
          ;;FIXME the latter command will be lost if several commands are sent
          (when-not (nil? timeout-ms)
            (go (alts! (conj ch-seq (timeout timeout-ms))))
            (log/debug "done result within" timeout-ms)))))))

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to