Block on recv instead of loop/polling in MultiSession
authorBill Erickson <berick@esilibrary.com>
Thu, 31 Jan 2013 20:05:53 +0000 (15:05 -0500)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Wed, 13 Mar 2013 21:36:13 +0000 (17:36 -0400)
When the time comes to wait for responses to arrive, block on the XMPP
socket (which uses select() under the covers) until data arrives,
instead of looping furiously and calling receive in non-blocking mode.
Before this change, waiting on responses resulted in long-running CPU
spikes.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
src/perl/lib/OpenSRF/MultiSession.pm

index dd0579c..e196027 100644 (file)
@@ -215,17 +215,21 @@ sub request {
 sub session_wait {
        my $self = shift;
        my $all = shift;
+       my $xmpp = OpenSRF::Transport::PeerHandle->retrieve;
 
        my $count;
        if ($all) {
                $count = $self->running;
                while ($self->running) {
+                       # block on the xmpp socket until data arrives
+                       $xmpp->process(-1);
                        $self->session_reap;
                }
                return $count;
        } else {
                while(($count = $self->session_reap) == 0 && $self->running) {
-                       usleep 100;
+                       # block on the xmpp socket until data arrives
+                       $xmpp->process(-1);
                }
                return $count;
        }