]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ManifestJsGz.pm
www: manifest.js.gz generation no longer hogs event loop
[public-inbox.git] / lib / PublicInbox / ManifestJsGz.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::ManifestJsGz;
4 use strict;
5 use v5.10.1;
6 use Digest::SHA ();
7 use File::Spec ();
8 use bytes (); # length
9 use PublicInbox::Inbox;
10 use PublicInbox::Git;
11 use IO::Compress::Gzip qw(gzip);
12 use HTTP::Date qw(time2str);
13 *try_cat = \&PublicInbox::Inbox::try_cat;
14
15 our $json;
16 for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
17         eval "require $mod" or next;
18         # ->ascii encodes non-ASCII to "\uXXXX"
19         $json = $mod->new->ascii(1) and last;
20 }
21
22 sub response {
23         my ($env, $list) = @_;
24         $json or return [ 404, [], [] ];
25         my $self = bless {
26                 -abs2urlpath => {},
27                 -mtime => 0,
28                 manifest => {},
29                 -list => $list,
30                 psgi_env => $env,
31         }, __PACKAGE__;
32
33         # PSGI server will call this immediately and give us a callback (-wcb)
34         sub {
35                 $self->{-wcb} = $_[0]; # HTTP write callback
36                 iterate_start($self);
37         };
38 }
39
40 sub fingerprint ($) {
41         my ($git) = @_;
42         # TODO: convert to qspawn for fairness when there's
43         # thousands of repos
44         my ($fh, $pid) = $git->popen('show-ref');
45         my $dig = Digest::SHA->new(1);
46         while (read($fh, my $buf, 65536)) {
47                 $dig->add($buf);
48         }
49         close $fh;
50         waitpid($pid, 0);
51         return if $?; # empty, uninitialized git repo
52         $dig->hexdigest;
53 }
54
55 sub manifest_add ($$;$$) {
56         my ($self, $ibx, $epoch, $default_desc) = @_;
57         my $url_path = "/$ibx->{name}";
58         my $git_dir = $ibx->{inboxdir};
59         if (defined $epoch) {
60                 $git_dir .= "/git/$epoch.git";
61                 $url_path .= "/git/$epoch.git";
62         }
63         return unless -d $git_dir;
64         my $git = PublicInbox::Git->new($git_dir);
65         my $fingerprint = fingerprint($git) or return; # no empty repos
66
67         chomp(my $owner = $git->qx('config', 'gitweb.owner'));
68         chomp(my $desc = try_cat("$git_dir/description"));
69         utf8::decode($owner);
70         utf8::decode($desc);
71         $owner = undef if $owner eq '';
72         $desc = 'Unnamed repository' if $desc eq '';
73
74         # templates/hooks--update.sample and git-multimail in git.git
75         # only match "Unnamed repository", not the full contents of
76         # templates/this--description in git.git
77         if ($desc =~ /\AUnnamed repository/) {
78                 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
79         }
80
81         my $reference;
82         chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
83         if ($alt) {
84                 # n.b.: GitPython doesn't seem to handle comments or C-quoted
85                 # strings like native git does; and we don't for now, either.
86                 my @alt = split(/\n+/, $alt);
87
88                 # grokmirror only supports 1 alternate for "reference",
89                 if (scalar(@alt) == 1) {
90                         my $objdir = "$git_dir/objects";
91                         $reference = File::Spec->rel2abs($alt[0], $objdir);
92                         $reference =~ s!/[^/]+/?\z!!; # basename
93                 }
94         }
95         $self->{-abs2urlpath}->{$git_dir} = $url_path;
96         my $modified = $git->modified;
97         if ($modified > $self->{-mtime}) {
98                 $self->{-mtime} = $modified;
99         }
100         $self->{manifest}->{$url_path} = {
101                 owner => $owner,
102                 reference => $reference,
103                 description => $desc,
104                 modified => $modified,
105                 fingerprint => $fingerprint,
106         };
107 }
108
109 sub iterate_start {
110         my ($self) = @_;
111         if (my $async = $self->{psgi_env}->{'pi-httpd.async'}) {
112                 # PublicInbox::HTTPD::Async->new
113                 $async->(undef, undef, $self);
114         } else {
115                 event_step($self) while $self->{-wcb};
116         }
117 }
118
119 sub event_step {
120         my ($self) = @_;
121         while (my $ibx = shift(@{$self->{-list}})) {
122                 eval {
123                         if (defined(my $max = $ibx->max_git_epoch)) {
124                                 my $desc = $ibx->description;
125                                 for my $epoch (0..$max) {
126                                         manifest_add($self, $ibx, $epoch, $desc)
127                                 }
128                         } else {
129                                 manifest_add($self, $ibx);
130                         }
131                 };
132                 warn "E: $@" if $@;
133                 if (my $async = $self->{psgi_env}->{'pi-httpd.async'}) {
134                         # PublicInbox::HTTPD::Async->new
135                         $async->(undef, undef, $self);
136                 }
137                 return; # more steps needed
138         }
139         my $abs2urlpath = delete $self->{-abs2urlpath};
140         my $wcb = delete $self->{-wcb};
141         my $manifest = delete $self->{manifest};
142         while (my ($url_path, $repo) = each %$manifest) {
143                 defined(my $abs = $repo->{reference}) or next;
144                 $repo->{reference} = $abs2urlpath->{$abs};
145         }
146         $manifest = $json->encode($manifest);
147         gzip(\$manifest => \(my $out));
148         $wcb->([ 200, [ qw(Content-Type application/gzip),
149                  'Last-Modified', time2str($self->{-mtime}),
150                  'Content-Length', bytes::length($out) ], [ $out ] ]);
151 }
152
153 1;