1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # event cleanups (currently for PublicInbox::DS)
5 package PublicInbox::EvCleanup;
8 use base qw(PublicInbox::DS);
11 sub enabled { $ENABLED }
12 sub enable { $ENABLED = 1 }
14 my $asapq = [ [], undef ];
15 my $nextq = [ [], undef ];
16 my $laterq = [ [], undef ];
19 my $self = fields::new('PublicInbox::EvCleanup');
22 # This is a dummy pipe which is always writable so it can always
23 # fires in the next event loop iteration.
24 pipe($r, $w) or die "pipe: $!";
25 fcntl($w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
26 $self->SUPER::new($w, 0);
28 # always writable, since PublicInbox::EvCleanup::event_step
29 # never drains wbuf. We can avoid wasting a hash slot by
30 # stuffing the read-end of the pipe into the never-to-be-touched
45 # ensure PublicInbox::DS::ToClose processing after timers fire
46 sub _asap_close () { $asapq->[1] ||= _asap_timer() }
48 sub _run_asap () { _run_all($asapq) }
59 # Called by PublicInbox::DS
62 $self->watch_write(0);
67 $singleton ||= once_init();
68 $singleton->watch_write(1);
74 push @{$asapq->[0]}, $cb;
75 $asapq->[1] ||= _asap_timer();
80 push @{$nextq->[0]}, $cb;
81 $nextq->[1] ||= PublicInbox::DS->AddTimer(0, *_run_next);
86 push @{$laterq->[0]}, $cb;
87 $laterq->[1] ||= PublicInbox::DS->AddTimer(60, *_run_later);