[Spread-users] Connecting to spread from Apache/mod_perl2

Daniel Risacher magnus at alum.mit.edu
Fri Jun 23 11:37:45 EDT 2006


Does anyone have any experiences to share interfacing Apache-based applications (using mod_perl2, in my case) to Spread?

Initially, I tried just connecting to spread from the mod_perl response handler, posting something, listening for a reponse (with timeout) and disconnecting.  This was distinctly unsatisfying because the disconnect doesn't seem to happen.  The next web request fails because it can't reconnect - the spread private name is already in use.  To remedy this problem, I'm now generating a random private name for each web request.  

This causes me to wonder: What happens to all the old names/connections?  Am I leaking somewhere?  I don't know, but I'd like to.  Is there some more reasonable thing to do like pooling connections a-la DBI?  Does anyone else have a better approach?

- Dan

Here's what my code looks like: 

package MyWebApp::Response; 

# various use statements go here ...

sub handler {
    my ($r) = @_;

    my $spuid = rand(1<<31);
    my ($mbox, $private_group) = Spread::connect({spread_name => "4803",
                                                  private_name => "a$spuid"});

    if (! defined $mbox) {
        $r->content_type('text/html');
        $r->print(p("($$) Failed to connect to spread: \"$!\" ".$Spread::sperrno));
        return Apache2::Const::OK;
    }

    Spread::multicast($mbox, RELIABLE_MESS, 'webapp', 0, "PING $private_group");
    # Another spread application should respond with a PONG

    my($service_type, $sender, $groups, $mess_type, $endian, $message) =
        Spread::receive($mbox, 1.789);  # 1.789 second timeout on receive

    $r->content_type('text/html');
    if (defined $message) {
        $r->print(p("Message from $sender: $message"));
    } else {
        $r->print(p("Timeout!"));
    }
    Spread::disconnect($mbox);
    $r->print(p("Spread::disconnect -- ".$Spread::sperrorno."\n"));
    return Apache2::Const::OK;
}






More information about the Spread-users mailing list