]> Sergey Matveev's repositories - public-inbox.git/commitdiff
www: rework async_* to use method table
authorEric Wong <e@yhbt.net>
Thu, 30 Jul 2020 23:43:50 +0000 (23:43 +0000)
committerEric Wong <e@yhbt.net>
Sat, 1 Aug 2020 00:38:13 +0000 (00:38 +0000)
Although the ->async_next method does not take $self as
a receiver, but rather a PublicInbox::HTTP object, we may
still retrieve it to be called with the HTTP object via
UNIVERSAL->can.

lib/PublicInbox/GzipFilter.pm
lib/PublicInbox/Mbox.pm
lib/PublicInbox/MboxGz.pm
lib/PublicInbox/WwwAtomStream.pm
lib/PublicInbox/WwwAttach.pm
lib/PublicInbox/WwwStream.pm

index f1354d2b7eb4e015a54aca61c129df1c70838ab4..20030433fa1e7ded0a95f3dc97ba27e0f8d5c828 100644 (file)
@@ -7,8 +7,8 @@
 #
 # In newer versions, public-inbox-httpd supports a backpressure-aware
 # pull/push model which also accounts for slow git blob storage.
-# {async_next} callbacks only run when the DS {wbuf} is drained
-# {async_eml} callbacks only run when a blob arrives from git.
+# async_next callbacks only run when the DS {wbuf} is drained
+# async_eml callbacks only run when a blob arrives from git.
 #
 # We continue to support getline+close for generic PSGI servers.
 package PublicInbox::GzipFilter;
