Class: AWS::Core::Http::EMHttpHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/core/http/em_http_handler.rb

Overview

An EM-Synchrony implementation for Fiber based asynchronous ruby application. See github.com/igrigorik/async-rails and www.mikeperham.com/2010/04/03/introducing-phat-an-asynchronous-rails-app/ for examples of Aync-Rails application

In Rails add the following to your aws.rb initializer

require ‘aws-sdk’ require ‘aws/core/http/em_http_handler’ AWS.config(

:http_handler => AWS::Http::EMHttpHandler.new(
  :proxy => {:host => '127.0.0.1',    # proxy address
     :port => 9000,                 # proxy port
     :type => :socks5},
:pool_size => 20,   # Default is 0, set to > 0 to enable pooling
:async => false))   # If set to true all requests are handle asynchronously
                    # and initially return nil

EM-AWS exposes all connections options for EM-Http-Request at initialization For more information on available options see github.com/igrigorik/em-http-request/wiki/Issuing-Requests#available-connection–request-parameters If Options from the request section of the above link are present, they set on every request but may be over written by the request object

Direct Known Subclasses

Http::EMHttpHandler

Constant Summary collapse

EM_PASS_THROUGH_ERRORS =
[
  NoMethodError, FloatDomainError, TypeError, NotImplementedError,
  SystemExit, Interrupt, SyntaxError, RangeError, NoMemoryError,
  ArgumentError, ZeroDivisionError, LoadError, NameError,
  LocalJumpError, SignalException, ScriptError,
  SystemStackError, RegexpError, IndexError,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EMHttpHandler

Constructs a new HTTP handler using EM-Synchrony. each request. These options will be sent to get, post, head, put, or delete when a request is made. Note that :body, :head, :parser, and :ssl_ca_file are ignored. If you need to set the CA file see: github.com/igrigorik/em-http-request/wiki/Issuing-Requests#available-connection–request-parameters

Parameters:

  • options (Hash) (defaults to: {})

    Default options to send to EM-Synchrony on



51
52
53
54
55
56
57
# File 'lib/aws/core/http/em_http_handler.rb', line 51

def initialize options = {}
  @default_options = options
  @client_options = fetch_client_options
  @pool_options = fetch_pool_options
  @pool = HotTub::Session.new(pool_options) { |url|
    EM::HttpRequest.new(url,client_options)} if with_pool?
end

Instance Attribute Details

#client_optionsObject (readonly)

Returns the value of attribute client_options.



42
43
44
# File 'lib/aws/core/http/em_http_handler.rb', line 42

def client_options
  @client_options
end

#default_optionsObject (readonly)

Returns the value of attribute default_options.



42
43
44
# File 'lib/aws/core/http/em_http_handler.rb', line 42

def default_options
  @default_options
end

#poolObject (readonly)

Returns the value of attribute pool.



42
43
44
# File 'lib/aws/core/http/em_http_handler.rb', line 42

def pool
  @pool
end

#pool_optionsObject (readonly)

Returns the value of attribute pool_options.



42
43
44
# File 'lib/aws/core/http/em_http_handler.rb', line 42

def pool_options
  @pool_options
end

Instance Method Details

#handle(request, response, &read_block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/aws/core/http/em_http_handler.rb', line 59

def handle(request,response,&read_block)
  if EM::reactor_running?
    process_request(request,response,&read_block)
  else
    EM.synchrony do
      process_request(request,response,&read_block)
      pool.close_all if pool
      EM.stop
    end
  end
end

#handle_async(request, response, handle, &read_block) ⇒ Object

If the request option :async are set to true that request will handled asynchronously returning nil initially and processing in the background managed by EM-Synchrony. If the client option :async all requests will be handled asynchronously. EX:

EM.synchrony do
  s3 = AWS::S3.new
  s3.obj.write('test', :async => true) => nil
  EM::Synchrony.sleep(2)
  s3.obj.read => # 'test'
  EM.stop
end


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/aws/core/http/em_http_handler.rb', line 83

def handle_async(request,response,handle,&read_block)
  if EM::reactor_running?
    process_request(request,response,true,&read_block)
  else
    EM.synchrony do
      process_request(request,response,true,&read_block)
      pool.close_all if @pool
      EM.stop
    end
  end
end

#with_pool?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/aws/core/http/em_http_handler.rb', line 95

def with_pool?
  (default_options[:pool_size].to_i > 0)
end