]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD/Async.pm
httpd/async: remove EINTR check
[public-inbox.git] / lib / PublicInbox / HTTPD / Async.pm
1 # Copyright (C) 2016-2018 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 is is intended for use with read-only pipes.
8 package PublicInbox::HTTPD::Async;
9 use strict;
10 use warnings;
11 use base qw(PublicInbox::DS);
12 use fields qw(cb cleanup);
13 require PublicInbox::EvCleanup;
14
15 sub new {
16         my ($class, $io, $cb, $cleanup) = @_;
17
18         # no $io? call $cb at the top of the next event loop to
19         # avoid recursion:
20         unless (defined($io)) {
21                 PublicInbox::EvCleanup::asap($cb) if $cb;
22                 PublicInbox::EvCleanup::next_tick($cleanup) if $cleanup;
23                 return;
24         }
25
26         my $self = fields::new($class);
27         IO::Handle::blocking($io, 0);
28         $self->SUPER::new($io, PublicInbox::DS::EPOLLIN());
29         $self->{cb} = $cb;
30         $self->{cleanup} = $cleanup;
31         $self;
32 }
33
34 sub restart_read ($) { $_[0]->watch(PublicInbox::DS::EPOLLIN()) }
35
36 # fires after pending writes are complete:
37 sub restart_read_cb ($) {
38         my ($self) = @_;
39         sub { restart_read($self) }
40 }
41
42 sub main_cb ($$$) {
43         my ($http, $fh, $bref) = @_;
44         sub {
45                 my ($self) = @_;
46                 my $r = sysread($self->{sock}, $$bref, 8192);
47                 if ($r) {
48                         $fh->write($$bref);
49                         if ($http->{sock}) { # !closed
50                                 if ($http->{wbuf}) {
51                                         $self->watch(0);
52                                         $http->write(restart_read_cb($self));
53                                 }
54                                 # stay in EPOLLIN, but let other clients
55                                 # get some work done, too.
56                                 return;
57                         }
58                         # fall through to close below...
59                 } elsif (!defined $r) {
60                         return restart_read($self) if $!{EAGAIN};
61                 }
62
63                 # Done! Error handling will happen in $fh->close
64                 # called by the {cleanup} handler
65                 $http->{forward} = undef;
66                 $self->close;
67         }
68 }
69
70 sub async_pass {
71         my ($self, $http, $fh, $bref) = @_;
72         # In case the client HTTP connection ($http) dies, it
73         # will automatically close this ($self) object.
74         $http->{forward} = $self;
75         $fh->write($$bref); # PublicInbox:HTTP::{chunked,identity}_wcb
76         $self->{cb} = main_cb($http, $fh, $bref);
77 }
78
79 sub event_step { $_[0]->{cb}->(@_) }
80
81 sub close {
82         my $self = shift;
83         my $cleanup = $self->{cleanup};
84         $self->{cleanup} = $self->{cb} = undef;
85         $self->SUPER::close(@_);
86
87         # we defer this to the next timer loop since close is deferred
88         PublicInbox::EvCleanup::next_tick($cleanup) if $cleanup;
89 }
90
91 1;