@@ -46,18 +46,16 @@ sub gz_or_noop {
 sub gzf_maybe ($$) { bless { gz => gz_or_noop(@_) }, __PACKAGE__ }
 
 sub psgi_response {
-       my ($self, $code, $res_hdr, $next_cb, $eml_cb) = @_;
+       my ($self, $code, $res_hdr) = @_;
        my $env = $self->{env};
        $self->{gz} //= gz_or_noop($res_hdr, $env);
        if ($env->{'pi-httpd.async'}) {
-               $self->{async_next} = $next_cb;
-               $self->{async_eml} = $eml_cb;
                my $http = $env->{'psgix.io'}; # PublicInbox::HTTP
                $http->{forward} = $self;
                sub {
                        my ($wcb) = @_; # -httpd provided write callback
                        $self->{http_out} = $wcb->([$code, $res_hdr]);
-                       $next_cb->($http); # start stepping
+                       $self->can('async_next')->($http); # start stepping
                };
        } else { # generic PSGI code path
                [ $code, $res_hdr, $self ];
@@ -172,12 +170,12 @@ sub async_blob_cb { # git->cat_async callback
                # it's possible to have TOCTOU if an admin runs
                # public-inbox-(edit|purge), just move onto the next message
                warn "E: $smsg->{blob} missing in $self->{-inbox}->{inboxdir}\n";
-               return $http->next_step($self->{async_next});
+               return $http->next_step($self->can('async_next'));
        }
        $smsg->{blob} eq $oid or bail($self, "BUG: $smsg->{blob} != $oid");
-       eval { $self->{async_eml}->($self, PublicInbox::Eml->new($bref)) };
+       eval { $self->async_eml(PublicInbox::Eml->new($bref)) };
        bail($self, "E: async_eml: $@") if $@;
-       $http->next_step($self->{async_next});
+       $http->next_step($self->can('async_next'));
 }
 
 sub smsg_blob {
index 115321c610f70be296f46dcf1b2de38512fcd006..fc83a893c60ab384f21061d3b2dff12edb6f365c 100644 (file)
@@ -29,7 +29,7 @@ sub getline {
        }
 }
 
-# called by PublicInbox::DS::write
+# called by PublicInbox::DS::write after http->next_step
 sub async_next {
        my ($http) = @_; # PublicInbox::HTTP
        my $ctx = $http->{forward} or return; # client aborted
@@ -40,7 +40,7 @@ sub async_next {
        warn "E: $@" if $@;
 }
 
-sub async_eml { # ->{async_eml} for async_blob_cb
+sub async_eml { # for async_blob_cb
        my ($ctx, $eml) = @_;
        my $smsg = delete $ctx->{smsg};
        # next message
@@ -87,7 +87,7 @@ sub emit_raw {
        my $smsg = $ctx->{smsg} = $over->next_by_mid(@$mip) or return;
        my $res_hdr = res_hdr($ctx, $smsg->{subject});
        bless $ctx, __PACKAGE__;
-       $ctx->psgi_response(200, $res_hdr, \&async_next, \&async_eml);
+       $ctx->psgi_response(200, $res_hdr);
 }
 
 sub msg_hdr ($$;$) {
index 967af9c68ad1bf9e4961fbb86aa4ddc1bd2ddee4..913be6e4221550b6bec29753a43c8a3183f49d46 100644 (file)
@@ -29,8 +29,7 @@ sub mbox_gz {
        bless $self, __PACKAGE__;
        my $res_hdr = [ 'Content-Type' => 'application/gzip',
                'Content-Disposition' => "inline; filename=$fn.mbox.gz" ];
-       $self->psgi_response(200, $res_hdr, \&async_next,
-                               \&PublicInbox::Mbox::async_eml);
+       $self->psgi_response(200, $res_hdr);
 }
 
 # called by Plack::Util::foreach or similar (generic PSGI)
@@ -47,4 +46,6 @@ sub getline {
        $self->zflush;
 }
 
+no warnings 'once';
+*async_eml = \&PublicInbox::Mbox::async_eml;
 1;
index 2f9b953b2829cea360ea6627c044a56c89a63739..1ed806fd42543fcf4ad650577f8ad369732fbccb 100644 (file)
@@ -35,7 +35,7 @@ sub async_next ($) {
        warn "E: $@" if $@;
 }
 
-sub async_eml { # ->{async_eml} for async_blob_cb
+sub async_eml { # for async_blob_cb
        my ($ctx, $eml) = @_;
        my $smsg = delete $ctx->{smsg};
        $ctx->{http_out}->write($ctx->translate(feed_entry($ctx, $smsg, $eml)))
@@ -45,7 +45,7 @@ sub response {
        my ($class, $ctx, $code, $cb) = @_;
        my $res_hdr = [ 'Content-Type' => 'application/atom+xml' ];
        $class->new($ctx, $cb);
-       $ctx->psgi_response($code, $res_hdr, \&async_next, \&async_eml);
+       $ctx->psgi_response($code, $res_hdr);
 }
 
 # called once for each message by PSGI server
index 20417295efd960194d97eb24d73c40bb736cacda..0b2cda900ea00be6c181c288f3a5379f82588d87 100644 (file)
@@ -35,7 +35,7 @@ sub get_attach_i { # ->each_part callback
        $res->[2]->[0] = $part;
 }
 
-sub async_eml { # ->{async_eml} for async_blob_cb
+sub async_eml { # for async_blob_cb
        my ($ctx, $eml) = @_;
        eval { $eml->each_part(\&get_attach_i, $ctx, 1) };
        if ($@) {
@@ -55,8 +55,6 @@ sub async_next {
 sub scan_attach ($) { # public-inbox-httpd only
        my ($ctx) = @_;
        $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
-       $ctx->{async_eml} = \&async_eml;
-       $ctx->{async_next} = \&async_next;
        $ctx->smsg_blob($ctx->{smsg});
 }
 
index 23b03f0e8dc49ddf8780964a9f988fd41d99b6dc..d79770ed1d82b8dbd484ca84dbf445962116bef8 100644 (file)
@@ -28,7 +28,7 @@ sub init {
        bless $ctx, __PACKAGE__;
 }
 
-sub async_eml { # ->{async_eml} for async_blob_cb
+sub async_eml { # for async_blob_cb
        my ($ctx, $eml) = @_;
        $ctx->{http_out}->write($ctx->translate($ctx->{cb}->($ctx, $eml)));
 }
@@ -198,7 +198,7 @@ sub aresponse {
        my ($ctx, $code, $cb) = @_;
        my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
        init($ctx, $cb);
-       $ctx->psgi_response($code, $res_hdr, \&async_next, \&async_eml);
+       $ctx->psgi_response($code, $res_hdr);
 }
 
 1;