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>
4 # RFC 8054 NNTP COMPRESS DEFLATE implementation
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
12 # [a] - initial implementation using per-client Deflate contexts and buffer
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;
20 use parent qw(PublicInbox::NNTP);
21 use Compress::Raw::Zlib;
24 -Bufsize => PublicInbox::NNTP::LINE_MAX,
25 -WindowBits => -15, # RFC 1951
29 # global deflate context and buffer
30 my $zbuf = \(my $buf = '');
34 ($zout, $err) = Compress::Raw::Zlib::Deflate->new(
35 # nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
36 # the zlib C library and git use MemLevel=8 as the default
38 -Bufsize => 65536, # same as nnrpd
39 -WindowBits => -15, # RFC 1951
42 $err == Z_OK or die "Failed to initialize zlib deflate stream: $err";
47 my ($class, $self) = @_;
48 my ($in, $err) = Compress::Raw::Zlib::Inflate->new(%IN_OPT);
50 $self->err("Inflate->new failed: $err");
51 $self->res('403 Unable to activate compression');
54 $self->res('206 Compression active');
59 # overrides PublicInbox::NNTP::compressed
63 my ($self, $rbuf, $len, $off) = @_;
65 my $zin = $self->{zin} or return; # closed
67 my $dbuf = delete($self->{dbuf}) // '';
68 $doff = length($dbuf);
69 my $r = PublicInbox::DS::do_read($self, \$dbuf, $len, $doff) or return;
71 # Workaround inflate bug appending to OOK scalars:
72 # <https://rt.cpan.org/Ticket/Display.html?id=132734>
73 # We only have $off if the client is pipelining, and pipelining
74 # is where our substr() OOK optimization in event_step makes sense.
81 # assert(length($$rbuf) == $off) as far as NNTP.pm is concerned
82 # -ConsumeInput is true, so $dbuf is automatically emptied
83 my $err = $zin->inflate($dbuf, $rbuf);
85 $self->{dbuf} = $dbuf if $dbuf ne '';
86 $r = length($$rbuf) and return $r;
87 # nothing ready, yet, get more, later
96 # override PublicInbox::DS::msg_more
100 # $_[1] may be a reference or not for ->deflate
101 my $err = $zout->deflate($_[1], $zbuf);
102 $err == Z_OK or die "->deflate failed $err";
109 my $deflated = $zbuf;
110 $zbuf = \(my $next = '');
112 my $err = $zout->flush($deflated, Z_FULL_FLUSH);
113 $err == Z_OK or die "->flush failed $err";
115 # We can still let the lower socket layer do buffering:
116 PublicInbox::DS::msg_more($self, $$deflated);
119 # compatible with PublicInbox::DS::write, so $_[1] may be a reference or not
122 return PublicInbox::DS::write($self, $_[1]) if ref($_[1]) eq 'CODE';
124 my $deflated = $zbuf;
125 $zbuf = \(my $next = '');
127 # $_[1] may be a reference or not for ->deflate
128 my $err = $zout->deflate($_[1], $deflated);
129 $err == Z_OK or die "->deflate failed $err";
130 $err = $zout->flush($deflated, Z_FULL_FLUSH);
131 $err == Z_OK or die "->flush failed $err";
133 # We can still let the socket layer do buffering:
134 PublicInbox::DS::write($self, $deflated);