]> Sergey Matveev's repositories - public-inbox.git/commitdiff
www: manifest.js.gz generation no longer hogs event loop
authorEric Wong <e@80x24.org>
Wed, 9 Sep 2020 06:26:11 +0000 (06:26 +0000)
committerEric Wong <e@80x24.org>
Thu, 10 Sep 2020 19:45:18 +0000 (19:45 +0000)
It's still as slow as before with hundreds/thousands of inboxes,
but at least it's fair.  Future changes will allow it to be
cached and memoized with persistent HTTP servers.

MANIFEST
lib/PublicInbox/ManifestJsGz.pm [new file with mode: 0644]
lib/PublicInbox/WWW.pm
lib/PublicInbox/WwwListing.pm
t/www_listing.t

index 44670c7ee0b3e574670ccf3175c8644ee0b20872..0e225b6a99ed65d7dc08a59131b4e9a741ac4e51 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -157,6 +157,7 @@ lib/PublicInbox/Lock.pm
 lib/PublicInbox/MDA.pm
 lib/PublicInbox/MID.pm
 lib/PublicInbox/MIME.pm
+lib/PublicInbox/ManifestJsGz.pm
 lib/PublicInbox/Mbox.pm
 lib/PublicInbox/MboxGz.pm
 lib/PublicInbox/MsgIter.pm
