use "global" data in handler (was : custom proxy setup with mod_perl)

André Warnier <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
Hi.

This refers indirectly to the thread "custom proxy setup with mod_perl", but is more 
general, I believe.

For the PerlFixupHandler which was discussed before, I need to compare the current request 
URL to a predefined static list of URLs, to decide how exactly to proxy this call.
At the moment, there are about 15 URLs in that list; and I believe that there might, maybe 
in some cases, be up to 100.

I suppose that I could make this list of URLs available to the handler by doing something 
like this :

<Location />
   ...
   PerlFixupHandler My::Proxy->handler
   PerlAddVar CHKU "http://www.site-1.com/"
   PerlAddVar CHKU "http://www.site-2.org/path?query"
   PerlAddVar CHKU "http://www.site-3.biz/"
   PerlAddVar CHKU "http://www.site-4.edu/"
   etc.. 50 times
</Location>

and then do
	my @list = $r->dir_config('CHKU');
in the handler.

On the other hand, I could also put these URLs in a separate configuration text file, and 
have the handler read it at each invocation :

<Location />
   ...
   PerlFixupHandler My::Proxy->handler
   PerlSetVar URL_LIST "/etc/apache2/conf/urllist.txt"
</Location>

and in the handler :

	my $urllist = $r->dir_config('URL_LIST') || "default.list";
	my @list = ();
	my $fh;
	open($fh,'<',$urllist) or safe_die;
	@list = <$fh>;
	close $fh;

But neither of these is very satisfying.
The first one above is OK up to 10-20 URLs, but becomes a bit unsightly and clumsy for more.
The second one has the handler read and parse a file at each invocation (even if the 
request URL turns out not to be in the list, and the call should just be proxy-ed as is).
Sounds inefficient.

So, I would like to do this :

Package My::Proxy;

our $URLS;

sub handler {
my $r = shift;
unless (defined($URLS)) {
	my $urllist = $r->dir_config('URL_LIST') || "/etc/apache2/default_urls.list";
	my $fh;
	my @list;
	open($fh,'<',$urllist) or safe_die;
	@list = <$fh>;
	close $fh;
	$URLS = \@list;  # or = [ @list ] ??
}
.. use @$URLS, read-only ..


}

As I understand the above, in a pre-fork MPM, each time Apache forks a new child, at the 
first handler invocation $URLS will be undef, and thus the code in the handler will define 
it, as a reference to an array of URLs read from the file.
And from then on, the list accessible through $URLS should remain available and unvariable 
at each subsequent invocation of this handler (in this child), as long as this Apache 
child process lives.

First, I may be wrong in my understanding. (that's a question).

Second, if I am right in the above, then does this same understanding extend to all 
possible Apache MPMs ?  Or am I creating a horrible risk of some race condition, or memory 
leak here ?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.