From 54c4de1fb4422f2daf0c93b6fdc6de5efda92226 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 2 Jan 2024 12:15:56 +1300 Subject: [PATCH] Support older rubies. --- lib/async/scheduler.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/async/scheduler.rb b/lib/async/scheduler.rb index eb84a709..ec38fb26 100644 --- a/lib/async/scheduler.rb +++ b/lib/async/scheduler.rb @@ -165,15 +165,28 @@ def address_resolve(hostname) ::Resolv.getaddresses(hostname) end + + if IO.method_defined?(:timeout) + private def get_timeout(io) + io.timeout + end + else + private def get_timeout(io) + nil + end + end + # @asynchronous May be non-blocking.. def io_wait(io, events, timeout = nil) fiber = Fiber.current if timeout + # If an explicit timeout is specified, we expect that the user will handle it themselves: timer = @timers.after(timeout) do fiber.transfer end - elsif timeout = io.timeout + elsif timeout = get_timeout(io) + # Otherwise, if we default to the io's timeout, we raise an exception: timer = @timers.after(timeout) do fiber.raise(::IO::TimeoutError, "Timeout while waiting for IO to become ready!") end @@ -183,12 +196,12 @@ def io_wait(io, events, timeout = nil) ensure timer&.cancel end - + if ::IO::Event::Support.buffer? def io_read(io, buffer, length, offset = 0) fiber = Fiber.current - if timeout = io.timeout + if timeout = get_timeout(io) timer = @timers.after(timeout) do fiber.raise(::IO::TimeoutError, "execution expired") end @@ -203,7 +216,7 @@ def io_read(io, buffer, length, offset = 0) def io_write(io, buffer, length, offset = 0) fiber = Fiber.current - if timeout = io.timeout + if timeout = get_timeout(io) timer = @timers.after(timeout) do fiber.raise(::IO::TimeoutError, "execution expired") end