]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiAuth.pm
thread: avoid Perl5 internal scratchpad target cache
[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 # call net_merge_all_done ->-> do per-WQ-class defined actions
24 package PublicInbox::LeiAuth;
25 use strict;
26 use v5.10.1;
27
28 sub do_auth_atfork { # used by IPC WQ workers
29         my ($self, $wq) = @_;
30         return if $wq->{-wq_worker_nr} != 0; # only first worker calls this
31         my $lei = $wq->{lei};
32         my $net = $lei->{net};
33         if ($net->{-auth_done}) { # from previous worker... (ugly)
34                 $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
35                                 $lei->fail("pkt_do net_merge_continue: $!");
36                 return;
37         }
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                 $net->{-auth_done} = 1;
43                 $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
44                                 die "pkt_do net_merge_continue: $!";
45                 $net->{mics_cached} = $mics if $mics;
46                 $net->{nn_cached} = $nn if $nn;
47         };
48         $lei->fail($@) if $@;
49 }
50
51 sub net_merge_all { # called in wq worker via wq_broadcast
52         my ($wq, $net_new) = @_;
53         my $net = $wq->{lei}->{net};
54         %$net = (%$net, %$net_new);
55 }
56
57 # called by top-level lei-daemon when first worker is done with auth
58 # passes updated net auth info to current workers
59 sub net_merge_continue {
60         my ($wq, $lei, $net_new) = @_;
61         $wq->{-net_new} = $net_new; # for "lei up"
62         $wq->wq_broadcast('PublicInbox::LeiAuth::net_merge_all', $net_new);
63         $wq->net_merge_all_done($lei); # defined per-WQ
64 }
65
66 sub op_merge { # prepares PktOp->pair ops
67         my ($self, $ops, $wq, $lei) = @_;
68         $ops->{net_merge_continue} = [ \&net_merge_continue, $wq, $lei ];
69 }
70
71 sub new { bless \(my $x), __PACKAGE__ }
72
73 1;