Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments to explain how the HTTPLoader example works #157

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/http_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# }
# GQL

# An example loader which is blocking and synchronous as a whole, but executes all of its operations concurrently.
module Loaders
class HTTPLoader < GraphQL::Batch::Loader
def initialize(host:, size: 4, timeout: 4)
Expand All @@ -45,13 +46,19 @@ def initialize(host:, size: 4, timeout: 4)
end

def perform(operations)
# This fans out and starts off all the concurrent work, which starts and
# immediately returns Concurrent::Promises::Future` objects for each operation.
futures = operations.map do |operation|
Concurrent::Promises.future do
pool.with { |connection| operation.call(connection) }
end
end
# At this point, all of the concurrent work has been started.

# This converges back in, waiting on each concurrent future to finish, and fulfilling each
# (non-concurrent) Promise.rb promise.
operations.each_with_index.each do |operation, index|
fulfill(operation, futures[index].value)
fulfill(operation, futures[index].value) # .value is a blocking call
end
end

Expand Down