Skip to content

Commit

Permalink
Some docs on EventMachine adapters. (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
damau committed Apr 11, 2021
1 parent 99afc0f commit 8d379a1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
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 "Parallel manager: #{conn.parallel_manager}"

@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 "Parallel manager: #{conn.parallel_manager}"

@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 @@ -177,6 +178,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

0 comments on commit 8d379a1

Please sign in to comment.