]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NNTPdeflate.pm
66210bfa84c040c1a2caa90f8d4c01e59d2e5c67
[public-inbox.git] / lib / PublicInbox / NNTPdeflate.pm
1 # Copyright (C) 2019 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 # Warning, enabling compression for C10K NNTP clients is rather
6 # expensive in terms of memory use.
7 #
8 # RSS usage for 10K idle-but-did-something NNTP clients on 64-bit:
9 #   TLS + DEFLATE :  1.8 GB  (MemLevel=9, 1.2 GB with MemLevel=8)
10 #   TLS only      :  <200MB
11 #   plain         :   <50MB
12 package PublicInbox::NNTPdeflate;
13 use strict;
14 use warnings;
15 use 5.010_001;
16 use base qw(PublicInbox::NNTP);
17 use Compress::Raw::Zlib;
18 use Hash::Util qw(unlock_hash); # dependency of fields for perl 5.10+, anyways
19
20 my %IN_OPT = (
21         -Bufsize => PublicInbox::NNTP::LINE_MAX,
22         -WindowBits => -15, # RFC 1951
23         -AppendOutput => 1,
24 );
25
26 my %OUT_OPT = (
27         # nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
28         # but the zlib C library and git use MemLevel=8
29         # as the default.  Using 8 drops our memory use with 10K
30         # TLS clients from 1.8 GB to 1.2 GB, but...
31         # FIXME: sometimes clients fail with 8, so we use 9
32         # -MemLevel => 9,
33
34         # needs more testing, nothing obviously different in terms of memory
35         -Bufsize => 65536,
36
37         -WindowBits => -15, # RFC 1951
38         -AppendOutput => 1,
39 );
40
41 sub enable {
42         my ($class, $self) = @_;
43         unlock_hash(%$self);
44         bless $self, $class;
45         $self->{zin} = [ Compress::Raw::Zlib::Inflate->new(%IN_OPT), '' ];
46         $self->{zout} = [ Compress::Raw::Zlib::Deflate->new(%OUT_OPT), '' ];
47 }
48
49 # overrides PublicInbox::NNTP::compressed
50 sub compressed { 1 }
51
52 # SUPER is PublicInbox::DS::do_read, so $_[1] may be a reference or not
53 sub do_read ($$$$) {
54         my ($self, $rbuf, $len, $off) = @_;
55
56         my $zin = $self->{zin} or return; # closed
57         my $deflated = \($zin->[1]);
58         my $r = $self->SUPER::do_read($deflated, $len) or return;
59
60         # assert(length($$rbuf) == $off) as far as NNTP.pm is concerned
61         # -ConsumeInput is true, so $deflated is automatically emptied
62         my $err = $zin->[0]->inflate($deflated, $rbuf);
63         if ($err == Z_OK) {
64                 $r = length($$rbuf) and return $r;
65                 # nothing ready, yet, get more, later
66                 $self->requeue;
67         } else {
68                 delete $self->{zin};
69                 $self->close;
70         }
71         0;
72 }
73
74 # override PublicInbox::DS::msg_more
75 sub msg_more ($$) {
76         my $self = $_[0];
77         my $zout = $self->{zout};
78
79         # $_[1] may be a reference or not for ->deflate
80         my $err = $zout->[0]->deflate($_[1], $zout->[1]);
81         $err == Z_OK or die "->deflate failed $err";
82         1;
83 }
84
85 # SUPER is PublicInbox::DS::write, so $_[1] may be a reference or not
86 sub write ($$) {
87         my $self = $_[0];
88         return $self->SUPER::write($_[1]) if ref($_[1]) eq 'CODE';
89         my $zout = $self->{zout};
90         my $deflated = pop @$zout;
91
92         # $_[1] may be a reference or not for ->deflate
93         my $err = $zout->[0]->deflate($_[1], $deflated);
94         $err == Z_OK or die "->deflate failed $err";
95         $err = $zout->[0]->flush($deflated, Z_PARTIAL_FLUSH);
96         $err == Z_OK or die "->flush failed $err";
97
98         # PublicInbox::DS::write puts partial writes into another buffer,
99         # so we can prepare the next deflate buffer:
100         $zout->[1] = '';
101         $self->SUPER::write(\$deflated);
102 }
103
104 1;