]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/NNTP.pm
ds: set event flags directly at initialization
[public-inbox.git] / lib / PublicInbox / NNTP.pm
index fa412f8c6c8bc893b84ed80cb668cc45522a6fb9..eb1679a76b32072a740f4959829cb0b989dcce01 100644 (file)
@@ -14,7 +14,7 @@ use PublicInbox::Git;
 require PublicInbox::EvCleanup;
 use Email::Simple;
 use POSIX qw(strftime);
-use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
+PublicInbox::DS->import(qw(now msg_more));
 use Digest::SHA qw(sha1_hex);
 use Time::Local qw(timegm timelocal);
 use constant {
@@ -25,8 +25,6 @@ use constant {
        r430 => '430 No article with that message-id',
 };
 
-sub now () { clock_gettime(CLOCK_MONOTONIC) };
-
 my @OVERVIEW = qw(Subject From Date Message-ID References Xref);
 my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines)) . ":\r\n";
 my $LIST_HEADERS = join("\r\n", @OVERVIEW,
@@ -53,11 +51,11 @@ sub next_tick () {
                } else {
                        # pipelined request, we bypassed socket-readiness
                        # checks to get here:
-                       event_read($nntp);
+                       event_step($nntp);
 
                        # maybe there's more pipelined data, or we'll have
                        # to register it for socket-readiness notifications
-                       if (!$nntp->{long_res} && !$nntp->{closed}) {
+                       if (!$nntp->{long_res} && $nntp->{sock}) {
                                check_read($nntp);
                        }
                }
@@ -66,9 +64,8 @@ sub next_tick () {
 
 sub update_idle_time ($) {
        my ($self) = @_;
-        my $sock = $self->{sock} or return;
-       my $fd = fileno($sock);
-       defined $fd and $EXPMAP->{$fd} = [ now(), $self ];
+       my $sock = $self->{sock} or return;
+       $EXPMAP->{fileno($sock)} = [ now(), $self ];
 }
 
 sub expire_old () {
@@ -100,11 +97,10 @@ sub expire_old () {
 sub new ($$$) {
        my ($class, $sock, $nntpd) = @_;
        my $self = fields::new($class);
-       $self->SUPER::new($sock);
+       $self->SUPER::new($sock, PublicInbox::DS::EPOLLIN());
        $self->{nntpd} = $nntpd;
        res($self, '201 ' . $nntpd->{servername} . ' ready - post via email');
        $self->{rbuf} = '';
-       $self->watch_read(1);
        update_idle_time($self);
        $expt ||= PublicInbox::EvCleanup::later(*expire_old);
        $self;
@@ -134,7 +130,7 @@ sub process_line ($$) {
 
        my $res = eval { $req->($self, @args) };
        my $err = $@;
-       if ($err && !$self->{closed}) {
+       if ($err && $self->{sock}) {
                local $/ = "\n";
                chomp($l);
                err($self, 'error from: %s (%s)', $l, $err);
@@ -162,12 +158,12 @@ sub cmd_xgtitle ($;$) {
 
 sub list_overview_fmt ($) {
        my ($self) = @_;
-       do_more($self, $OVERVIEW_FMT);
+       msg_more($self, $OVERVIEW_FMT);
 }
 
 sub list_headers ($;$) {
        my ($self) = @_;
-       do_more($self, $LIST_HEADERS);
+       msg_more($self, $LIST_HEADERS);
 }
 
 sub list_active ($;$) {
@@ -522,8 +518,8 @@ sub simple_body_write ($$) {
        $s->body_set('');
        $body =~ s/^\./../smg;
        $body =~ s/(?<!\r)\n/\r\n/sg;
-       do_more($self, $body);
-       do_more($self, "\r\n") unless $body =~ /\r\n\z/s;
+       msg_more($self, $body);
+       msg_more($self, "\r\n") unless $body =~ /\r\n\z/s;
        '.'
 }
 
@@ -553,8 +549,8 @@ sub cmd_article ($;$) {
        my ($n, $mid, $s) = @$r;
        set_art($self, $art);
        more($self, "220 $n <$mid> article retrieved - head and body follow");
-       do_more($self, _header($s));
-       do_more($self, "\r\n");
+       msg_more($self, _header($s));
+       msg_more($self, "\r\n");
        simple_body_write($self, $s);
 }
 
@@ -565,7 +561,7 @@ sub cmd_head ($;$) {
        my ($n, $mid, $s) = @$r;
        set_art($self, $art);
        more($self, "221 $n <$mid> article retrieved - head follows");
-       do_more($self, _header($s));
+       msg_more($self, _header($s));
        '.'
 }
 
@@ -632,7 +628,7 @@ sub long_response ($$) {
        my $t0 = now();
        $self->{long_res} = sub {
                my $more = eval { $cb->() };
-               if ($@ || $self->{closed}) {
+               if ($@ || !$self->{sock}) {
                        $self->{long_res} = undef;
 
                        if ($@) {
@@ -640,14 +636,14 @@ sub long_response ($$) {
                                    "%s during long response[$fd] - %0.6f",
                                    $@, now() - $t0);
                        }
-                       if ($self->{closed}) {
-                               out($self, " deferred[$fd] aborted - %0.6f",
-                                          now() - $t0);
-                       } else {
+                       if ($self->{sock}) {
                                update_idle_time($self);
                                check_read($self);
+                       } else {
+                               out($self, " deferred[$fd] aborted - %0.6f",
+                                          now() - $t0);
                        }
-               } elsif ($more) { # scalar @{$self->{wbuf}}:
+               } elsif ($more) { # $self->{wbuf}:
                        # no recursion, schedule another call ASAP
                        # but only after all pending writes are done
                        update_idle_time($self);
@@ -765,7 +761,7 @@ sub hdr_searchmsg ($$$$) {
                                $tmp .= $s->{num} . ' ' . $s->$field . "\r\n";
                        }
                        utf8::encode($tmp);
-                       do_more($self, $tmp);
+                       msg_more($self, $tmp);
                        $cur = $msgs->[-1]->{num} + 1;
                });
        }
@@ -917,20 +913,14 @@ sub cmd_xpath ($$) {
        '223 '.join(' ', @paths);
 }
 
-sub res ($$) {
-       my ($self, $line) = @_;
-       do_write($self, $line . "\r\n");
-}
+sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
 
-sub more ($$) {
-       my ($self, $line) = @_;
-       do_more($self, $line . "\r\n");
-}
+sub more ($$) { msg_more($_[0], $_[1] . "\r\n") }
 
 sub do_write ($$) {
-       my ($self, $data) = @_;
-       my $done = $self->write($data);
-       return 0 if $self->{closed};
+       my $self = $_[0];
+       my $done = $self->write(\($_[1]));
+       return 0 unless $self->{sock};
 
        # Do not watch for readability if we have data in the queue,
        # instead re-enable watching for readability when we can
@@ -949,32 +939,16 @@ sub out ($$;@) {
        printf { $self->{nntpd}->{out} } $fmt."\n", @args;
 }
 
-use constant MSG_MORE => ($^O eq 'linux') ? 0x8000 : 0;
+sub event_step {
+       my ($self) = @_;
 
-sub do_more ($$) {
-       my ($self, $data) = @_;
-       if (MSG_MORE && !scalar(@{$self->{wbuf}})) {
-               my $n = send($self->{sock}, $data, MSG_MORE);
-               if (defined $n) {
-                       my $dlen = length($data);
-                       return 1 if $n == $dlen; # all done!
-                       $data = substr($data, $n, $dlen - $n);
-               }
-       }
-       do_write($self, $data);
-}
+       return unless $self->flush_write && $self->{sock};
+       return if $self->{long_res};
 
-sub event_write {
-       my ($self) = @_;
        update_idle_time($self);
-       # only continue watching for readability when we are done writing:
-       if ($self->write(undef) == 1 && !$self->{long_res}) {
-               $self->watch_read(1);
-       }
-}
+       # only read more requests if we've drained the write buffer,
+       # otherwise we can be buffering infinitely w/o backpressure
 
-sub event_read {
-       my ($self) = @_;
        use constant LINE_MAX => 512; # RFC 977 section 2.3
        my $rbuf = \($self->{rbuf});
        my $r;
@@ -1025,9 +999,8 @@ sub check_read {
 
 sub not_idle_long ($$) {
        my ($self, $now) = @_;
-        my $sock = $self->{sock} or return;
-       defined(my $fd = fileno($sock)) or return;
-       my $ary = $EXPMAP->{$fd} or return;
+       my $sock = $self->{sock} or return;
+       my $ary = $EXPMAP->{fileno($sock)} or return;
        my $exp_at = $ary->[0] + $EXPTIME;
        $exp_at > $now;
 }
@@ -1036,7 +1009,7 @@ sub not_idle_long ($$) {
 sub busy {
        my ($self, $now) = @_;
        ($self->{rbuf} ne '' || $self->{long_res} ||
-               scalar(@{$self->{wbuf}}) || not_idle_long($self, $now));
+               $self->{wbuf} || not_idle_long($self, $now));
 }
 
 1;