]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NNTPdeflate.pm
nntp: event_step: prepare for async git reads
[public-inbox.git] / lib / PublicInbox / NNTPdeflate.pm
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # RFC 8054 NNTP COMPRESS DEFLATE implementation
5 #
6 # RSS usage for 10K idle-but-did-something NNTP clients on 64-bit:
7 #   TLS + DEFLATE[a] :  1.8 GB  (MemLevel=9, 1.2 GB with MemLevel=8)
8 #   TLS + DEFLATE[b] :  ~300MB
9 #   TLS only         :  <200MB
10 #   plain            :   <50MB
11 #
12 # [a] - initial implementation using per-client Deflate contexts and buffer
13 #
14 # [b] - memory-optimized implementation using a global deflate context.
15 #       It's less efficient in terms of compression, but way more
16 #       efficient in terms of server memory usage.
17 package PublicInbox::NNTPdeflate;
18 use strict;
19 use warnings;
20 use 5.010_001;
21 use base qw(PublicInbox::NNTP);
22 use Compress::Raw::Zlib;
23 use Hash::Util qw(unlock_hash); # dependency of fields for perl 5.10+, anyways
24
25 my %IN_OPT = (
26         -Bufsize => PublicInbox::NNTP::LINE_MAX,
27         -WindowBits => -15, # RFC 1951
28         -AppendOutput => 1,
29 );
30
31 # global deflate context and buffer
32 my $zbuf = \(my $buf = '');
33 my $zout;
34 {
35         my $err;
36         ($zout, $err) = Compress::Raw::Zlib::Deflate->new(
37                 # nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
38                 # the zlib C library and git use MemLevel=8 as the default
39                 # -MemLevel => 9,
40                 -Bufsize => 65536, # same as nnrpd
41                 -WindowBits => -15, # RFC 1951
42                 -AppendOutput => 1,
43         );
44         $err == Z_OK or die "Failed to initialize zlib deflate stream: $err";
45 }
46
47
48 sub enable {
49         my ($class, $self) = @_;
50         my ($in, $err) = Compress::Raw::Zlib::Inflate->new(%IN_OPT);
51         if ($err != Z_OK) {
52                 $self->err("Inflate->new failed: $err");
53                 $self->res('403 Unable to activate compression');
54                 return;
55         }
56         unlock_hash(%$self);
57         $self->res('206 Compression active');
58         bless $self, $class;
59         $self->{zin} = $in;
60 }
61
62 # overrides PublicInbox::NNTP::compressed
63 sub compressed { 1 }
64
65 sub do_read ($$$$) {
66         my ($self, $rbuf, $len, $off) = @_;
67
68         my $zin = $self->{zin} or return; # closed
69         my $doff;
70         my $dbuf = delete($self->{dbuf}) // '';
71         $doff = length($dbuf);
72         my $r = PublicInbox::DS::do_read($self, \$dbuf, $len, $doff) or return;
73
74         # Workaround inflate bug appending to OOK scalars:
75         # <https://rt.cpan.org/Ticket/Display.html?id=132734>
76         # We only have $off if the client is pipelining, and pipelining
77         # is where our substr() OOK optimization in event_step makes sense.
78         if ($off) {
79                 my $copy = $$rbuf;
80                 undef $$rbuf;
81                 $$rbuf = $copy;
82         }
83
84         # assert(length($$rbuf) == $off) as far as NNTP.pm is concerned
85         # -ConsumeInput is true, so $dbuf is automatically emptied
86         my $err = $zin->inflate($dbuf, $rbuf);
87         if ($err == Z_OK) {
88                 $self->{dbuf} = $dbuf if $dbuf ne '';
89                 $r = length($$rbuf) and return $r;
90                 # nothing ready, yet, get more, later
91                 $self->requeue;
92         } else {
93                 delete $self->{zin};
94                 $self->close;
95         }
96         0;
97 }
98
99 # override PublicInbox::DS::msg_more
100 sub msg_more ($$) {
101         my $self = $_[0];
102
103         # $_[1] may be a reference or not for ->deflate
104         my $err = $zout->deflate($_[1], $zbuf);
105         $err == Z_OK or die "->deflate failed $err";
106         1;
107 }
108
109 sub zflush ($) {
110         my ($self) = @_;
111
112         my $deflated = $zbuf;
113         $zbuf = \(my $next = '');
114
115         my $err = $zout->flush($deflated, Z_FULL_FLUSH);
116         $err == Z_OK or die "->flush failed $err";
117
118         # We can still let the lower socket layer do buffering:
119         PublicInbox::DS::msg_more($self, $$deflated);
120 }
121
122 # compatible with PublicInbox::DS::write, so $_[1] may be a reference or not
123 sub write ($$) {
124         my $self = $_[0];
125         return PublicInbox::DS::write($self, $_[1]) if ref($_[1]) eq 'CODE';
126
127         my $deflated = $zbuf;
128         $zbuf = \(my $next = '');
129
130         # $_[1] may be a reference or not for ->deflate
131         my $err = $zout->deflate($_[1], $deflated);
132         $err == Z_OK or die "->deflate failed $err";
133         $err = $zout->flush($deflated, Z_FULL_FLUSH);
134         $err == Z_OK or die "->flush failed $err";
135
136         # We can still let the socket layer do buffering:
137         PublicInbox::DS::write($self, $deflated);
138 }
139
140 1;