I answered directly to Torsten by error
Here is my answer and the reply

2008/2/6, Torsten Foertsch <torsten.foertsch@gmx.net>:
On Wed 06 Feb 2008, titetluc titetluc wrote:
> The module I am developing has to delete the cookie if it is not refreshed
> regularly.
> The question: how can I manage this timeout inactivity ?
> The best solution would be to use a mechanism where callbacks (deleting the
> cookie rfom the database) would be called automatically on inactivity.
> Does such an API is proposed by :
>      . the APR API
>      . mod_perl API
>      . an Apache2::xxx perl module
>      . a CPAN module
> If not, how can I solve my problem ? (I could verify regularly in the DB
> storage, but this is a last resort solution. Even in this case, how could I
> implement it ?)

Apache (at least 2.2.x) implements a "monitor" hook, see server/mpm_common.c.
To use this hook you'd have to write a bit XS stuff like Geoff's
AuthenHook, ... since there is AFAIK no CPAN module. This hook is run from
time to time in the parent apache.

Otherwise there are 2 standard ways to do that:

- a cron job or something similar in the DB itself
- check each time in a connection cleanup handler (to do it not too often you
can use a global variable that holds the timestamp of the last cleanup and
run it only if the difference to the current time grows too big.)

I'd go for one of the standard ways since:

- easier to implement
- your code doesn't run as root


> One naive question: how can I declare a global variable under mod_perl ?
> Each request is run with a thread and by default Perl does not share
> variables !
> I declared my variable as shared (using the threads::shared module) but
> this declaration does not seem to be sufficient in a mod_perl environment
> !!!

I meant something like this:

package My::Cleanup;

use strict;
use Apache2::Const -compile=>('OK', 'DECLINED');
use Apache2::RequestRec ();
use Apache2::Connection ();
use APR::Pool ();

my $lastrun=0;         # this is the global variable: one per process
my $check_interval=60; #check every minute

sub run {
 my $time=time;
 if( $time>$lastrun+$check_interval ) {
   # here you can check the modification time ((stat)[9]) of a file on disk
   # flock() it with LOCK_NB set and return if flock fails.
   # alternatively you can implement an interface to apr_proc_mutex which is
   # quite easy, see ThreadMutex for example.
   # or you implement $lastrun in your database.
   # or you use BerkeleyDB which is actually shared memory.

   # doit
 return Apache2:Const::OK;

sub handler {
 my ($r)=@_;
 unless( $r->connection->pnotes->{cleanup_installed} ) {
 return Apache2::Const::DECLINED;


# install it either as PerlPostReadRequestHandler or as
# PerlHeaderParserHandler
PerlInitHandler My::Cleanup

I'd implement the process global variable as shown plus perhaps another
variable in the database if the session is stored there. It depends on how
expensive your cleanup is.