Skip to content

Commit 57e7150

Browse files
authored
Merge pull request #157 from Shopify/explain-HTTPLoader
2 parents 425d9dc + 400237c commit 57e7150

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

examples/http_loader.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
# }
3636
# GQL
3737

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

4748
def perform(operations)
49+
# This fans out and starts off all the concurrent work, which starts and
50+
# immediately returns Concurrent::Promises::Future` objects for each operation.
4851
futures = operations.map do |operation|
4952
Concurrent::Promises.future do
5053
pool.with { |connection| operation.call(connection) }
5154
end
5255
end
56+
# At this point, all of the concurrent work has been started.
57+
58+
# This converges back in, waiting on each concurrent future to finish, and fulfilling each
59+
# (non-concurrent) Promise.rb promise.
5360
operations.each_with_index.each do |operation, index|
54-
fulfill(operation, futures[index].value)
61+
fulfill(operation, futures[index].value) # .value is a blocking call
5562
end
5663
end
5764

0 commit comments

Comments
 (0)