]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiAuth.pm
lei_auth: diagram for current behavior
[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 #
7 # timelines
8 # lei-daemon              |  LeiAuth worker #0      | other WQ workers
9 # ----------------------------------------------------------
10 # spawns all workers ---->[ workers all start and run ipc_atfork_child ]
11 #                         | do_auth_atfork          | wq_worker_loop sleep
12 #                         | # reads .netrc          |
13 #                         | # queries git-credential|
14 #                         | send net_merge_continue |
15 #                         |         |               |
16 #                         |         v               |
17 # recv net_merge_continue <---------/               |
18 #            |            |                         |
19 #            v            |                         |
20 # broadcast net_merge_all [ all workers (including LeiAuth worker #0) ]
21 #                         [ LeiAuth worker #0 becomes just another WQ worker ]
22 #                         |
23 #                         | each worker sends net_merge_done1 to lei-daemon
24 #                         |              |  | ... |
25 #                         |              v  v     v
26 # recv net_merge_done1 <--<-------<------/--/--<--/
27 #
28 # call net_merge_all_done ->-> do per-class defined actions
29 package PublicInbox::LeiAuth;
30 use strict;
31 use v5.10.1;
32
33 sub do_auth_atfork { # used by IPC WQ workers
34         my ($self, $wq) = @_;
35         return if $wq->{-wq_worker_nr} != 0; # only first worker calls this
36         my $lei = $wq->{lei};
37         my $net = $lei->{net};
38         eval { # fill auth info (may prompt user or read netrc)
39                 my $mics = $net->imap_common_init($lei);
40                 my $nn = $net->nntp_common_init($lei);
41                 # broadcast successful auth info to lei-daemon:
42                 $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
43                                 die "pkt_do net_merge_continue: $!";
44                 $net->{mics_cached} = $mics if $mics;
45                 $net->{nn_cached} = $nn if $nn;
46         };
47         $lei->fail($@) if $@;
48 }
49
50 sub net_merge_done1 { # bump merge-count in top-level lei-daemon
51         my ($wq) = @_;
52         return if ++$wq->{nr_net_merge_done} != $wq->{-wq_nr_workers};
53         $wq->net_merge_all_done; # defined per wq-class (e.g. LeiImport)
54 }
55
56 sub net_merge_all { # called in wq worker via wq_broadcast
57         my ($wq, $net_new) = @_;
58         my $net = $wq->{lei}->{net};
59         %$net = (%$net, %$net_new);
60         # notify daemon we're ready
61         $wq->{lei}->{pkt_op_p}->pkt_do('net_merge_done1') or
62                 die "pkt_op_do net_merge_done1: $!";
63 }
64
65 # called by top-level lei-daemon when first worker is done with auth
66 sub net_merge_continue {
67         my ($wq, $net_new) = @_;
68         $wq->wq_broadcast('net_merge_all', $net_new); # pass to current workers
69 }
70
71 sub op_merge { # prepares PktOp->pair ops
72         my ($self, $ops, $wq) = @_;
73         $ops->{net_merge_continue} = [ \&net_merge_continue, $wq ];
74         $ops->{net_merge_done1} = [ \&net_merge_done1, $wq ];
75 }
76
77 sub new { bless \(my $x), __PACKAGE__ }
78
79 1;