Class: GraphQL::Dataloader::AsyncDataloader

Inherits:
GraphQL::Dataloader show all
Defined in:
lib/graphql/dataloader/async_dataloader.rb

Instance Attribute Summary

Attributes inherited from GraphQL::Dataloader

#fiber_limit

Instance Method Summary collapse

Methods inherited from GraphQL::Dataloader

#append_job, #cleanup_fiber, #clear_cache, #get_fiber_variables, #initialize, #nonblocking?, #run_fiber, #run_isolated, #set_fiber_variables, #spawn_fiber, use, #with, with_dataloading

Constructor Details

This class inherits a constructor from GraphQL::Dataloader

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/graphql/dataloader/async_dataloader.rb', line 14

def run
  jobs_fiber_limit, total_fiber_limit = calculate_fiber_limit
  job_fibers = []
  next_job_fibers = []
  source_tasks = []
  next_source_tasks = []
  first_pass = true
  sources_condition = Async::Condition.new
  manager = spawn_fiber do
    while first_pass || !job_fibers.empty?
      first_pass = false
      fiber_vars = get_fiber_variables

      while (f = (job_fibers.shift || (((job_fibers.size + next_job_fibers.size + source_tasks.size) < jobs_fiber_limit) && spawn_job_fiber)))
        if f.alive?
          finished = run_fiber(f)
          if !finished
            next_job_fibers << f
          end
        end
      end
      job_fibers.concat(next_job_fibers)
      next_job_fibers.clear

      Sync do |root_task|
        set_fiber_variables(fiber_vars)
        while !source_tasks.empty? || @source_cache.each_value.any? { |group_sources| group_sources.each_value.any?(&:pending?) }
          while (task = (source_tasks.shift || (((job_fibers.size + next_job_fibers.size + source_tasks.size + next_source_tasks.size) < total_fiber_limit) && spawn_source_task(root_task, sources_condition))))
            if task.alive?
              root_task.yield # give the source task a chance to run
              next_source_tasks << task
            end
          end
          sources_condition.signal
          source_tasks.concat(next_source_tasks)
          next_source_tasks.clear
        end
      end
    end
  end

  manager.resume
  if manager.alive?
    raise "Invariant: Manager didn't terminate successfully: #{manager}"
  end

rescue UncaughtThrowError => e
  throw e.tag, e.value
end

#yieldObject



5
6
7
8
9
10
11
12
# File 'lib/graphql/dataloader/async_dataloader.rb', line 5

def yield
  if (condition = Fiber[:graphql_dataloader_next_tick])
    condition.wait
  else
    Fiber.yield
  end
  nil
end