Skip to content

Commit

Permalink
Support older rubies.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jan 1, 2024
1 parent e3555e0 commit 54c4de1
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 54c4de1

Please sign in to comment.