Skip to content
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

RUBY-3097 add/use monotonic time for deadlines #5491

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
require 'concurrent-ruby'

require "mongo"
require 'mongo/active_support'
require "mongo/active_support"

require "mongoid/version"
require "mongoid/deprecable"
Expand All @@ -27,6 +27,7 @@
require "mongoid/tasks/database"
require "mongoid/query_cache"
require "mongoid/warnings"
require "mongoid/utils"

# If we are using Rails then we will include the Mongoid railtie. This has all
# the nifty initializers that Mongoid needs.
Expand Down
22 changes: 22 additions & 0 deletions lib/mongoid/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Mongoid

# @api private
module Utils

# This function should be used if you need to measure time.
# @example Calculate elapsed time.
# starting = Utils.monotonic_time
# # do something time consuming
# ending = Utils.monotonic_time
# puts "It took #{(ending - starting).to_i} seconds"
#
# @see https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/
#
# @return [Float] seconds according to monotonic clock
module_function def monotonic_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end
end
2 changes: 1 addition & 1 deletion lib/mongoid/warnings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def warning(id, message)

warning :geo_haystack_deprecated, 'The geoHaystack type is deprecated.'
warning :as_json_compact_deprecated, '#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.'
warning :symbol_type_deprecated, 'The BSON Symbol type is deprecated by MongoDB. Please use String or StringifiedSymbol field types instead of the Symbol field type'
warning :symbol_type_deprecated, 'The BSON Symbol type is deprecated by MongoDB. Please use String or StringifiedSymbol field types instead of the Symbol field type.'
warning :legacy_readonly, 'The readonly! method will only mark the document readonly when the legacy_readonly feature flag is switched off.'
end
end
4 changes: 2 additions & 2 deletions spec/integration/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def clean_env
end

def wait_for_port(port, timeout, process)
deadline = Time.now + timeout
deadline = Mongoid::Utils.monotonic_time + timeout
loop do
begin
Socket.tcp('localhost', port, nil, nil, connect_timeout: 0.5) do |socket|
Expand All @@ -336,7 +336,7 @@ def wait_for_port(port, timeout, process)
unless process.alive?
raise "Process #{process} died while waiting for port #{port}"
end
if Time.now > deadline
if Mongoid::Utils.monotonic_time > deadline
raise
end
end
Expand Down