From 74bbc3da398d00ba12e9294e360ad177ab2061ed Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 24 May 2016 03:41:51 +0000 Subject: standardize timer-related event-loop code Standardize the code we have in place to avoid creating too many timer objects. We do not need exact timers for things that don't need to be run ASAP, so we can play things fast and loose to avoid wasting power with unnecessary wakeups. We only need two classes of timers: * asap - run this on the next loop tick, after operating on @Danga::Socket::ToClose to close remaining sockets * later - run at some point in the future. It could be as soon as immediately (like "asap"), and as late as 60s into the future. In the future, we support an "emergency" switch to fire "later" timers immediately. --- lib/PublicInbox/HTTP.pm | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'lib/PublicInbox/HTTP.pm') diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index 104a2132..00c9a044 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -26,13 +26,22 @@ use constant { # FIXME: duplicated code with NNTP.pm my $WEAKEN = {}; # string(inbox) -> inbox -my $WEAKTIMER; +my $weakt; sub weaken_task () { - $WEAKTIMER = undef; + $weakt = undef; $_->weaken_all for values %$WEAKEN; $WEAKEN = {}; } +my $pipelineq = []; +my $pipet; +sub process_pipelineq () { + my $q = $pipelineq; + $pipet = undef; + $pipelineq = []; + rbuf_process($_) foreach @$q; +} + # Use the same configuration parameter as git since this is primarily # a slow-client sponge for git-http-backend # TODO: support per-respository http.maxRequestBuffer somehow... @@ -234,7 +243,7 @@ sub response_write { if (my $obj = $env->{'pi-httpd.inbox'}) { # grace period for reaping resources $WEAKEN->{"$obj"} = $obj; - $WEAKTIMER ||= Danga::Socket->AddTimer(60, *weaken_task); + $weakt ||= PublicInbox::EvCleanup::later(*weaken_task); } $self->{env} = undef; }; @@ -281,15 +290,6 @@ sub more ($$) { $self->write($_[1]); } -my $pipelineq = []; -my $next_tick; -sub process_pipelineq () { - $next_tick = undef; - my $q = $pipelineq; - $pipelineq = []; - rbuf_process($_) foreach @$q; -} - # overrides existing Danga::Socket method sub event_write { my ($self) = @_; @@ -300,7 +300,7 @@ sub event_write { $self->watch_read(1); } else { # avoid recursion for pipelined requests push @$pipelineq, $self; - $next_tick ||= Danga::Socket->AddTimer(0, *process_pipelineq); + $pipet ||= PublicInbox::EvCleanup::asap(*process_pipelineq); } } -- cgit v1.2.3-24-ge0c7