Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Ensure mongodb credentials are always sent upon (re)connection
Browse files Browse the repository at this point in the history
If existing socket is reset, perhaps due to mongod failure,
credentials hash isn't reset. This causes them to not be sent upon
reconnection. Changed to reset at connect() instead of relying on a
disconnect() that may or may not happen.

Also cache last set of credentials so they can be reused when connection
loss is detected by with_connection().
  • Loading branch information
zarqman committed Oct 4, 2014
1 parent a7f9ec0 commit 695971d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/moped/authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def apply_credentials(logins)
login(database, username, password)
end
end
@original_credentials = credentials.dup
end
self
end
Expand Down
7 changes: 5 additions & 2 deletions lib/moped/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def alive?
#
# @since 1.0.0
def connect
credentials.clear
@sock = if !!options[:ssl]
Socket::SSL.connect(host, port, timeout)
else
Expand Down Expand Up @@ -74,7 +75,6 @@ def connected?
#
# @since 1.0.0
def disconnect
credentials.clear
@sock.close
rescue
ensure
Expand Down Expand Up @@ -217,7 +217,10 @@ def read_data(socket, length)
#
# @since 1.3.0
def with_connection
connect if @sock.nil? || !@sock.alive?
if @sock.nil? || !@sock.alive?
connect
apply_credentials(@original_credentials || {})
end
yield @sock
end
end
Expand Down

0 comments on commit 695971d

Please sign in to comment.