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

Fred Moyer <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <CAHrynWBLMPf_qX=Qng4DeN1Firw_jrQAk=4y+5b9T=bd3+=UOA@mail.gmail.com>
On Sat, Nov 24, 2012 at 5:09 AM, André Warnier <[email protected]> wrote:
> 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.

Maybe read the variables in startup.pl, and setup a package to provide
them to other modules at runtime?
http://perl.apache.org/docs/2.0/user/handlers/server.html#Startup_File

# pass $urllist var from httpd.conf?

startup.pl:
>         my $fh;
>         open($fh,'<',$urllist) or safe_die;
>         my @list = <$fh>;
>         close $fh;
My::Proxy->set_urllist(\@list);


There are a few different ways to specify $urllist in httpd.conf I
think so that startup.pl can get at it at startup time. Remember that
startup.pl is actually read twice I think (when apache starts, it
starts twice to make sure it can survive a restart).

http://perl.apache.org/docs/2.0/api/Apache2/ServerUtil.html#C_restart_count_

Hope that helps - there are some details I'm probably glossing over here.

>
> 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.