user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 5/4] nntp: improve error reporting for COMPRESS
Date: Sun, 7 Jul 2019 07:08:31 +0000	[thread overview]
Message-ID: <20190707070831.fafviqilsywuc5bw@dcvr> (raw)
In-Reply-To: <20190705225339.5698-1-e@80x24.org>

Add some checks for errors at initialization, though there's not
much that can be done with ENOMEM-type errors aside from
dropping clients.

We can also get rid of the scary FIXME for MemLevel=8.  It was a
stupid error on my part in the original per-client deflate
stream implementation calling C::R::Z::{Inflate,Deflate} in
array context and getting the extra dualvar error code as a
string result, causing the {zin}/{zout} array refs to have
extra array elements.
---
 lib/PublicInbox/NNTP.pm        |  3 +--
 lib/PublicInbox/NNTPdeflate.pm | 33 +++++++++++++++++++++------------
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 895858b7..6fee29f4 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -922,9 +922,8 @@ sub cmd_starttls ($) {
 # RFC 8054
 sub cmd_compress ($$) {
 	my ($self, $alg) = @_;
-	return '503 Only the DEFLATE is supported' if uc($alg) ne 'DEFLATE';
+	return '503 Only DEFLATE is supported' if uc($alg) ne 'DEFLATE';
 	return r502 if $self->compressed || !$have_deflate;
-	res($self, '206 Compression active');
 	PublicInbox::NNTPdeflate->enable($self);
 	$self->requeue;
 	undef
diff --git a/lib/PublicInbox/NNTPdeflate.pm b/lib/PublicInbox/NNTPdeflate.pm
index 78da2a58..10e2337c 100644
--- a/lib/PublicInbox/NNTPdeflate.pm
+++ b/lib/PublicInbox/NNTPdeflate.pm
@@ -30,24 +30,33 @@ my %IN_OPT = (
 
 # global deflate context and buffer
 my $zbuf = \(my $buf = '');
-my $zout = Compress::Raw::Zlib::Deflate->new(
-	# nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
-	# but the zlib C library and git use MemLevel=8 as the default.
-	# FIXME: sometimes clients fail with 8, so we use 9
-	# -MemLevel => 9,
-
-	# needs more testing, nothing obviously different in terms of memory
-	-Bufsize => 65536,
+my $zout;
+{
+	my $err;
+	($zout, $err) = Compress::Raw::Zlib::Deflate->new(
+		# nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
+		# the zlib C library and git use MemLevel=8 as the default
+		# -MemLevel => 9,
+		-Bufsize => 65536, # same as nnrpd
+		-WindowBits => -15, # RFC 1951
+		-AppendOutput => 1,
+	);
+	$err == Z_OK or die "Failed to initialize zlib deflate stream: $err";
+}
 
-	-WindowBits => -15, # RFC 1951
-	-AppendOutput => 1,
-);
 
 sub enable {
 	my ($class, $self) = @_;
+	my ($in, $err) = Compress::Raw::Zlib::Inflate->new(%IN_OPT);
+	if ($err != Z_OK) {
+		$self->err("Inflate->new failed: $err");
+		$self->res('403 Unable to activate compression');
+		return;
+	}
 	unlock_hash(%$self);
+	$self->res('206 Compression active');
 	bless $self, $class;
-	$self->{zin} = [ Compress::Raw::Zlib::Inflate->new(%IN_OPT), '' ];
+	$self->{zin} = [ $in, '' ];
 }
 
 # overrides PublicInbox::NNTP::compressed
-- 
EW


      parent reply	other threads:[~2019-07-07  7:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-05 22:53 [PATCH 0/4] implement NNTP COMPRESS per RFC 8054 Eric Wong
2019-07-05 22:53 ` [PATCH 1/4] nntp: use msg_more as a method Eric Wong
2019-07-05 22:53 ` [PATCH 2/4] nntp: move LINE_MAX constant to the top Eric Wong
2019-07-05 22:53 ` [PATCH 3/4] nntp: support COMPRESS DEFLATE per RFC 8054 Eric Wong
2019-07-05 22:53 ` [RFC 4/4] nntp: reduce memory overhead of zlib Eric Wong
2019-07-07  7:08 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190707070831.fafviqilsywuc5bw@dcvr \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).