Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add graceful handling of rs.stepDown on queries.

If a query was getting executed on a node that had already been
authenticated, but the node was reconfigured, restarted, or underwent a
stepDown - we now attempt to login once again if an authentication
failure occurs.

[ related #60, #72 ]
  • Loading branch information...
commit c931329ccbe24eb051d2fd56cdbf983a0af1adde 1 parent 23dafd7
Durran Jordan durran authored
Showing with 24 additions and 3 deletions.
  1. +14 −3 lib/moped/node.rb
  2. +10 −0 lib/moped/protocol/reply.rb
17 lib/moped/node.rb
View
@@ -343,9 +343,20 @@ def passive?
def query(database, collection, selector, options = {})
operation = Protocol::Query.new(database, collection, selector, options)
- process operation do |reply|
- if reply.flags.include?(:query_failure)
- raise Errors::QueryFailure.new(operation, reply.documents.first)
+ process(operation) do |reply|
+ if reply.query_failed?
+ if reply.unauthorized? && auth.has_key?[database]
+ # If we got here, most likely this is the case of Moped
+ # authenticating successfully against the node originally, but the
+ # node has been reset or gone down and come back up. The most
+ # common case here is a rs.stepDown() which will reinitialize the
+ # connection. In this case we need to requthenticate and try again,
+ # otherwise we'll just raise the error to the user.
+ login(database, *auth[database])
+ query(database, collection, selector, options)
+ else
+ raise Errors::QueryFailure.new(operation, reply.documents.first)
+ end
end
reply
end
10 lib/moped/protocol/reply.rb
View
@@ -13,6 +13,8 @@ module Protocol
class Reply
include Message
+ UNAUTHORIZED = 10057
+
# @attribute
# @return [Number] the length of the message
int32 :length
@@ -53,6 +55,14 @@ class Reply
finalize
+ def query_failed?
+ flags.include?(:query_failure)
+ end
+
+ def unauthorized?
+ documents.first["code"] == UNAUTHORIZED
+ end
+
class << self
# Consumes a buffer, returning the deserialized Reply message.
Please sign in to comment.
Something went wrong with that request. Please try again.