Skip to content

Commit 2b81909

Browse files
committed
Ruby thread pools now use monotonic clock.
1 parent 6ca4c14 commit 2b81909

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

lib/concurrent/executor/ruby_thread_pool_executor.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
require 'thread'
22

3-
require_relative 'executor'
43
require 'concurrent/atomic/event'
4+
require 'concurrent/executor/executor'
55
require 'concurrent/executor/ruby_thread_pool_worker'
6+
require 'concurrent/utility/clock_time'
67

78
module Concurrent
89

@@ -91,7 +92,7 @@ def initialize(opts = {})
9192
@largest_length = 0
9293

9394
@gc_interval = opts.fetch(:gc_interval, 1).to_i # undocumented
94-
@last_gc_time = Time.now.to_f - [1.0, (@gc_interval * 2.0)].max
95+
@last_gc_time = Concurrent::clock_time - [1.0, (@gc_interval * 2.0)].max
9596
end
9697

9798
# @!macro executor_module_method_can_overflow_question
@@ -225,13 +226,13 @@ def ensure_capacity?
225226
#
226227
# @!visibility private
227228
def prune_pool
228-
if Time.now.to_f - @gc_interval >= @last_gc_time
229+
if Concurrent::clock_time - @gc_interval >= @last_gc_time
229230
@pool.delete_if { |worker| worker.dead? }
230231
# send :stop for each thread over idletime
231232
@pool.
232-
select { |worker| @idletime != 0 && Time.now.to_f - @idletime > worker.last_activity }.
233+
select { |worker| @idletime != 0 && Concurrent::clock_time - @idletime > worker.last_activity }.
233234
each { @queue << :stop }
234-
@last_gc_time = Time.now.to_f
235+
@last_gc_time = Concurrent::clock_time
235236
end
236237
end
237238

lib/concurrent/executor/ruby_thread_pool_worker.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'thread'
22
require 'concurrent/logging'
3+
require 'concurrent/utility/clock_time'
34

45
module Concurrent
56

@@ -12,7 +13,7 @@ def initialize(queue, parent)
1213
@queue = queue
1314
@parent = parent
1415
@mutex = Mutex.new
15-
@last_activity = Time.now.to_f
16+
@last_activity = Concurrent::clock_time
1617
@thread = nil
1718
end
1819

@@ -64,7 +65,7 @@ def run(thread = Thread.current)
6465
# let it fail
6566
log DEBUG, ex
6667
ensure
67-
@last_activity = Time.now.to_f
68+
@last_activity = Concurrent::clock_time
6869
@parent.on_end_task
6970
end
7071
end

0 commit comments

Comments
 (0)