Skip to content

Commit

Permalink
RUBY-1567 Implement lastUpdateTime (#1137)
Browse files Browse the repository at this point in the history
  • Loading branch information
p-mongo committed Nov 15, 2018
1 parent f2af65e commit 80f369b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/mongo/cluster/sdam_flow.rb
Expand Up @@ -55,10 +55,15 @@ def initialize(cluster, previous_desc, updated_desc)
# updated_desc's address.
def update_server_descriptions
servers_list.each do |server|
if server.address == updated_desc.address && server.description != updated_desc
if server.address == updated_desc.address
changed = server.description != updated_desc
# Always update server description, so that fields like
# last_update_time reflect the last ismaster response
server.update_description(updated_desc)
# There should only be one match
return true
# But return if there was a content difference between
# descriptions, and if there wasn't we'll skip the remainder of
# sdam flow
return changed
end
end
false
Expand Down
8 changes: 8 additions & 0 deletions lib/mongo/server/description.rb
Expand Up @@ -199,6 +199,7 @@ def initialize(address, config = {}, average_round_trip_time = 0)
@features = Features.new(wire_versions, me || @address.to_s)
end
@average_round_trip_time = average_round_trip_time
@last_update_time = Time.now.freeze
end

# @return [ Address ] address The server's address.
Expand Down Expand Up @@ -663,6 +664,13 @@ def op_time
end
end

# Time when the server was last checked.
#
# @return [ Time ] Last check time.
#
# @since 2.7.0
attr_reader :last_update_time

# Check equality of two descriptions.
#
# @example Check description equality.
Expand Down
17 changes: 17 additions & 0 deletions spec/integration/server_description_spec.rb
Expand Up @@ -28,4 +28,21 @@
expect(desc.last_write_date).to be_a(Time)
end
end

describe '#last_update_time' do
before do
ClientRegistry.instance.close_all_clients
end

let(:client) { ClientRegistry.instance.global_client('authorized') }
let(:desc) { client.cluster.servers.first.description }

it 'is set' do
client.database.command(ismaster: 1)

expect(desc.last_update_time).to be_a(Time)
# checked in the last 3 seconds
expect(Time.now - desc.last_update_time < 3).to be true
end
end
end

0 comments on commit 80f369b

Please sign in to comment.