1 # Copyright (C) 2019 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;
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
26 -Bufsize => PublicInbox::NNTP::LINE_MAX,
27 -WindowBits => -15, # RFC 1951
31 # global deflate context and buffer
32 my $zbuf = \(my $buf = '');
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
40 -Bufsize => 65536, # same as nnrpd
41 -WindowBits => -15, # RFC 1951
44 $err == Z_OK or die "Failed to initialize zlib deflate stream: $err";
49 my ($class, $self) = @_;
50 my ($in, $err) = Compress::Raw::Zlib::Inflate->new(%IN_OPT);
52 $self->err("Inflate->new failed: $err");
53 $self->res('403 Unable to activate compression');
57 $self->res('206 Compression active');
62 # overrides PublicInbox::NNTP::compressed
65 # $_[1] may be a reference or not
67 my ($self, $rbuf, $len, $off) = @_;
69 my $zin = $self->{zin} or return; # closed
71 my $dbuf = delete($self->{dbuf}) // '';
72 $doff = length($dbuf);
73 my $r = PublicInbox::DS::do_read($self, \$dbuf, $len, $doff) or return;
75 # assert(length($$rbuf) == $off) as far as NNTP.pm is concerned
76 # -ConsumeInput is true, so $dbuf is automatically emptied
77 my $err = $zin->inflate($dbuf, $rbuf);
79 $self->{dbuf} = $dbuf if $dbuf ne '';
80 $r = length($$rbuf) and return $r;
81 # nothing ready, yet, get more, later
90 # override PublicInbox::DS::msg_more
94 # $_[1] may be a reference or not for ->deflate
95 my $err = $zout->deflate($_[1], $zbuf);
96 $err == Z_OK or die "->deflate failed $err";
103 my $deflated = $zbuf;
104 $zbuf = \(my $next = '');
106 my $err = $zout->flush($deflated, Z_FULL_FLUSH);
107 $err == Z_OK or die "->flush failed $err";
109 # We can still let the lower socket layer do buffering:
110 PublicInbox::DS::msg_more($self, $$deflated);
113 # compatible with PublicInbox::DS::write, so $_[1] may be a reference or not
116 return PublicInbox::DS::write($self, $_[1]) if ref($_[1]) eq 'CODE';
118 my $deflated = $zbuf;
119 $zbuf = \(my $next = '');
121 # $_[1] may be a reference or not for ->deflate
122 my $err = $zout->deflate($_[1], $deflated);
123 $err == Z_OK or die "->deflate failed $err";
124 $err = $zout->flush($deflated, Z_FULL_FLUSH);
125 $err == Z_OK or die "->flush failed $err";
127 # We can still let the socket layer do buffering:
128 PublicInbox::DS::write($self, $deflated);