Class: Julewire::Ractor::ReplyTimeoutScheduler
- Inherits:
-
Object
- Object
- Julewire::Ractor::ReplyTimeoutScheduler
- Defined in:
- lib/julewire/ractor/reply_timeout_scheduler.rb
Constant Summary collapse
- THREAD_NAME =
"julewire-ractor-reply-timeout"
Instance Method Summary collapse
- #cancel(thread) ⇒ Object
-
#initialize(timeout_value:) ⇒ ReplyTimeoutScheduler
constructor
A new instance of ReplyTimeoutScheduler.
- #schedule(reply, timeout:) ⇒ Object
- #with_timeout(reply, timeout:) ⇒ Object
Constructor Details
#initialize(timeout_value:) ⇒ ReplyTimeoutScheduler
Returns a new instance of ReplyTimeoutScheduler.
8 9 10 |
# File 'lib/julewire/ractor/reply_timeout_scheduler.rb', line 8 def initialize(timeout_value:) @timeout_value = timeout_value end |
Instance Method Details
#cancel(thread) ⇒ Object
20 21 22 23 24 |
# File 'lib/julewire/ractor/reply_timeout_scheduler.rb', line 20 def cancel(thread) thread&.kill thread&.join nil end |
#schedule(reply, timeout:) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/julewire/ractor/reply_timeout_scheduler.rb', line 12 def schedule(reply, timeout:) Thread.new(Float(timeout)) do |delay| Thread.current.name = THREAD_NAME sleep(delay) send_timeout(reply) end end |
#with_timeout(reply, timeout:) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/julewire/ractor/reply_timeout_scheduler.rb', line 26 def with_timeout(reply, timeout:) token = schedule(reply, timeout: timeout) yield ensure cancel(token) end |