Skip to content

Commit 68f5bfb

Browse files
refactored:
- variable names - removed mutex, as was added by mistake - `check_timeouts` now is thinner after unnecessary code deletion
1 parent 34a6765 commit 68f5bfb

1 file changed

Lines changed: 14 additions & 40 deletions

File tree

lib/rage/fiber_scheduler.rb

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
class Rage::FiberScheduler
66
MAX_READ = 65536
77
TIMEOUT_WORKER_INTERVAL = 100 # miliseconds
8-
FIBER_KILL_DELAY = 500 # miliseconds
98

109
def initialize
1110
@root_fiber = Fiber.current
1211
@dns_cache = {}
1312

14-
@alive_fibers = Hash.new { |h, k| h[k] = {} }
15-
@fibers_mutex = Mutex.new
13+
@fiber_timeouts = Hash.new { |h, k| h[k] = {} }
1614

1715
start_timeout_worker
1816
end
@@ -75,23 +73,18 @@ def kernel_sleep(duration = nil)
7573

7674
def timeout_after(duration, exception_class = Timeout::Error, *exception_arguments, &block)
7775
fiber = Fiber.current
78-
timeout_deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + duration
79-
80-
@fibers_mutex.synchronize do
81-
@alive_fibers[fiber.__get_id][timeout_deadline] = {
82-
fiber: fiber,
83-
timeout_deadline: timeout_deadline,
84-
exception_class: exception_class,
85-
exception_arguments: exception_arguments
86-
}
87-
end
76+
timeout = Process.clock_gettime(Process::CLOCK_MONOTONIC) + duration
77+
78+
@fiber_timeouts[fiber][timeout] = {
79+
exception_class: exception_class,
80+
exception_arguments: exception_arguments
81+
}
8882

8983
begin
9084
block.call
9185
ensure
92-
@fibers_mutex.synchronize do
93-
@alive_fibers[fiber.__get_id].delete(timeout_deadline)
94-
end
86+
@fiber_timeouts[fiber].delete(timeout)
87+
@fiber_timeouts.delete(fiber) if @fiber_timeouts[fiber].empty?
9588
end
9689
end
9790

@@ -164,38 +157,19 @@ def close
164157
private
165158

166159
def start_timeout_worker
167-
return unless ::Iodine.running?
168-
169160
::Iodine.run_every(Rage::FiberScheduler::TIMEOUT_WORKER_INTERVAL) do
170161
check_timeouts
171162
end
172163
end
173164

174165
def check_timeouts
175-
@fibers_mutex.synchronize do
176-
@alive_fibers.delete_if do |fiber_id, fiber_timeouts|
177-
fiber_timeouts.delete_if do |timeout_key, fiber_context|
178-
current_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
179-
180-
next false if current_time < fiber_context[:timeout_deadline]
181-
182-
fiber = fiber_context[:fiber]
183-
unblock(nil, fiber)
184-
185-
if fiber.alive?
186-
fiber.raise(fiber_context[:exception_class], *fiber_context[:exception_arguments])
187-
188-
::Iodine.run_after(FIBER_KILL_DELAY) do
189-
fiber.kill if fiber.alive?
190-
end
191-
else
192-
fiber_timeouts.delete(timeout_key)
193-
end
166+
@fiber_timeouts.each_pair do |fiber, timeouts|
167+
timeouts.delete_if do |timeout, context|
168+
next false if Process.clock_gettime(Process::CLOCK_MONOTONIC) < timeout
194169

195-
true
196-
end
170+
fiber.raise(context[:exception_class], *context[:exception_arguments]) if fiber.alive?
197171

198-
fiber_timeouts.length == 0
172+
true
199173
end
200174
end
201175
end

0 commit comments

Comments
 (0)