]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD/Async.pm
7238650aff978605c53213fdd5586f32465f785b
[public-inbox.git] / lib / PublicInbox / HTTPD / Async.pm
1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # XXX This is a totally unstable API for public-inbox internal use only
5 # This is exposed via the 'pi-httpd.async' key in the PSGI env hash.
6 # The name of this key is not even stable!
7 # Currently intended for use with read-only pipes with expensive
8 # processes such as git-http-backend(1), cgit(1)
9 #
10 # fields:
11 # http: PublicInbox::HTTP ref
12 # fh: PublicInbox::HTTP::{Identity,Chunked} ref (can ->write + ->close)
13 # cb: initial read callback
14 # arg: arg for {cb}
15 # end_obj: CODE or object which responds to ->event_step when ->close is called
16 package PublicInbox::HTTPD::Async;
17 use strict;
18 use parent qw(PublicInbox::DS);
19 use Errno qw(EAGAIN);
20 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
21
22 # This is called via: $env->{'pi-httpd.async'}->()
23 # $io is a read-only pipe ($rpipe) for now, but may be a
24 # bidirectional socket in the future.
25 sub new {
26         my ($class, $io, $cb, $arg, $end_obj) = @_;
27
28         # no $io? call $cb at the top of the next event loop to
29         # avoid recursion:
30         unless (defined($io)) {
31                 PublicInbox::DS::requeue($cb ? $cb : $arg);
32                 die '$end_obj unsupported w/o $io' if $end_obj;
33                 return;
34         }
35         my $self = bless {
36                 cb => $cb, # initial read callback
37                 arg => $arg, # arg for $cb
38                 end_obj => $end_obj, # like END{}, can ->event_step
39         }, $class;
40         my $pp = tied *$io;
41         $pp->{fh}->blocking(0) // die "$io->blocking(0): $!";
42         $self->SUPER::new($io, EPOLLIN | EPOLLET);
43 }
44
45 sub event_step {
46         my ($self) = @_;
47         if (my $cb = delete $self->{cb}) {
48                 # this may call async_pass when headers are done
49                 $cb->(my $refcnt_guard = delete $self->{arg});
50         } elsif (my $sock = $self->{sock}) {
51                 my $http = $self->{http};
52                 # $self->{sock} is a read pipe for git-http-backend or cgit
53                 # and 65536 is the default Linux pipe size
54                 my $r = sysread($sock, my $buf, 65536);
55                 if ($r) {
56                         $self->{fh}->write($buf); # may call $http->close
57                         if ($http->{sock}) { # !closed
58                                 $self->requeue;
59                                 # let other clients get some work done, too
60                                 return;
61                         }
62
63                         # else: fall through to close below...
64                 } elsif (!defined $r && $! == EAGAIN) {
65                         return; # EPOLLET means we'll be notified
66                 }
67
68                 # Done! Error handling will happen in $self->{fh}->close
69                 # called by end_obj->event_step handler
70                 delete $http->{forward};
71                 $self->close; # queues end_obj->event_step to be called
72         } # else { # we may've been requeued but closed by $http
73 }
74
75 # once this is called, all data we read is passed to the
76 # to the PublicInbox::HTTP instance ($http) via $fh->write
77 sub async_pass {
78         my ($self, $http, $fh, $bref) = @_;
79         # In case the client HTTP connection ($http) dies, it
80         # will automatically close this ($self) object.
81         $http->{forward} = $self;
82
83         # write anything we overread when we were reading headers
84         $fh->write($$bref); # PublicInbox:HTTP::{chunked,identity}_wcb
85
86         # we're done with this, free this memory up ASAP since the
87         # calls after this may use much memory:
88         $$bref = undef;
89
90         $self->{http} = $http;
91         $self->{fh} = $fh;
92
93         # either hit EAGAIN or ->requeue to keep EPOLLET happy
94         event_step($self);
95 }
96
97 # may be called as $forward->close in PublicInbox::HTTP or EOF (event_step)
98 sub close {
99         my $self = $_[0];
100         $self->SUPER::close; # DS::close
101
102         # we defer this to the next timer loop since close is deferred
103         if (my $end_obj = delete $self->{end_obj}) {
104                 # this calls $end_obj->event_step
105                 # (likely PublicInbox::Qspawn::event_step,
106                 #  NOT PublicInbox::HTTPD::Async::event_step)
107                 PublicInbox::DS::requeue($end_obj);
108         }
109 }
110
111 1;