Skip to content

Commit

Permalink
Use monotonic clock
Browse files Browse the repository at this point in the history
* Uses Process.clock_gettime(Process::CLOCK_MONOTONIC), which was added in Ruby 2.1, instead of Time.now.
  This prevents issues with scheduled timers when running alongside Timecop as reported in
  iconara#52
* Bumps minimum Ruby version to 2.1
* Updates Travis test ruby versions
  • Loading branch information
brendar committed Aug 31, 2021
1 parent 611ee8d commit e424079
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ bundler_args: --without development
services:
- redis-server
rvm:
- 1.9.3
- 2.0.0
- 2.1.1
- 2.1.10
- 2.2.10
- 2.3.8
- 2.4.10
- 2.5.9
- 2.6.8
- 2.7.4
- jruby
- jruby-head
- rbx-2
Expand Down
2 changes: 1 addition & 1 deletion ione.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ Gem::Specification.new do |s|
s.require_paths = %w(lib)

s.platform = Gem::Platform::RUBY
s.required_ruby_version = '>= 1.9.3'
s.required_ruby_version = '>= 2.1.0'
end
13 changes: 10 additions & 3 deletions lib/ione/io/io_reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class IoReactor
# @param options [Hash] only used to inject behaviour during tests
def initialize(options={})
@options = options
@clock = options[:clock] || Time
@clock = options[:clock] || MonotonicClock
@state = PENDING_STATE
@error_listeners = []
@unblocker = Unblocker.new
Expand Down Expand Up @@ -464,7 +464,7 @@ def to_s
class IoLoopBody
def initialize(unblocker, options={})
@selector = options[:selector] || IO
@clock = options[:clock] || Time
@clock = options[:clock] || MonotonicClock
@timeout = options[:tick_resolution] || 1
@drain_timeout = options[:drain_timeout] || 5
@lock = Mutex.new
Expand Down Expand Up @@ -540,7 +540,7 @@ def to_s
# @private
class Scheduler
def initialize(options={})
@clock = options[:clock] || Time
@clock = options[:clock] || MonotonicClock
@lock = Mutex.new
@timer_queue = Heap.new
@pending_timers = {}
Expand Down Expand Up @@ -615,5 +615,12 @@ def to_s
%(#<#{self.class.name} @timers=[#{@pending_timers.values.map(&:to_s).join(', ')}]>)
end
end

# @private
module MonotonicClock
def self.now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end
end
end

0 comments on commit e424079

Please sign in to comment.