Popping messages from Ruby Queue in a proper way -
here code send messages queue:
def initialize @messages = queue.new end def send_messages_from_queue while msg = @messages.pop(true) #pushing data through websocket end rescue threaderror #raised if queue empty end
it works has 1 drawback: if there no channel subscribers message not being sent still taken out of queue
, therefore goes lost.
how take last message queue not delete there until sent?
i expose inner queue:
@messages.class.module_eval { attr_reader :que }
or
@messages.define_singleton_method(:first_message) @que.first end
but not right way , has it's own drawback:
when take message out of queue in 1 thread using pop
, won't available other thread few milliseconds later, when use custom first_message
method, message available other thread until call pop
.
Comments
Post a Comment