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);
9 use PublicInbox::Syscall qw(EPOLLOUT EPOLLONESHOT);
12 sub enabled { $ENABLED }
13 sub enable { $ENABLED = 1 }
15 my $asapq = [ [], undef ];
16 my $nextq = [ [], undef ];
17 my $laterq = [ [], undef ];
20 my $self = fields::new('PublicInbox::EvCleanup');
23 # This is a dummy pipe which is always writable so it can always
24 # fires in the next event loop iteration.
25 pipe($r, $w) or die "pipe: $!";
26 fcntl($w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
27 $self->SUPER::new($w, 0);
29 # always writable, since PublicInbox::EvCleanup::event_step
30 # never drains wbuf. We can avoid wasting a hash slot by
31 # stuffing the read-end of the pipe into the never-to-be-touched
46 # ensure PublicInbox::DS::ToClose processing after timers fire
47 sub _asap_close () { $asapq->[1] ||= _asap_timer() }
49 # Called by PublicInbox::DS
50 sub event_step { _run_all($asapq) }
63 $singleton ||= once_init();
64 $singleton->watch(EPOLLOUT|EPOLLONESHOT);
70 push @{$asapq->[0]}, $cb;
71 $asapq->[1] ||= _asap_timer();
76 push @{$nextq->[0]}, $cb;
77 $nextq->[1] ||= PublicInbox::DS->AddTimer(0, *_run_next);
82 push @{$laterq->[0]}, $cb;
83 $laterq->[1] ||= PublicInbox::DS->AddTimer(60, *_run_later);