From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.9 required=3.0 tests=ALL_TRUSTED,BAYES_00, T_RP_MATCHES_RCVD shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: yahns-public@yhbt.net Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 0974120442 for ; Sun, 13 Dec 2015 02:17:12 +0000 (UTC) From: Eric Wong To: yahns-public@yhbt.net Subject: [PATCH] openssl_client: use `exception: false' for accept_nonblock Date: Sun, 13 Dec 2015 02:17:12 +0000 Message-Id: <20151213021712.17776-1-e@80x24.org> List-Id: Ruby 2.3 will support this feature to reduce allocations for common errors. --- lib/yahns/openssl_client.rb | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/yahns/openssl_client.rb b/lib/yahns/openssl_client.rb index ffa4b3e..246b407 100644 --- a/lib/yahns/openssl_client.rb +++ b/lib/yahns/openssl_client.rb @@ -53,14 +53,9 @@ module Yahns::OpenSSLClient # :nodoc: if @need_accept # most protocols require read before write, so we start the negotiation # process here: - begin - @ssl.accept_nonblock - rescue IO::WaitReadable - return :wait_readable - rescue IO::WaitWritable - return :wait_writable - rescue OpenSSL::SSL::SSLError - return nil + case rv = accept_nonblock(@ssl) + when :wait_readable, :wait_writable, nil + return rv end @need_accept = false end @@ -71,4 +66,20 @@ module Yahns::OpenSSLClient # :nodoc: @ssl.close # flushes SSLSocket super # IO#close end + + if RUBY_VERSION.to_f >= 2.3 + def accept_nonblock(ssl) + ssl.accept_nonblock(exception: false) + end + else + def accept_nonblock(ssl) + ssl.accept_nonblock + rescue IO::WaitReadable + :wait_readable + rescue IO::WaitWritable + :wait_writable + rescue OpenSSL::SSL::SSLError + nil + end + end end -- EW