]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiAuth.pm
lei q: ensure wq workers shutdown on IMAP auth failures
[public-inbox.git] / lib / PublicInbox / LeiAuth.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Authentication worker for anything that needs auth for read/write IMAP
5 # (eventually for read-only NNTP access)
6 package PublicInbox::LeiAuth;
7 use strict;
8 use v5.10.1;
9 use PublicInbox::PktOp qw(pkt_do);
10
11 sub do_auth_atfork { # used by IPC WQ workers
12         my ($self, $wq) = @_;
13         return if $wq->{-wq_worker_nr} != 0;
14         my $lei = $wq->{lei};
15         my $net = $lei->{net};
16         eval {
17                 my $mics = $net->imap_common_init($lei);
18                 my $nn = $net->nntp_common_init($lei);
19                 pkt_do($lei->{pkt_op_p}, 'net_merge', $net) or
20                                 die "pkt_do net_merge: $!";
21                 $net->{mics_cached} = $mics if $mics;
22                 $net->{nn_cached} = $nn if $nn;
23         };
24         $lei->fail($@) if $@;
25 }
26
27 sub net_merge_done1 { # bump merge-count in top-level lei-daemon
28         my ($wq) = @_;
29         return if ++$wq->{nr_net_merge_done} != $wq->{-wq_nr_workers};
30         $wq->net_merge_complete; # defined per wq-class (e.g. LeiImport)
31 }
32
33 sub net_merge_all { # called in wq worker via wq_broadcast
34         my ($wq, $net_new) = @_;
35         my $net = $wq->{lei}->{net};
36         %$net = (%$net, %$net_new);
37         pkt_do($wq->{lei}->{pkt_op_p}, 'net_merge_done1') or
38                 die "pkt_op_do net_merge_done1: $!";
39 }
40
41 # called by top-level lei-daemon when first worker is done with auth
42 sub net_merge_continue {
43         my ($wq, $net_new) = @_;
44         $wq->wq_broadcast('net_merge_all', $net_new);
45 }
46
47 sub op_merge { # prepares PktOp->pair ops
48         my ($self, $ops, $wq) = @_;
49         $ops->{net_merge} = [ \&net_merge_continue, $wq ];
50         $ops->{net_merge_done1} = [ \&net_merge_done1, $wq ];
51 }
52
53 sub new { bless \(my $x), __PACKAGE__ }
54
55 1;