Permalink
Browse files

Removing more duplicate code in sockets

  • Loading branch information...
1 parent 7556fcc commit 45bbf4831e74705e2eda01422f3c24967f9db53a @durran durran committed Feb 27, 2014
@@ -165,6 +165,14 @@ def handle_errors
raise Mongo::SocketError, SSL_ERROR
end
+ def initialize!
+ Timeout.timeout(timeout, Mongo::SocketTimeoutError) do
+ @socket = initialize_socket
+ yield if block_given?
+ self
+ end
+ end
+
def initialize_socket
sock = default_socket
sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
@@ -38,9 +38,7 @@ class SSL
#
# @since 3.0.0
def connect!
- Timeout.timeout(timeout, Mongo::SocketTimeoutError) do
- @socket = initialize_socket
-
+ initialize! do
# Apply ssl wrapper and perform handshake.
ssl_socket = OpenSSL::SSL::SSLSocket.new(@socket, context)
ssl_socket.sync_close = true
@@ -52,7 +50,6 @@ def connect!
raise Mongo::SocketError, 'SSL handshake failed due to a hostname mismatch.'
end
end
- self
end
end
@@ -34,10 +34,7 @@ class TCP
#
# @since 3.0.0
def connect!
- Timeout.timeout(timeout, Mongo::SocketTimeoutError) do
- @socket = initialize_socket
- self
- end
+ initialize!
end
# Initializes a new TCP socket.
@@ -31,10 +31,7 @@ class Unix
#
# @since 3.0.0
def connect!
- Timeout.timeout(timeout, Mongo::SocketTimeoutError) do
- @socket = initialize_socket
- self
- end
+ initialize!
end
# Initializes a new Unix socket.

0 comments on commit 45bbf48

Please sign in to comment.