-
Notifications
You must be signed in to change notification settings - Fork 253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DRY up connection handling logic #224
Changes from 3 commits
d5f7516
edee2ee
e1a0d13
5a4ddb1
ab320af
e829069
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,56 +8,47 @@ class Net::LDAP::Connection #:nodoc: | |
|
||
def initialize(server) | ||
@instrumentation_service = server[:instrumentation_service] | ||
server[:hosts] = [[server[:host], server[:port]]] if server[:hosts].nil? | ||
|
||
if server[:socket] | ||
prepare_socket(server) | ||
else | ||
server[:hosts] = [[server[:host], server[:port]]] if server[:hosts].nil? | ||
open_connection(server) | ||
end | ||
|
||
yield self if block_given? | ||
end | ||
|
||
def prepare_socket(server) | ||
@conn = server[:socket] | ||
def prepare_socket(server, close = false) | ||
socket = server[:socket] | ||
encryption = server[:encryption] | ||
|
||
if server[:encryption] | ||
setup_encryption server[:encryption] | ||
end | ||
@conn = socket | ||
setup_encryption encryption if encryption | ||
rescue | ||
# Ensure the connection is closed when requested in the event of an SSL | ||
# setup failure. | ||
@conn.close if close | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like new behavior. Why this change in this PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sockets created by the library in This can also be handled within the |
||
@conn = nil | ||
raise | ||
end | ||
|
||
def open_connection(server) | ||
hosts = server[:hosts] | ||
encryption = server[:encryption] | ||
|
||
errors = [] | ||
server[:hosts].each do |host, port| | ||
hosts.each do |host, port| | ||
begin | ||
return connect_to_host(host, port, server) | ||
rescue Net::LDAP::Error | ||
errors << $! | ||
prepare_socket(server.merge(socket: TCPSocket.new(host, port)), true) | ||
return | ||
rescue Net::LDAP::Error, SocketError, SystemCallError, | ||
OpenSSL::SSL::SSLError | ||
errors << [$!, host, port] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assign the error to a variable rather than using the implicit here: rescue Net::LDAP::Error, SocketError, SystemCallError, OpenSSL::SSL::SSLError => e
errors << [e, host, port]
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
end | ||
end | ||
|
||
raise errors.first if errors.size == 1 | ||
raise Net::LDAP::Error, | ||
"Unable to connect to any given server: \n #{errors.join("\n ")}" | ||
end | ||
|
||
def connect_to_host(host, port, server) | ||
begin | ||
@conn = TCPSocket.new(host, port) | ||
rescue SocketError | ||
raise Net::LDAP::Error, "No such address or other socket error." | ||
rescue Errno::ECONNREFUSED | ||
raise Net::LDAP::ConnectionRefusedError, "Server #{host} refused connection on port #{port}." | ||
rescue Errno::EHOSTUNREACH => error | ||
raise Net::LDAP::Error, "Host #{host} was unreachable (#{error.message})" | ||
rescue Errno::ETIMEDOUT | ||
raise Net::LDAP::Error, "Connection to #{host} timed out." | ||
end | ||
|
||
if server[:encryption] | ||
setup_encryption server[:encryption] | ||
end | ||
raise Net::LDAP::ConnectionError.new(errors) | ||
end | ||
|
||
module GetbyteForSSLSocket | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This works even when
server[:host]
and/orserver[:port]
arenil
. It raises aSocketError
and matches up with the old behavior.