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

Some docs on EventMachine adapters. #1232

Merged
merged 4 commits into from Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 46 additions & 1 deletion docs/adapters/em-http.md
Expand Up @@ -7,20 +7,65 @@ top_name: Adapters
top_link: ./
---

This Adapter uses the [em-http-request][rdoc] gem to make HTTP requests.
This Adapter uses [EventMachine](https://github.com/eventmachine/eventmachine/) and the tie in [em-http-request][src]

It can be used to make parallel requests using EventMachine.

The major difference between this and EMSynchrony is that it does not use fibers.

**Error handling and responses have a slightly different behaviour and structure in some cases. Please run thorough testing scenarios, including connection failures and SSL failures**

You will need to add em-http-request to your Gemfile:

```ruby
# Gemfile
gem 'em-http-request'
```

### Base request
```ruby
require 'faraday'
require 'em-http-request'

conn = Faraday.new(...) do |f|
# no custom options available
f.adapter :em_http
end
```

### Parallel Requests

```ruby
require 'faraday'
require 'em-http-request'

urls = Array.new(5) { 'http://127.0.0.1:3000' }

conn = Faraday::Connection.new do |builder|
builder.adapter :em_http
end

begin
conn.in_parallel do
puts "Parrallel manager: #{conn.parallel_manager}"
damau marked this conversation as resolved.
Show resolved Hide resolved

@responses = urls.map do |url|
conn.get(url)
end
end
end

# Gather responses outside of block
puts @responses.map(&:status).join(', ')
puts @responses.map(&:status).compact.count
```

## Links

* [Gem RDoc][rdoc]
* [Gem source][src]
* [Adapter RDoc][adapter_rdoc]
* [EM-Synchrony Adapter](./em-synchrony.md)

[rdoc]: https://www.rubydoc.info/gems/em-http-request
[src]: https://github.com/igrigorik/em-http-request#readme
Expand Down
52 changes: 51 additions & 1 deletion docs/adapters/em-synchrony.md
Expand Up @@ -7,15 +7,65 @@ top_name: Adapters
top_link: ./
---

This Adapter uses the [em-synchrony][rdoc] gem to make HTTP requests.
This Adapter uses [EventMachine](https://github.com/eventmachine/eventmachine/) and the tie in [em-http-request](https://www.rubydoc.info/gems/em-http-request) in conjunction with [em-synchrony][rdoc]

It can be used to make parallel requests using EventMachine.

The key difference between this and EM-Http is that it uses fibers. For more information see igrigorik's blog posts on the matter:

- [fibers-cooperative-scheduling-in-ruby](https://www.igvita.com/2009/05/13/fibers-cooperative-scheduling-in-ruby/)
- [untangling-evented-code-with-ruby-fibers](https://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers)

**Error handling and responses have a slightly different behaviour and structure in some cases. Please run thorough testing scenarios, including connection failures and SSL failures**

You will need to add em-http-request and em-synchrony to your Gemfile:

```ruby
# Gemfile
gem 'em-http-request'
gem 'em-synchrony'
```

### Base request
```ruby
require 'faraday'
require 'em-http-request'
require 'em-synchrony'

conn = Faraday.new(...) do |f|
# no custom options available
f.adapter :em_synchrony
end
```

### Parallel Requests

```ruby
require 'faraday'
require 'em-http-request'
require 'em-synchrony'

urls = Array.new(5) { 'http://127.0.0.1:3000' }

conn = Faraday::Connection.new do |builder|
builder.adapter :em_synchrony
end

begin
conn.in_parallel do
puts "Parrallel manager: #{conn.parallel_manager}"
damau marked this conversation as resolved.
Show resolved Hide resolved

@responses = urls.map do |url|
conn.get(url)
end
end
end

# Gather responses outside of block
puts @responses.map(&:status).join(', ')
puts @responses.map(&:status).compact.count
```

## Links

* [Gem RDoc][rdoc]
Expand Down
2 changes: 2 additions & 0 deletions docs/adapters/index.md
Expand Up @@ -20,6 +20,7 @@ Faraday includes these adapters (but not the HTTP client libraries):
* [Excon][excon]
* [Patron][patron]
* [EM-Synchrony][em-synchrony]
* [EM-Http][em-http]
* [HTTPClient][httpclient]

While most adapters use a common Ruby HTTP client library, adapters can also
Expand Down Expand Up @@ -176,6 +177,7 @@ Compare to the finished example [em-synchrony](https://github.com/lostisland/far
[excon]: ./excon
[patron]: ./patron
[em-synchrony]: ./em-synchrony
[em-http]: ./em-http
[httpclient]: ./httpclient
[typhoeus]: https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
[faraday-http]: https://github.com/lostisland/faraday-http
Expand Down