blaine / xmpp4r-simple

Jabber::Simple
GNU General Public License v2.0
140 stars 83 forks source link

Deferred message delivery spikes CPU usage to 100% #1

Open mtodd opened 15 years ago

mtodd commented 15 years ago

This is directly related to:

http://code.google.com/p/xmpp4r-simple/issues/detail?id=7#makechanges

The culprit is start_deferred_delivery_thread with the following code:

def start_deferred_delivery_thread #:nodoc:
Thread.new {
  loop {
    messages = [queue(:pending_messages).pop].flatten
    messages.each do |message|
      if subscribed_to?(message[:to])
        deliver(message[:to], message[:message], message[:type])
      else
        queue(:pending_messages) << message
      end
    end
  }
}

end

The loop has absolutely no sleep time and will run at max speed, continually dequeueing and queueing until there is no more CPU power left, then it keeps going until nothing else runs on your computer (unless you've got god/monit monitoring it) until the subscription actually occurs.

In my production environment, this is asynchronous, and can occur even upwards of days later, and I'd rather not have my CPU raped for that long.

mtodd commented 15 years ago

Committed quick fix and submitted pull request with tests.

mtodd commented 15 years ago

http://github.com/mtodd/xmpp4r-simple just in case you want to inspect my patch