-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better pruning for RubyThreadPoolExecutor
- Loading branch information
Showing
8 changed files
with
139 additions
and
107 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
lib/concurrent-ruby/concurrent/collection/ruby_timeout_queue.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Concurrent | ||
module Collection | ||
# @!visibility private | ||
# @!macro ruby_timeout_queue | ||
class RubyTimeoutQueue < ::Queue | ||
def initialize(*args) | ||
if RUBY_VERSION >= '3.2' | ||
raise 'RubyTimeoutQueue is not needed on Ruby 3.2 or later, use ::Queue instead' | ||
end | ||
|
||
super(*args) | ||
|
||
@mutex = Mutex.new | ||
@cond_var = ConditionVariable.new | ||
end | ||
|
||
def push(obj) | ||
super(obj).tap { @mutex.synchronize { @cond_var.signal } } | ||
end | ||
alias_method :enq, :push | ||
alias_method :<<, :push | ||
|
||
def pop(non_block = false, timeout: nil) | ||
if non_block && timeout | ||
raise ArgumentError, "can't set a timeout if non_block is enabled" | ||
end | ||
|
||
if non_block | ||
super(true) | ||
elsif empty? && timed_out?(timeout) { @mutex.synchronize { @cond_var.wait(@mutex, timeout) } } | ||
nil | ||
else | ||
super(false) | ||
end | ||
end | ||
alias_method :deq, :pop | ||
alias_method :shift, :pop | ||
|
||
private | ||
|
||
def timed_out?(timeout) | ||
return unless timeout | ||
|
||
# https://github.com/ruby/ruby/pull/4256 | ||
if RUBY_VERSION >= '3.1' | ||
yield.nil? | ||
else | ||
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout | ||
yield | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline | ||
end | ||
end | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
lib/concurrent-ruby/concurrent/collection/timeout_queue.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Concurrent | ||
module Collection | ||
# @!visibility private | ||
# @!macro internal_implementation_note | ||
TimeoutQueueImplementation = if RUBY_VERSION >= '3.2' | ||
::Queue | ||
else | ||
require 'concurrent/collection/ruby_timeout_queue' | ||
RubyTimeoutQueue | ||
end | ||
private_constant :TimeoutQueueImplementation | ||
|
||
# @!visibility private | ||
# @!macro timeout_queue | ||
class TimeoutQueue < TimeoutQueueImplementation | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.