]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD/Async.pm
solver: rewrite to use Qspawn->psgi_qx and pi-httpd.async
[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(Danga::Socket);
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);
29         $self->{cb} = $cb;
30         $self->{cleanup} = $cleanup;
31         $self->watch_read(1);
32         $self;
33 }
34
35 # fires after pending writes are complete:
36 sub restart_read_cb ($) {
37         my ($self) = @_;
38         sub { $self->watch_read(1) }
39 }
40
41 sub main_cb ($$$) {
42         my ($http, $fh, $bref) = @_;
43         sub {
44                 my ($self) = @_;
45                 my $r = sysread($self->{sock}, $$bref, 8192);
46                 if ($r) {
47                         $fh->write($$bref);
48                         unless ($http->{closed}) { # Danga::Socket sets this
49                                 if ($http->{write_buf_size}) {
50                                         $self->watch_read(0);
51                                         $http->write(restart_read_cb($self));
52                                 }
53                                 # stay in watch_read, but let other clients
54                                 # get some work done, too.
55                                 return;
56                         }
57                         # fall through to close below...
58                 } elsif (!defined $r) {
59                         return if $!{EAGAIN} || $!{EINTR};
60                 }
61
62                 # Done! Error handling will happen in $fh->close
63                 # called by the {cleanup} handler
64                 $http->{forward} = undef;
65                 $self->close;
66         }
67 }
68
69 sub async_pass {
70         my ($self, $http, $fh, $bref) = @_;
71         # In case the client HTTP connection ($http) dies, it
72         # will automatically close this ($self) object.
73         $http->{forward} = $self;
74         $fh->write($$bref); # PublicInbox:HTTP::{chunked,identity}_wcb
75         $self->{cb} = main_cb($http, $fh, $bref);
76 }
77
78 sub event_read { $_[0]->{cb}->(@_) }
79 sub event_hup { $_[0]->{cb}->(@_) }
80 sub event_err { $_[0]->{cb}->(@_) }
81
82 sub close {
83         my $self = shift;
84         my $cleanup = $self->{cleanup};
85         $self->{cleanup} = $self->{cb} = undef;
86         $self->SUPER::close(@_);
87
88         # we defer this to the next timer loop since close is deferred
89         PublicInbox::EvCleanup::next_tick($cleanup) if $cleanup;
90 }
91
92 1;