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