Class: GraphQL::Dataloader::AsyncDataloader::Run
- Inherits:
-
Object
- Object
- GraphQL::Dataloader::AsyncDataloader::Run
- Defined in:
- lib/graphql/dataloader/async_dataloader.rb
Instance Attribute Summary collapse
-
#dataloader ⇒ Object
readonly
Returns the value of attribute dataloader.
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#jobs_fiber_limit ⇒ Object
readonly
Returns the value of attribute jobs_fiber_limit.
-
#lazies_at_depth ⇒ Object
readonly
Returns the value of attribute lazies_at_depth.
-
#root_task ⇒ Object
Returns the value of attribute root_task.
-
#snoozed_jobs_condition ⇒ Object
readonly
Returns the value of attribute snoozed_jobs_condition.
-
#snoozed_sources_condition ⇒ Object
readonly
Returns the value of attribute snoozed_sources_condition.
-
#trace ⇒ Object
Returns the value of attribute trace.
Instance Method Summary collapse
- #check_error! ⇒ Object
- #close_queues ⇒ Object
- #current_sources_fiber_limit ⇒ Object
-
#expect_resumes(count) ⇒ Object
Signalled tasks don't appear in any accounting until their first slice pushes
:resumed_task, so they have to be counted at signal time:. - #has_bandwidth? ⇒ Boolean
- #has_pending_work? ⇒ Boolean
-
#initialize(dataloader, total_fiber_limit, jobs_fiber_limit) ⇒ Run
constructor
A new instance of Run.
- #jobs_bandwidth? ⇒ Boolean
- #new_queues(mode) ⇒ Object
-
#push_task_message(msg, data) ⇒ Object
Push to the tasks_channel, tolerating a closed channel: on the error path,
run_queuecloses the channel while sibling tasks can still run one more slice beforeroot_task.cancelreaches them. - #quiesced? ⇒ Boolean
- #running? ⇒ Boolean
- #sources_bandwidth? ⇒ Boolean
- #wait_for_activity ⇒ Object
Constructor Details
#initialize(dataloader, total_fiber_limit, jobs_fiber_limit) ⇒ Run
Returns a new instance of Run.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 53 def initialize(dataloader, total_fiber_limit, jobs_fiber_limit) @dataloader = dataloader @root_task = nil @trace = nil @jobs = [] @total_fiber_limit = total_fiber_limit @jobs_fiber_limit = jobs_fiber_limit @lazies_at_depth = Hash.new { |h, k| h[k] = [] } @running_tasks = nil @tasks_channel = nil @tasks_channel_task = nil @activity = nil @task_error = nil @expected_resumes = 0 @mode = nil @snoozed_jobs_condition = Async::Condition.new @snoozed_sources_condition = Async::Condition.new end |
Instance Attribute Details
#dataloader ⇒ Object (readonly)
Returns the value of attribute dataloader.
77 78 79 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 77 def dataloader @dataloader end |
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
77 78 79 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 77 def jobs @jobs end |
#jobs_fiber_limit ⇒ Object (readonly)
Returns the value of attribute jobs_fiber_limit.
77 78 79 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 77 def jobs_fiber_limit @jobs_fiber_limit end |
#lazies_at_depth ⇒ Object (readonly)
Returns the value of attribute lazies_at_depth.
77 78 79 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 77 def lazies_at_depth @lazies_at_depth end |
#root_task ⇒ Object
Returns the value of attribute root_task.
75 76 77 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 75 def root_task @root_task end |
#snoozed_jobs_condition ⇒ Object (readonly)
Returns the value of attribute snoozed_jobs_condition.
77 78 79 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 77 def snoozed_jobs_condition @snoozed_jobs_condition end |
#snoozed_sources_condition ⇒ Object (readonly)
Returns the value of attribute snoozed_sources_condition.
77 78 79 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 77 def snoozed_sources_condition @snoozed_sources_condition end |
#trace ⇒ Object
Returns the value of attribute trace.
75 76 77 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 75 def trace @trace end |
Instance Method Details
#check_error! ⇒ Object
128 129 130 131 132 133 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 128 def check_error! if (err = @task_error) @task_error = nil raise err end end |
#close_queues ⇒ Object
87 88 89 90 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 87 def close_queues @tasks_channel.close @tasks_channel_task.cancel end |
#current_sources_fiber_limit ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 169 def current_sources_fiber_limit within_limit = @total_fiber_limit - running_count if within_limit < 1 1 else within_limit end end |
#expect_resumes(count) ⇒ Object
Signalled tasks don't appear in any accounting until their first slice
pushes :resumed_task, so they have to be counted at signal time:
124 125 126 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 124 def expect_resumes(count) @expected_resumes = count end |
#has_bandwidth? ⇒ Boolean
118 119 120 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 118 def has_bandwidth? @mode == :jobs ? jobs_bandwidth? : sources_bandwidth? end |
#has_pending_work? ⇒ Boolean
114 115 116 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 114 def has_pending_work? @mode == :jobs ? @jobs.any? : @dataloader.pending_sources.any?(&:pending?) # rubocop:disable Development/NoneWithoutBlockCop end |
#jobs_bandwidth? ⇒ Boolean
79 80 81 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 79 def jobs_bandwidth? running_count < @jobs_fiber_limit end |
#new_queues(mode) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 135 def new_queues(mode) @mode = mode @tasks_channel = Async::Queue.new(parent: @root_task) @activity = Async::Condition.new @task_error = nil @expected_resumes = 0 @running_tasks = [] @tasks_channel_task = @root_task.async do |_t| while ((msg, data) = @tasks_channel.wait) case msg when :started_task @running_tasks.push(data) data.run when :resumed_task if @expected_resumes > 0 @expected_resumes -= 1 end @running_tasks.push(data) when :finished_task, :paused_task @running_tasks.delete(data) when :task_error @task_error ||= data else raise ArgumentError, "Unknown tasks_channel action: #{msg.inspect}" end @activity.signal end end end |
#push_task_message(msg, data) ⇒ Object
Push to the tasks_channel, tolerating a closed channel: on the error path, run_queue
closes the channel while sibling tasks can still run one more slice before
root_task.cancel reaches them. Record :task_error payloads so they aren't lost, and
return false so the caller can stop the task instead of raising ClosedError into user code.
96 97 98 99 100 101 102 103 104 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 96 def (msg, data) @tasks_channel.push([msg, data]) true rescue Async::Queue::ClosedError if msg == :task_error @task_error ||= data end false end |
#quiesced? ⇒ Boolean
110 111 112 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 110 def quiesced? @running_tasks.empty? && @tasks_channel.empty? && @expected_resumes == 0 end |
#running? ⇒ Boolean
165 166 167 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 165 def running? @snoozed_jobs_condition.waiting? || @snoozed_sources_condition.waiting? end |
#sources_bandwidth? ⇒ Boolean
83 84 85 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 83 def sources_bandwidth? running_count < current_sources_fiber_limit end |
#wait_for_activity ⇒ Object
106 107 108 |
# File 'lib/graphql/dataloader/async_dataloader.rb', line 106 def wait_for_activity @activity.wait end |