about summary refs log tree commit homepage
path: root/lib/PublicInbox/EvCleanup.pm
blob: 5efb0930340ddde2f66c5d58392b12f221f4975b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;