-
Notifications
You must be signed in to change notification settings - Fork 983
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 support for a new ParallelManager#execute
method.
#1584
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,18 +42,34 @@ class FlorpHttp < ::Faraday::Adapter | |
def self.setup_parallel_manager(_options = nil) | ||
FlorpParallelManager.new # NB: we will need to define this | ||
end | ||
end | ||
|
||
class FlorpParallelManager | ||
def add(request, method, *args, &block) | ||
# Collect the requests | ||
def call(env) | ||
# NB: you can call `in_parallel?` here to check if the current request | ||
# is part of a parallel batch. Useful if you need to collect all requests | ||
# into the ParallelManager before running them. | ||
end | ||
end | ||
|
||
def run | ||
# Process the requests | ||
class FlorpParallelManager | ||
# The execute method will be passed the same block as `in_parallel`, | ||
# so you can either collect the requests or just wrap them into a wrapper, | ||
# depending on how your adapter works. | ||
def execute(&block) | ||
run_async(&block) | ||
end | ||
end | ||
``` | ||
|
||
Compare to the finished example [em-synchrony](https://github.com/lostisland/faraday-em_synchrony/blob/main/lib/faraday/adapter/em_synchrony.rb) | ||
### A note on the old, deprecated interface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, and so good with this note! |
||
|
||
Prior to the introduction of the `execute` method, the `ParallelManager` was expected to implement a `run` method | ||
and the execution of the block was done by the Faraday connection BEFORE calling that method. | ||
|
||
This approach made the `ParallelManager` implementation harder and forced you to keep state around. | ||
The new `execute` implementation allows to avoid this shortfall and support different flows. | ||
|
||
As of Faraday 2.0, `run` is still supported in case `execute` is not implemented by the `ParallelManager`, | ||
but this method should be considered deprecated. | ||
|
||
For reference, please see an example using `run` from [em-synchrony](https://github.com/lostisland/faraday-em_synchrony/blob/main/lib/faraday/adapter/em_synchrony.rb) | ||
and its [ParallelManager implementation](https://github.com/lostisland/faraday-em_synchrony/blob/main/lib/faraday/adapter/em_synchrony/parallel_manager.rb). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,15 +314,23 @@ def in_parallel? | |
# | ||
# @yield a block to execute multiple requests. | ||
# @return [void] | ||
def in_parallel(manager = nil) | ||
def in_parallel(manager = nil, &block) | ||
@parallel_manager = manager || default_parallel_manager do | ||
warn 'Warning: `in_parallel` called but no parallel-capable adapter ' \ | ||
'on Faraday stack' | ||
warn caller[2, 10].join("\n") | ||
nil | ||
end | ||
yield | ||
@parallel_manager&.run | ||
return yield unless @parallel_manager | ||
|
||
if @parallel_manager.respond_to?(:execute) | ||
# Execute is the new method that is responsible for executing the block. | ||
@parallel_manager.execute(&block) | ||
else | ||
# TODO: Old behaviour, deprecate and remove in 3.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Very kind not to emit deprecation warnings, here!) |
||
yield | ||
@parallel_manager.run | ||
end | ||
ensure | ||
@parallel_manager = nil | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This context-giving comment is gold.