Skip to content

Latest commit

 

History

History
78 lines (59 loc) · 2.06 KB

em-synchrony.md

File metadata and controls

78 lines (59 loc) · 2.06 KB
layout title permalink hide top_name top_link
documentation
EM-Synchrony Adapter
/adapters/em-synchrony
true
Adapters
./

This Adapter uses EventMachine and the tie in em-http-request in conjunction with em-synchrony

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:

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:

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

Base request

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

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