Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


I am using Cache::FastMmap 1.34 with mod_perl 2.0.4 (Debian packaged) to help with configuration. I am using the PerlTransHandler hook to determine where on the filesystem a particular virtual host resides, by looking it up in a database then caching the results.

It mostly works, but every now and then it stops working. When it's not working Apache seems to hang on every page load, and I see this in the log:

[Thu Nov 12 23:30:56 2009] [notice] child pid 21117 exit signal Alarm +clock (14) [Fri Nov 13 00:14:54 2009] [notice] child pid 21309 exit signal Alarm +clock (14) [Fri Nov 13 00:49:55 2009] [error] [client] cache old sl +ots mistmatch at /usr/local/lib/perl/5.10.0/Cache/ line 65 +6.\n [Fri Nov 13 01:46:04 2009] [error] [client] cache old slo +ts mistmatch at /usr/local/lib/perl/5.10.0/Cache/ line 656 +.\n

Google doesn't turn up any obvious solutions for this.

My Apache configuration looks like this:

PerlModule My::VirtualHost PerlTransHandler My::VirtualHost::TransHandler

My code looks basically like this (in reality it is a bit more complicated):

package My::VirtualHost::TransHandler; use strict; use warnings; use Apache2::Const -compile => qw(OK DECLINED); use Apache2::RequestRec (); use My::Config; use DBI; use Cache::FastMmap; # This happens globally in the parent process our $cache = Cache::FastMmap->new( read_cb => \&get_virthost, expire_time => '1m', init_file => 1, cache_not_found => 1, ); our($dbh,$sth); sub get_sth { if (!$dbh) { $dbh = DBI->connect(My::Config::DB_DSN, My::Config::DB_USER, M +y::Config::DB_PASS) or die "Couldn't connect to database\n"; $sth = $dbh->prepare('SELECT virthost_id FROM virtual_hosts WH +ERE url = lower(?)') or die "Couldn't prepare statement\n"; } return $sth; } sub get_virthost { my($ctx,$hn)=@_; my $ret; eval { my $url = 'http://'.$hn; my $sth = get_sth(); $sth->execute($url); my $row = $sth->fetchrow_arrayref(); if ($row && defined($row->[0])) { $ret = $row->[0]; } else { $ret = undef; } $sth->finish(); return $ret; }; if ($@) { # TODO: Some kind of error handling warn "Error getting virtual host: $@"; $ret = undef; } $ret; } sub handler { my $r = shift; my $hn = $r->hostname(); if (!$hn) { # TODO: Return some kind of error return Apache2::Const::DECLINED; } my $id = $cache->get($hn); if (defined($id)) { my $newfn = My::Config::BASE_PATH . "/virthosts/$id".$r->uri() +; $r->filename($newfn); return Apache2::Const::OK; } else { return Apache2::Const::DECLINED; } }

My questions are:

  • Does anybody see anything obviously wrong with what I'm doing?
  • Has anybody done something similar before, successfully or unsuccessfully?


In reply to Cache::FastMmap and mod_perl by sgifford

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2023-05-28 12:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found