]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiAuth.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / LeiAuth.pm
index 6593ba515db4bfd82ecc7b65e82c65635b139c7a..9b09cecf05cb1f6b54a86720cf695d34d0b75f07 100644 (file)
@@ -3,79 +3,71 @@
 
 # Authentication worker for anything that needs auth for read/write IMAP
 # (eventually for read-only NNTP access)
+#
+# timelines
+# lei-daemon              |  LeiAuth worker #0      | other WQ workers
+# ----------------------------------------------------------
+# spawns all workers ---->[ workers all start and run ipc_atfork_child ]
+#                         | do_auth_atfork          | wq_worker_loop sleep
+#                         | # reads .netrc          |
+#                         | # queries git-credential|
+#                         | send net_merge_continue |
+#                         |         |               |
+#                         |         v               |
+# recv net_merge_continue <---------/               |
+#            |            |                         |
+#            v            |                         |
+# broadcast net_merge_all [ all workers (including LeiAuth worker #0) ]
+#                         [ LeiAuth worker #0 becomes just another WQ worker ]
+#                         |
+# call net_merge_all_done ->-> do per-WQ-class defined actions
 package PublicInbox::LeiAuth;
 use strict;
 use v5.10.1;
-use parent qw(PublicInbox::IPC);
-use PublicInbox::PktOp qw(pkt_do);
-use PublicInbox::NetReader;
 
-sub nrd_merge {
-       my ($lei, $nrd_new) = @_;
-       if ($lei->{pkt_op_p}) { # from lei_convert worker
-               pkt_do($lei->{pkt_op_p}, 'nrd_merge', $nrd_new);
-       } else { # single lei-daemon consumer
-               my $self = $lei->{auth} or return; # client disconnected
-               my $nrd = $self->{nrd};
-               %$nrd = (%$nrd, %$nrd_new);
+sub do_auth_atfork { # used by IPC WQ workers
+       my ($self, $wq) = @_;
+       return if $wq->{-wq_worker_nr} != 0; # only first worker calls this
+       my $lei = $wq->{lei};
+       my $net = $lei->{net};
+       if ($net->{-auth_done}) { # from previous worker... (ugly)
+               $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
+                               $lei->fail("pkt_do net_merge_continue: $!");
+               return;
        }
+       eval { # fill auth info (may prompt user or read netrc)
+               my $mics = $net->imap_common_init($lei);
+               my $nn = $net->nntp_common_init($lei);
+               # broadcast successful auth info to lei-daemon:
+               $net->{-auth_done} = 1;
+               $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
+                               die "pkt_do net_merge_continue: $!";
+               $net->{mics_cached} = $mics if $mics;
+               $net->{nn_cached} = $nn if $nn;
+       };
+       $lei->fail($@) if $@;
 }
 
-sub do_auth { # called via wq_io_do
-       my ($self) = @_;
-       my ($lei, $nrd) = @$self{qw(lei nrd)};
-       $nrd->imap_common_init($lei);
-       nrd_merge($lei, $nrd); # tell lei-daemon updated auth info
-}
-
-sub do_finish_auth { # dwaitpid callback
-       my ($arg, $pid) = @_;
-       my ($self, $lei, $post_auth_cb, @args) = @$arg;
-       $? ? $lei->dclose : $post_auth_cb->(@args);
-}
-
-sub auth_eof {
-       my ($lei, $post_auth_cb, @args) = @_;
-       my $self = delete $lei->{auth} or return;
-       $self->wq_wait_old(\&do_finish_auth, $lei, $post_auth_cb, @args);
+sub net_merge_all { # called in wq worker via wq_broadcast
+       my ($wq, $net_new) = @_;
+       my $net = $wq->{lei}->{net};
+       %$net = (%$net, %$net_new);
 }
 
-sub auth_start {
-       my ($self, $lei, $post_auth_cb, @args) = @_;
-       $lei->_lei_cfg(1); # workers may need to read config
-       my $ops = {
-               '!' => [ $lei->can('fail_handler'), $lei ],
-               '|' => [ $lei->can('sigpipe_handler'), $lei ],
-               'x_it' => [ $lei->can('x_it'), $lei ],
-               'child_error' => [ $lei->can('child_error'), $lei ],
-               'nrd_merge' => [ \&nrd_merge, $lei ],
-               '' => [ \&auth_eof, $lei, $post_auth_cb, @args ],
-       };
-       ($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
-       $self->wq_workers_start('lei_auth', 1, $lei->oldset, {lei => $lei});
-       my $op = delete $lei->{pkt_op_c};
-       delete $lei->{pkt_op_p};
-       $self->wq_io_do('do_auth', []);
-       $self->wq_close(1);
-       $lei->event_step_init; # wait for shutdowns
-       if ($lei->{oneshot}) {
-               while ($op->{sock}) { $op->event_step }
-       }
+# called by top-level lei-daemon when first worker is done with auth
+# passes updated net auth info to current workers
+sub net_merge_continue {
+       my ($wq, $lei, $net_new) = @_;
+       $wq->{-net_new} = $net_new; # for "lei up"
+       $wq->wq_broadcast('PublicInbox::LeiAuth::net_merge_all', $net_new);
+       $wq->net_merge_all_done($lei); # defined per-WQ
 }
 
-sub ipc_atfork_child {
-       my ($self) = @_;
-       # prevent {sock} from being closed in lei_atfork_child:
-       my $s = delete $self->{lei}->{sock};
-       delete $self->{lei}->{auth}; # drop circular ref
-       $self->{lei}->lei_atfork_child;
-       $self->{lei}->{sock} = $s if $s;
-       $self->SUPER::ipc_atfork_child;
+sub op_merge { # prepares PktOp->pair ops
+       my ($self, $ops, $wq, $lei) = @_;
+       $ops->{net_merge_continue} = [ \&net_merge_continue, $wq, $lei ];
 }
 
-sub new {
-       my ($cls, $nrd) = @_;
-       bless { nrd => $nrd }, $cls;
-}
+sub new { bless \(my $x), __PACKAGE__ }
 
 1;