diff options
author | Eric Wong <e@80x24.org> | 2016-05-24 03:41:51 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2016-05-24 04:12:03 +0000 |
commit | 74bbc3da398d00ba12e9294e360ad177ab2061ed (patch) | |
tree | 964f723b1072e848c4db177ab43ba5461c657e89 /lib/PublicInbox/EvCleanup.pm | |
parent | e330b2eec13ebda99a7e2981375dee4fe397f14a (diff) | |
download | public-inbox-74bbc3da398d00ba12e9294e360ad177ab2061ed.tar.gz |
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.
Diffstat (limited to 'lib/PublicInbox/EvCleanup.pm')
-rw-r--r-- | lib/PublicInbox/EvCleanup.pm | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/PublicInbox/EvCleanup.pm b/lib/PublicInbox/EvCleanup.pm new file mode 100644 index 00000000..5efb0930 --- /dev/null +++ b/lib/PublicInbox/EvCleanup.pm @@ -0,0 +1,41 @@ +# Copyright (C) 2016 all contributors <meta@public-inbox.org> +# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> + +# event cleanups (currently for Danga::Socket) +package PublicInbox::EvCleanup; +use strict; +use warnings; + +my $asapq = { queue => [], timer => undef }; +my $laterq = { queue => [], timer => undef }; + +sub _run_all ($) { + my ($q) = @_; + + my $run = $q->{queue}; + $q->{queue} = []; + $q->{timer} = undef; + $_->() foreach @$run; +} + +sub _run_asap () { _run_all($asapq) } +sub _run_later () { _run_all($laterq) } + +sub asap ($) { + my ($cb) = @_; + push @{$asapq->{queue}}, $cb; + $asapq->{timer} ||= Danga::Socket->AddTimer(0, *_run_asap); +} + +sub later ($) { + my ($cb) = @_; + push @{$laterq->{queue}}, $cb; + $laterq->{timer} ||= Danga::Socket->AddTimer(60, *_run_later); +} + +END { + _run_asap(); + _run_later(); +} + +1; |