diff --git a/lib/PublicInbox/ManifestJsGz.pm b/lib/PublicInbox/ManifestJsGz.pm
new file mode 100644 (file)
index 0000000..328303c
--- /dev/null
@@ -0,0 +1,153 @@
+# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+package PublicInbox::ManifestJsGz;
+use strict;
+use v5.10.1;
+use Digest::SHA ();
+use File::Spec ();
+use bytes (); # length
+use PublicInbox::Inbox;
+use PublicInbox::Git;
+use IO::Compress::Gzip qw(gzip);
+use HTTP::Date qw(time2str);
+*try_cat = \&PublicInbox::Inbox::try_cat;
+
+our $json;
+for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
+       eval "require $mod" or next;
+       # ->ascii encodes non-ASCII to "\uXXXX"
+       $json = $mod->new->ascii(1) and last;
+}
+
+sub response {
+       my ($env, $list) = @_;
+       $json or return [ 404, [], [] ];
+       my $self = bless {
+               -abs2urlpath => {},
+               -mtime => 0,
+               manifest => {},
+               -list => $list,
+               psgi_env => $env,
+       }, __PACKAGE__;
+
+       # PSGI server will call this immediately and give us a callback (-wcb)
+       sub {
+               $self->{-wcb} = $_[0]; # HTTP write callback
+               iterate_start($self);
+       };
+}
+
+sub fingerprint ($) {
+       my ($git) = @_;
+       # TODO: convert to qspawn for fairness when there's
+       # thousands of repos
+       my ($fh, $pid) = $git->popen('show-ref');
+       my $dig = Digest::SHA->new(1);
+       while (read($fh, my $buf, 65536)) {
+               $dig->add($buf);
+       }
+       close $fh;
+       waitpid($pid, 0);
+       return if $?; # empty, uninitialized git repo
+       $dig->hexdigest;
+}
+
+sub manifest_add ($$;$$) {
+       my ($self, $ibx, $epoch, $default_desc) = @_;
+       my $url_path = "/$ibx->{name}";
+       my $git_dir = $ibx->{inboxdir};
+       if (defined $epoch) {
+               $git_dir .= "/git/$epoch.git";
+               $url_path .= "/git/$epoch.git";
+       }
+       return unless -d $git_dir;
+       my $git = PublicInbox::Git->new($git_dir);
+       my $fingerprint = fingerprint($git) or return; # no empty repos
+
+       chomp(my $owner = $git->qx('config', 'gitweb.owner'));
+       chomp(my $desc = try_cat("$git_dir/description"));
+       utf8::decode($owner);
+       utf8::decode($desc);
+       $owner = undef if $owner eq '';
+       $desc = 'Unnamed repository' if $desc eq '';
+
+       # templates/hooks--update.sample and git-multimail in git.git
+       # only match "Unnamed repository", not the full contents of
+       # templates/this--description in git.git
+       if ($desc =~ /\AUnnamed repository/) {
+               $desc = "$default_desc [epoch $epoch]" if defined($epoch);
+       }
+
+       my $reference;
+       chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
+       if ($alt) {
+               # n.b.: GitPython doesn't seem to handle comments or C-quoted
+               # strings like native git does; and we don't for now, either.
+               my @alt = split(/\n+/, $alt);
+
+               # grokmirror only supports 1 alternate for "reference",
+               if (scalar(@alt) == 1) {
+                       my $objdir = "$git_dir/objects";
+                       $reference = File::Spec->rel2abs($alt[0], $objdir);
+                       $reference =~ s!/[^/]+/?\z!!; # basename
+               }
+       }
+       $self->{-abs2urlpath}->{$git_dir} = $url_path;
+       my $modified = $git->modified;
+       if ($modified > $self->{-mtime}) {
+               $self->{-mtime} = $modified;
+       }
+       $self->{manifest}->{$url_path} = {
+               owner => $owner,
+               reference => $reference,
+               description => $desc,
+               modified => $modified,
+               fingerprint => $fingerprint,
+       };
+}
+
+sub iterate_start {
+       my ($self) = @_;
+       if (my $async = $self->{psgi_env}->{'pi-httpd.async'}) {
+               # PublicInbox::HTTPD::Async->new
+               $async->(undef, undef, $self);
+       } else {
+               event_step($self) while $self->{-wcb};
+       }
+}
+
+sub event_step {
+       my ($self) = @_;
+       while (my $ibx = shift(@{$self->{-list}})) {
+               eval {
+                       if (defined(my $max = $ibx->max_git_epoch)) {
+                               my $desc = $ibx->description;
+                               for my $epoch (0..$max) {
+                                       manifest_add($self, $ibx, $epoch, $desc)
+                               }
+                       } else {
+                               manifest_add($self, $ibx);
+                       }
+               };
+               warn "E: $@" if $@;
+               if (my $async = $self->{psgi_env}->{'pi-httpd.async'}) {
+                       # PublicInbox::HTTPD::Async->new
+                       $async->(undef, undef, $self);
+               }
+               return; # more steps needed
+       }
+       my $abs2urlpath = delete $self->{-abs2urlpath};
+       my $wcb = delete $self->{-wcb};
+       my $manifest = delete $self->{manifest};
+       while (my ($url_path, $repo) = each %$manifest) {
+               defined(my $abs = $repo->{reference}) or next;
+               $repo->{reference} = $abs2urlpath->{$abs};
+       }
+       $manifest = $json->encode($manifest);
+       gzip(\$manifest => \(my $out));
+       $wcb->([ 200, [ qw(Content-Type application/gzip),
+                'Last-Modified', time2str($self->{-mtime}),
+                'Content-Length', bytes::length($out) ], [ $out ] ]);
+}
+
+1;
index 2ea5d80db7817a8d2d12a94f5f1b38c6c92833b1..93ab3c9d82c7ee12b8139420f07dee6d2cd13396 100644 (file)
@@ -509,8 +509,8 @@ sub get_inbox_manifest ($$$) {
        my ($ctx, $inbox, $key) = @_;
        my $r404 = invalid_inbox($ctx, $inbox);
        return $r404 if $r404;
-       require PublicInbox::WwwListing;
-       PublicInbox::WwwListing::js($ctx->{env}, [$ctx->{-inbox}]);
+       require PublicInbox::ManifestJsGz;
+       PublicInbox::ManifestJsGz::response($ctx->{env}, [$ctx->{-inbox}]);
 }
 
 sub get_attach {
index 0be3764c1a3ee28ab908200497336a1a9b42bf77..12b0d9ad5f3f0bedae69919f3a9a4b0de557ff1f 100644 (file)
@@ -5,24 +5,11 @@
 # Used by PublicInbox::WWW
 package PublicInbox::WwwListing;
 use strict;
-use warnings;
 use PublicInbox::Hval qw(ascii_html prurl fmt_ts);
 use PublicInbox::Linkify;
-use PublicInbox::View;
-use PublicInbox::Inbox;
 use PublicInbox::GzipFilter qw(gzf_maybe);
+use PublicInbox::ManifestJsGz;
 use bytes (); # bytes::length
-use HTTP::Date qw(time2str);
-use Digest::SHA ();
-use File::Spec ();
-use IO::Compress::Gzip qw(gzip);
-*try_cat = \&PublicInbox::Inbox::try_cat;
-our $json;
-for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
-       eval "require $mod" or next;
-       # ->ascii encodes non-ASCII to "\uXXXX"
-       $json = $mod->new->ascii(1) and last;
-}
 
 sub list_all_i {
        my ($ibx, $arg) = @_;
@@ -132,106 +119,6 @@ sub html ($$) {
        [ $code, $h, [ $out ] ];
 }
 
-sub fingerprint ($) {
-       my ($git) = @_;
-       # TODO: convert to qspawn for fairness when there's
-       # thousands of repos
-       my ($fh, $pid) = $git->popen('show-ref');
-       my $dig = Digest::SHA->new(1);
-       while (read($fh, my $buf, 65536)) {
-               $dig->add($buf);
-       }
-       close $fh;
-       waitpid($pid, 0);
-       return if $?; # empty, uninitialized git repo
-       $dig->hexdigest;
-}
-
-sub manifest_add ($$;$$) {
-       my ($manifest, $ibx, $epoch, $default_desc) = @_;
-       my $url_path = "/$ibx->{name}";
-       my $git_dir = $ibx->{inboxdir};
-       if (defined $epoch) {
-               $git_dir .= "/git/$epoch.git";
-               $url_path .= "/git/$epoch.git";
-       }
-       return unless -d $git_dir;
-       my $git = PublicInbox::Git->new($git_dir);
-       my $fingerprint = fingerprint($git) or return; # no empty repos
-
-       chomp(my $owner = $git->qx('config', 'gitweb.owner'));
-       chomp(my $desc = try_cat("$git_dir/description"));
-       utf8::decode($owner);
-       utf8::decode($desc);
-       $owner = undef if $owner eq '';
-       $desc = 'Unnamed repository' if $desc eq '';
-
-       # templates/hooks--update.sample and git-multimail in git.git
-       # only match "Unnamed repository", not the full contents of
-       # templates/this--description in git.git
-       if ($desc =~ /\AUnnamed repository/) {
-               $desc = "$default_desc [epoch $epoch]" if defined($epoch);
-       }
-
-       my $reference;
-       chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
-       if ($alt) {
-               # n.b.: GitPython doesn't seem to handle comments or C-quoted
-               # strings like native git does; and we don't for now, either.
-               my @alt = split(/\n+/, $alt);
-
-               # grokmirror only supports 1 alternate for "reference",
-               if (scalar(@alt) == 1) {
-                       my $objdir = "$git_dir/objects";
-                       $reference = File::Spec->rel2abs($alt[0], $objdir);
-                       $reference =~ s!/[^/]+/?\z!!; # basename
-               }
-       }
-       $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
-       my $modified = $git->modified;
-       if ($modified > $manifest->{-mtime}) {
-               $manifest->{-mtime} = $modified;
-       }
-       $manifest->{$url_path} = {
-               owner => $owner,
-               reference => $reference,
-               description => $desc,
-               modified => $modified,
-               fingerprint => $fingerprint,
-       };
-}
-
-# manifest.js.gz
-sub js ($$) {
-       my ($env, $list) = @_;
-       # $json won't be defined if IO::Compress::Gzip is missing
-       $json or return [ 404, [], [] ];
-
-       my $manifest = { -abs2urlpath => {}, -mtime => 0 };
-       for my $ibx (@$list) {
-               if (defined(my $max = $ibx->max_git_epoch)) {
-                       my $desc = $ibx->description;
-                       for my $epoch (0..$max) {
-                               manifest_add($manifest, $ibx, $epoch, $desc);
-                       }
-               } else {
-                       manifest_add($manifest, $ibx);
-               }
-       }
-       my $abs2urlpath = delete $manifest->{-abs2urlpath};
-       my $mtime = delete $manifest->{-mtime};
-       while (my ($url_path, $repo) = each %$manifest) {
-               defined(my $abs = $repo->{reference}) or next;
-               $repo->{reference} = $abs2urlpath->{$abs};
-       }
-       my $out;
-       gzip(\($json->encode($manifest)) => \$out);
-       $manifest = undef;
-       [ 200, [ qw(Content-Type application/gzip),
-                'Last-Modified', time2str($mtime),
-                'Content-Length', bytes::length($out) ], [ $out ] ];
-}
-
 # not really a stand-alone PSGI app, but maybe it could be...
 sub call {
        my ($self, $env) = @_;
@@ -239,7 +126,7 @@ sub call {
        if ($env->{PATH_INFO} eq '/manifest.js.gz') {
                # grokmirror uses relative paths, so it's domain-dependent
                my $list = $self->{manifest_cb}->($self, $env, 'manifest');
-               js($env, $list);
+               PublicInbox::ManifestJsGz::response($env, $list);
        } else { # /
                my $list = $self->{www_cb}->($self, $env, 'www');
                html($env, $list);
index c4511cd1fed11cc46233e24b371b9d5c0f47b7f3..4309a5e17ba24e176409937b21e7475e95a65ed1 100644 (file)
@@ -10,9 +10,10 @@ use PublicInbox::Import;
 require_mods(qw(URI::Escape Plack::Builder Digest::SHA
                IO::Compress::Gzip IO::Uncompress::Gunzip HTTP::Tiny));
 require PublicInbox::WwwListing;
+require PublicInbox::ManifestJsGz;
 my $json = do {
        no warnings 'once';
-       $PublicInbox::WwwListing::json;
+       $PublicInbox::ManifestJsGz::json;
 } or plan skip_all => "JSON module missing";
 
 use_ok 'PublicInbox::Git';
@@ -20,7 +21,7 @@ use_ok 'PublicInbox::Git';
 my ($tmpdir, $for_destroy) = tmpdir();
 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
 PublicInbox::Import::init_bare($bare->{git_dir});
-is(PublicInbox::WwwListing::fingerprint($bare), undef,
+is(PublicInbox::ManifestJsGz::fingerprint($bare), undef,
        'empty repo has no fingerprint');
 {
        my $fi_data = './t/git.fast-import-data';
@@ -30,7 +31,7 @@ is(PublicInbox::WwwListing::fingerprint($bare), undef,
                'fast-import');
 }
 
-like(PublicInbox::WwwListing::fingerprint($bare), qr/\A[a-f0-9]{40}\z/,
+like(PublicInbox::ManifestJsGz::fingerprint($bare), qr/\A[a-f0-9]{40}\z/,
        'got fingerprint with non-empty repo');
 
 sub tiny_test {