Skip to content

Commit 5d0239e

Browse files
author
Robert Mosolgo
authored
Merge pull request #3502 from rmosolgo/dataloader-source-queue
Dataloader sources: Use a queue instead of a stack
2 parents ada9cc3 + 1017c50 commit 5d0239e

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

lib/graphql/dataloader.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,24 @@ def run
136136
# This is where an evented approach would be even better -- can we tell which
137137
# fibers are ready to continue, and continue execution there?
138138
#
139-
source_fiber_stack = if (first_source_fiber = create_source_fiber)
139+
source_fiber_queue = if (first_source_fiber = create_source_fiber)
140140
[first_source_fiber]
141141
else
142142
nil
143143
end
144144

145-
if source_fiber_stack
146-
# Use a stack with `.pop` here so that when a source causes another source to become pending,
147-
# that newly-pending source will run _before_ the one that depends on it.
148-
# (See below where the old fiber is pushed to the stack, then the new fiber is pushed on the stack.)
149-
while (outer_source_fiber = source_fiber_stack.pop)
145+
if source_fiber_queue
146+
while (outer_source_fiber = source_fiber_queue.shift)
150147
resume(outer_source_fiber)
151148

152-
if outer_source_fiber.alive?
153-
source_fiber_stack << outer_source_fiber
154-
end
155149
# If this source caused more sources to become pending, run those before running this one again:
156150
next_source_fiber = create_source_fiber
157151
if next_source_fiber
158-
source_fiber_stack << next_source_fiber
152+
source_fiber_queue << next_source_fiber
153+
end
154+
155+
if outer_source_fiber.alive?
156+
source_fiber_queue << outer_source_fiber
159157
end
160158
end
161159
end
@@ -224,16 +222,16 @@ def resume(fiber)
224222
#
225223
# @see https://github.com/rmosolgo/graphql-ruby/issues/3449
226224
def spawn_fiber
227-
fiber_locals = {}
225+
fiber_locals = {}
228226

229227
Thread.current.keys.each do |fiber_var_key|
230228
fiber_locals[fiber_var_key] = Thread.current[fiber_var_key]
231-
end
229+
end
232230

233-
Fiber.new do
231+
Fiber.new do
234232
fiber_locals.each { |k, v| Thread.current[k] = v }
235233
yield
236-
end
234+
end
237235
end
238236
end
239237
end

0 commit comments

Comments
 (0)