Class: EC2APIProxy::Proxy
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- EC2APIProxy::Proxy
- Defined in:
- lib/ec2-api-proxy/proxy.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#memcached ⇒ Object
readonly
Returns the value of attribute memcached.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options, memcached) ⇒ Proxy
constructor
A new instance of Proxy.
- #post_init ⇒ Object
- #receive_data(data) ⇒ Object
- #unbind ⇒ Object
Constructor Details
#initialize(options, memcached) ⇒ Proxy
Returns a new instance of Proxy.
13 14 15 16 17 18 19 |
# File 'lib/ec2-api-proxy/proxy.rb', line 13 def initialize(, memcached) @options = @memcached = memcached @logger = Logger.new($stdout) @data_len = nil @data = '' end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/ec2-api-proxy/proxy.rb', line 10 def logger @logger end |
#memcached ⇒ Object (readonly)
Returns the value of attribute memcached.
11 12 13 |
# File 'lib/ec2-api-proxy/proxy.rb', line 11 def memcached @memcached end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/ec2-api-proxy/proxy.rb', line 9 def @options end |
Instance Method Details
#post_init ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ec2-api-proxy/proxy.rb', line 21 def post_init if (peername = get_peername) @connect_from = Socket.unpack_sockaddr_in(peername) end if @options[:debug] port, ip = @connect_from @logger.debug("proxy: connect from #{ip}:#{port}") end end |
#receive_data(data) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ec2-api-proxy/proxy.rb', line 41 def receive_data(data) @data << data unless @data_len content_length = data.split("\r\n").find {|i| /\AContent-Length:/i =~ i } @data_len = content_length ? content_length.split(/:\s+/, 2).last.to_i : 0 end content = @data.split("\r\n\r\n", 2).last || '' @logger.debug("proxy: receive data (length=#{@data_len}, receive=#{content.length})") if @options[:debug] return if content.length < @data_len EM.defer { begin endpoint, params = parse_proxy_request(@data) action = (params['Action'] || []).first allow_cache = !!(/\ADescribe/ =~ action) key = params_to_key(params) @logger.debug("proxy: #{action} (allow_cache=#{allow_cache})") if @options[:debug] if (allow_cache and cache = @memcached.get(key)) @logger.debug("proxy: cache hit (length=#{cache.length})") if @options[:debug] send_data(cache) close_connection_after_writing else @logger.debug("proxy: cache miss") if @options[:debug] @backend = EM.connect( endpoint.host, @options[:use_https] ? 443 : endpoint.port, EC2APIProxy::Backend, self, allow_cache ? key : nil ) if @backend.error? @logger.error("proxy: backend connection error: #{@data.inspect}") send_error('ec2-api-proxy-backend-connection-error', 'backend connection error') close_backend_connection close_connection_after_writing else @backend.send_data(@data) end end rescue => e @logger.error("#{e.class.name}: #{e.}\n\tfrom #{e.backtrace.join("\n\tfrom ")}") send_error(e.class.name, e.) close_backend_connection close_connection_after_writing end } end |
#unbind ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/ec2-api-proxy/proxy.rb', line 32 def unbind close_backend_connection if @options[:debug] port, ip = @connect_from @logger.debug("proxy: unbind connection from #{ip}:#{port}") end end |