[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: Singleton Persistence
From:       John Dunlap <john () lariat ! co>
Date:       2014-02-27 17:46:02
Message-ID: CAC5eUSvdv7EPvtqeyOxuDGOdiomgUhMdzT+1fjCaBk-WF_LJjA () mail ! gmail ! com
[Download RAW message or body]

In the solution that I ended up using, the singleton in question contains
the reference to IOC::Container, which contains service literals for each
of the constants which are available in $apache->dir_config. As a
performance enhancement, when I attempt to retrieve an object from the
container, I check the server hostname against the hostname in the
container and only reinitialize the container if they don't match. This
avoids reinitializing the container if it happens to be sequentially used
in the same virtualhost multiple times. I suppose I could have built a more
sophisticated caching system which stores container instances in a hash and
retrieves the correct one by hostname but I didn't feel like spending the
time and it seemed like a more fragile approach than I'm comfortable with.

if (is_isa($apache, 'Apache2::RequestRec')) {
my $server_hostname = $apache->server->server_hostname;
my $container_hostname = $container->get('hostname');
if ($server_hostname ne $container_hostname) {
$container->{C} = undef;
$container->_init_components($apache);
}
}



On Thu, Feb 27, 2014 at 12:34 PM, Rolf Schaufelberger <rs@plusw.de> wrote:

> Hi,
>
> we are using Class:Singleton , yet we manually destroy the singleton at
> the beginning of each request before we create a new one.
> So I have a class PW::Application which isa Class::Singleton  and then
>
>     {
>         no strict 'refs';
>         ${"PW::Application\::_instance"}= undef;
>     }
>     $self->{appl}= PW::Application->instance($param{base});
>
> I tried Apache::Singleton before, but, as far as I can remember,  it
> didn't work with mp2.
> So why are we using a singleton ? Well  , our app does not  only  consist
> of the web part , we also have many scripts running that are part of the
> application, and we put things like DBIx::Class schema object, config,
> current user etc in our singleton and so we can access these data  the same
> way, neither if we run a function from web interface or from command line.
> Since we don't have heavy  load on our apps creating this singleton with
> every request works for me .
>
>
>
> Am 02.02.2014 um 02:14 schrieb John Dunlap <john@lariat.co>:
>
> > In mod_perl, can instantiated singletons survive requests? I'm asking
> because I appear to have one which *appears* to be bouncing back and forth
> between virtual hosts as perl interpreters are recycled. Is this possible
> and, if yes, how do I prevent it?
> >
> > Cheers!
> > John
>
>
> Rolf Schaufelberger
>

[Attachment #3 (text/html)]

<div dir="ltr">In the solution that I ended up using, the singleton in question \
contains the reference to IOC::Container, which contains service literals for each of \
the constants which are available in $apache-&gt;dir_config. As a performance \
enhancement, when I attempt to retrieve an object from the container, I check the \
server hostname against the hostname in the container and only reinitialize the \
container if they don&#39;t match. This avoids reinitializing the container if it \
happens to be sequentially used in the same virtualhost multiple times. I suppose I \
could have built a more sophisticated caching system which stores container instances \
in a hash and retrieves the correct one by hostname but I didn&#39;t feel like \
spending the time and it seemed like a more fragile approach than I&#39;m comfortable \
with.<br> <br><div><span class="" style="white-space:pre">	</span>if (is_isa($apache, \
&#39;Apache2::RequestRec&#39;)) {</div><div><span class="" \
style="white-space:pre">		</span>my $server_hostname = \
$apache-&gt;server-&gt;server_hostname;</div> <div><span class="" \
style="white-space:pre">		</span>my $container_hostname = \
$container-&gt;get(&#39;hostname&#39;);</div><div><span class="" \
style="white-space:pre">		</span>if ($server_hostname ne $container_hostname) {</div> \
<div><span class="" style="white-space:pre">			</span>$container-&gt;{C} = \
undef;</div><div><span class="" \
style="white-space:pre">			</span>$container-&gt;_init_components($apache);</div><div><span \
class="" style="white-space:pre">		</span>}</div> <div><span class="" \
style="white-space:pre">	</span>}</div><div><br></div></div><div \
class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Feb 27, 2014 at 12:34 \
PM, Rolf Schaufelberger <span dir="ltr">&lt;<a href="mailto:rs@plusw.de" \
target="_blank">rs@plusw.de</a>&gt;</span> wrote:<br> <blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br> <br>
we are using Class:Singleton , yet we manually destroy the singleton at the beginning \
of each request before we create a new one.<br> So I have a class PW::Application \
which isa Class::Singleton &nbsp;and then<br> <br>
&nbsp; &nbsp; {<br>
&nbsp; &nbsp; &nbsp; &nbsp; no strict &#39;refs&#39;;<br>
&nbsp; &nbsp; &nbsp; &nbsp; ${&quot;PW::Application\::_instance&quot;}= undef;<br>
&nbsp; &nbsp; }<br>
&nbsp; &nbsp; $self-&gt;{appl}= PW::Application-&gt;instance($param{base});<br>
<br>
I tried Apache::Singleton before, but, as far as I can remember, &nbsp;it \
didn&rsquo;t work with mp2.<br> So why are we using a singleton ? Well &nbsp;, our \
app does not &nbsp;only &nbsp;consist of the web part , we also have many scripts \
running that are part of the application, and we put things like DBIx::Class schema \
object, config, current user etc in our singleton and so we can access these data \
&nbsp;the same way, neither if we run a function from web interface or from command \
line.<br>

Since we don&rsquo;t have heavy &nbsp;load on our apps creating this singleton with \
every request works for me .<br> <br>
<br>
<br>
Am 02.02.2014 um 02:14 schrieb John Dunlap &lt;<a \
href="mailto:john@lariat.co">john@lariat.co</a>&gt;:<br> <div class="HOEnZb"><div \
class="h5"><br> &gt; In mod_perl, can instantiated singletons survive requests? \
I&#39;m asking because I appear to have one which *appears* to be bouncing back and \
forth between virtual hosts as perl interpreters are recycled. Is this possible and, \
if yes, how do I prevent it?<br>

&gt;<br>
&gt; Cheers!<br>
&gt; John<br>
<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">Rolf Schaufelberger<br>
</font></span></blockquote></div><br></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic