]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD/Async.pm
d5628ee8d7ca8b457f9999ef118b18553dab61fd
[public-inbox.git] / lib / PublicInbox / HTTPD / Async.pm
1 # Copyright (C) 2016-2019 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(cb end);
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, $end) = @_;
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);
27                 die '$end unsupported w/o $io' if $end;
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, later replaced by main_cb
35         $self->{end} = $end; # like END {}, but only for this object
36         $self;
37 }
38
39 sub main_cb ($$) {
40         my ($http, $fh) = @_;
41         sub {
42                 my ($self) = @_;
43                 # $self->{sock} is a read pipe for git-http-backend or cgit
44                 # and 65536 is the default Linux pipe size
45                 my $r = sysread($self->{sock}, my $buf, 65536);
46                 if ($r) {
47                         $fh->write($buf); # may call $http->close
48                         if ($http->{sock}) { # !closed
49                                 $self->requeue;
50                                 # let other clients get some work done, too
51                                 return;
52                         }
53
54                         # else: fall through to close below...
55                 } elsif (!defined $r && $! == EAGAIN) {
56                         return; # EPOLLET means we'll be notified
57                 }
58
59                 # Done! Error handling will happen in $fh->close
60                 # called by the {end} handler
61                 delete $http->{forward};
62                 $self->close; # queues ->{end} to be called
63         }
64 }
65
66 # once this is called, all data we read is passed to the
67 # to the PublicInbox::HTTP instance ($http) via $fh->write
68 sub async_pass {
69         my ($self, $http, $fh, $bref) = @_;
70         # In case the client HTTP connection ($http) dies, it
71         # will automatically close this ($self) object.
72         $http->{forward} = $self;
73
74         # write anything we overread when we were reading headers
75         $fh->write($$bref); # PublicInbox:HTTP::{chunked,identity}_wcb
76
77         # we're done with this, free this memory up ASAP since the
78         # calls after this may use much memory:
79         $$bref = undef;
80
81         # replace the header read callback with the main one
82         my $cb = $self->{cb} = main_cb($http, $fh);
83         $cb->($self); # either hit EAGAIN or ->requeue to keep EPOLLET happy
84 }
85
86 sub event_step {
87         # {cb} may be undef after ->requeue due to $http->close happening
88         my $cb = $_[0]->{cb} or return;
89         $cb->(@_);
90 }
91
92 # may be called as $forward->close in PublicInbox::HTTP or EOF (main_cb)
93 sub close {
94         my $self = $_[0];
95         delete $self->{cb};
96         $self->SUPER::close; # DS::close
97
98         # we defer this to the next timer loop since close is deferred
99         if (my $end = delete $self->{end}) {
100                 PublicInbox::DS::requeue($end);
101         }
102 }
103
104 1;