summaryrefslogtreecommitdiff
path: root/ext/openssl/ossl_ssl.c
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-12 01:41:51 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-12 01:41:51 +0000
commitc18df6d87cb1a26f596218beb8099c709e30aaf4 (patch)
tree080c4c27634bd3a33258b05e2291e5390e3d287d /ext/openssl/ossl_ssl.c
parent525d2102fcc625435598729e8c972c147eefd24f (diff)
connect_nonblock supports "exception: false"
This is for consistency with accept_nonblock arguments and gives a minor speedup from avoiding exceptions. [ruby-core:68838] [Feature #11024] * ext/openssl/ossl_ssl.c (ossl_ssl_connect_nonblock): support `exception: false' * (get_no_exception): move function location * ext/socket/socket.c (sock_connect_nonblock): support `exception: false' * test/openssl/test_pair.rb (test_connect_accept_nonblock_no_exception): test `exception: false' on connect, rename from `test_accept_nonblock_no_exception' * test/socket/test_nonblock.rb (test_connect_nonblock_no_exception): new test Benchmark results: default 0.050000 0.100000 0.150000 ( 0.151307) exception: false 0.030000 0.080000 0.110000 ( 0.108840) ----------------------------8<----------------------- require 'socket' require 'benchmark' require 'io/wait' require 'tmpdir' host = '127.0.0.1' serv = TCPServer.new(host, 0) # UNIX sockets may not hit EINPROGRESS nr = 5000 # few iterations to avoid running out of ports addr = serv.getsockname pid = fork do begin serv.accept.close rescue => e warn "#$$: #{e.message} (#{e.class})" end while true end at_exit { Process.kill(:TERM, pid) } serv.close Benchmark.bmbm do |x| x.report("default") do nr.times do s = Socket.new(:INET, :STREAM) s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1) begin s.connect_nonblock(addr) rescue IO::WaitWritable s.wait_writable end s.close end end x.report("exception: false") do nr.times do s = Socket.new(:INET, :STREAM) s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1) case s.connect_nonblock(addr, exception: false) when :wait_writable s.wait_writable end s.close end end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50254 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/openssl/ossl_ssl.c')
-rw-r--r--ext/openssl/ossl_ssl.c34
1 files changed, 22 insertions, 12 deletions
diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c
index 1a67d35d11..2e128356aa 100644
--- a/ext/openssl/ossl_ssl.c
+++ b/ext/openssl/ossl_ssl.c
@@ -1330,9 +1330,17 @@ ossl_ssl_connect(VALUE self)
return ossl_start_ssl(self, SSL_connect, "SSL_connect", 0, 0);
}
+static int
+get_no_exception(VALUE opts)
+{
+ if (!NIL_P(opts) && Qfalse == rb_hash_lookup2(opts, sym_exception, Qundef))
+ return 1;
+ return 0;
+}
+
/*
* call-seq:
- * ssl.connect_nonblock => self
+ * ssl.connect_nonblock([options]) => self
*
* Initiates the SSL/TLS handshake as a client in non-blocking manner.
*
@@ -1347,12 +1355,22 @@ ossl_ssl_connect(VALUE self)
* retry
* end
*
+ * By specifying `exception: false`, the options hash allows you to indicate
+ * that connect_nonblock should not raise an IO::WaitReadable or
+ * IO::WaitWritable exception, but return the symbol :wait_readable or
+ * :wait_writable instead.
*/
static VALUE
-ossl_ssl_connect_nonblock(VALUE self)
+ossl_ssl_connect_nonblock(int argc, VALUE *argv, VALUE self)
{
+ int no_exception;
+ VALUE opts = Qnil;
+
+ rb_scan_args(argc, argv, "0:", &opts);
+ no_exception = get_no_exception(opts);
+
ossl_ssl_setup(self);
- return ossl_start_ssl(self, SSL_connect, "SSL_connect", 1, 0);
+ return ossl_start_ssl(self, SSL_connect, "SSL_connect", 1, no_exception);
}
/*
@@ -1369,14 +1387,6 @@ ossl_ssl_accept(VALUE self)
return ossl_start_ssl(self, SSL_accept, "SSL_accept", 0, 0);
}
-static int
-get_no_exception(VALUE opts)
-{
- if (!NIL_P(opts) && Qfalse == rb_hash_lookup2(opts, sym_exception, Qundef))
- return 1;
- return 0;
-}
-
/*
* call-seq:
* ssl.accept_nonblock([options]) => self
@@ -2235,7 +2245,7 @@ Init_ossl_ssl(void)
rb_define_alias(cSSLSocket, "to_io", "io");
rb_define_method(cSSLSocket, "initialize", ossl_ssl_initialize, -1);
rb_define_method(cSSLSocket, "connect", ossl_ssl_connect, 0);
- rb_define_method(cSSLSocket, "connect_nonblock", ossl_ssl_connect_nonblock, 0);
+ rb_define_method(cSSLSocket, "connect_nonblock", ossl_ssl_connect_nonblock, -1);
rb_define_method(cSSLSocket, "accept", ossl_ssl_accept, 0);
rb_define_method(cSSLSocket, "accept_nonblock", ossl_ssl_accept_nonblock, -1);
rb_define_method(cSSLSocket, "sysread", ossl_ssl_read, -1);