Perform HTTP requests in parallel
- Ruby
- Go
Install the gem and add to the application's Gemfile by executing:
bundle add funnel_http
If bundler is not being used to manage dependencies, install the gem by executing:
gem install funnel_http
Use FunnelHttp::Client#perform
require "funnel_http"
client = FunnelHttp::Client.new
requests = [
{
method: :get,
uri: "https://example.com/api/user/1",
},
# with request header
{
method: :get,
uri: "https://example.com/api/user/2",
header: {
"Authorization" => "Bearer xxxxxxxx",
"X-Multiple-Values" => ["1st value", "2nd value"],
},
},
# with request body
{
method: :post,
uri: "https://example.com/api/user",
header: {
"Authorization" => "Bearer xxxxxxxx",
"Content-Type" => "application/json",
},
body: '{"name": "sue445"}',
},
]
responses = client.perform(requests)
# => [
# { status_code: 200, body: "Response of /api/user/1", header: { "Content-Type" => ["text/plain;charset=utf-8"]} }
# { status_code: 200, body: "Response of /api/user/2", header: { "Content-Type" => ["text/plain;charset=utf-8"]} }
# { status_code: 200, body: "Response of /api/user", header: { "Content-Type" => ["text/plain;charset=utf-8"]} }
# ]
Example1. Pass default header to FunnelHttp::Client#initialize
default_header = { "Authorization" => "Bearer xxxxxx" }
client = FunnelHttp::Client.new(default_header)
Example 2. Use FunnelHttp::Client#add_default_request_header
client.add_default_request_header("Authorization", "Bearer xxxxxx")
https://sue445.github.io/funnel_http/
Depending on the case, funnel_http
runs about 1.2x faster than pure-Ruby Thread
💨
See benchmark/
funnel_http
uses Go's goroutine for asynchronous processing of HTTP requests.
So this is faster than Ruby in many cases.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/sue445/funnel_http.
The gem is available as open source under the terms of the MIT License.