]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxIdle.pm
inboxidle: avoid needless syscalls on refresh
[public-inbox.git] / lib / PublicInbox / InboxIdle.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # fields:
5 # pi_config: PublicInbox::Config ref
6 # inot: Linux::Inotify2-like object
7 # pathmap => { inboxdir => [ ibx, watch1, watch2, watch3... ] } mapping
8 package PublicInbox::InboxIdle;
9 use strict;
10 use parent qw(PublicInbox::DS);
11 use Cwd qw(abs_path);
12 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
13 my $IN_MODIFY = 0x02; # match Linux inotify
14 my $ino_cls;
15 if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
16         $IN_MODIFY = Linux::Inotify2::IN_MODIFY();
17         $ino_cls = 'Linux::Inotify2';
18 } elsif (eval { require PublicInbox::KQNotify }) {
19         $IN_MODIFY = PublicInbox::KQNotify::NOTE_WRITE();
20         $ino_cls = 'PublicInbox::KQNotify';
21 }
22 require PublicInbox::In2Tie if $ino_cls;
23
24 sub in2_arm ($$) { # PublicInbox::Config::each_inbox callback
25         my ($ibx, $self) = @_;
26         my $dir = abs_path($ibx->{inboxdir});
27         if (!defined($dir)) {
28                 warn "W: $ibx->{inboxdir} not watched: $!\n";
29                 return;
30         }
31         my $inot = $self->{inot};
32         my $cur = $self->{pathmap}->{$dir} //= [];
33         my $lock = "$dir/".($ibx->version >= 2 ? 'inbox.lock' : 'ssoma.lock');
34
35         # transfer old subscriptions to the current inbox, cancel the old watch
36         my $old_ibx = $cur->[0];
37         $cur->[0] = $ibx;
38         if ($old_ibx) {
39                 $ibx->{unlock_subs} and
40                         die "BUG: $dir->{unlock_subs} should not exist";
41                 $ibx->{unlock_subs} = $old_ibx->{unlock_subs};
42
43                 # Linux::Inotify2::Watch::name matches if watches are the
44                 # same, no point in replacing a watch of the same name
45                 if ($cur->[1]->name eq $lock) {
46                         $self->{on_unlock}->{$lock} = $ibx;
47                         return;
48                 }
49                 # rare, name changed (v1 inbox converted to v2)
50                 $cur->[1]->cancel; # Linux::Inotify2::Watch::cancel
51         }
52
53         if (my $w = $cur->[1] = $inot->watch($lock, $IN_MODIFY)) {
54                 $self->{on_unlock}->{$w->name} = $ibx;
55         } else {
56                 warn "E: ".ref($inot)."->watch($lock, IN_MODIFY) failed: $!\n";
57                 if ($!{ENOSPC} && $^O eq 'linux') {
58                         warn <<"";
59 I: consider increasing /proc/sys/fs/inotify/max_user_watches
60
61                 }
62         }
63
64         # TODO: detect deleted packs (and possibly other files)
65 }
66
67 sub refresh {
68         my ($self, $pi_config) = @_;
69         $pi_config->each_inbox(\&in2_arm, $self);
70 }
71
72 sub new {
73         my ($class, $pi_config) = @_;
74         my $self = bless {}, $class;
75         my $inot;
76         if ($ino_cls) {
77                 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
78                 my $io = PublicInbox::In2Tie::io($inot);
79                 $self->SUPER::new($io, EPOLLIN | EPOLLET);
80         } else {
81                 require PublicInbox::FakeInotify;
82                 $inot = PublicInbox::FakeInotify->new;
83         }
84         $self->{inot} = $inot;
85         $self->{pathmap} = {}; # inboxdir => [ ibx, watch1, watch2, watch3...]
86         $self->{on_unlock} = {}; # lock path => ibx
87         refresh($self, $pi_config);
88         PublicInbox::FakeInotify::poll_once($self) if !$ino_cls;
89         $self;
90 }
91
92 sub event_step {
93         my ($self) = @_;
94         eval {
95                 my @events = $self->{inot}->read; # Linux::Inotify2::read
96                 my $on_unlock = $self->{on_unlock};
97                 for my $ev (@events) {
98                         if (my $ibx = $on_unlock->{$ev->fullname}) {
99                                 $ibx->on_unlock;
100                         }
101                 }
102         };
103         warn "{inot}->read err: $@\n" if $@;
104 }
105
106 # for graceful shutdown in PublicInbox::Daemon,
107 # just ensure the FD gets closed ASAP and subscribers
108 sub busy { 0 }
109
110 1;