Class: Restify::Adapter::EM::Connection
- Inherits:
-
Object
- Object
- Restify::Adapter::EM::Connection
- Defined in:
- lib/restify/adapter/em.rb
Instance Attribute Summary collapse
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
Class Method Summary collapse
Instance Method Summary collapse
- #call(request, writer, retried = false) ⇒ Object
- #connection ⇒ Object
-
#initialize(origin) ⇒ Connection
constructor
A new instance of Connection.
- #pipeline? ⇒ Boolean
- #process_next ⇒ Object
- #requests ⇒ Object
Constructor Details
#initialize(origin) ⇒ Connection
Returns a new instance of Connection.
22 23 24 25 |
# File 'lib/restify/adapter/em.rb', line 22 def initialize(origin) @origin = origin @pipeline = true end |
Instance Attribute Details
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
20 21 22 |
# File 'lib/restify/adapter/em.rb', line 20 def origin @origin end |
Class Method Details
.connections ⇒ Object
15 16 17 |
# File 'lib/restify/adapter/em.rb', line 15 def connections @connections ||= {} end |
.open(uri) ⇒ Object
11 12 13 |
# File 'lib/restify/adapter/em.rb', line 11 def open(uri) connections[uri.origin] ||= new uri.origin end |
Instance Method Details
#call(request, writer, retried = false) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/restify/adapter/em.rb', line 31 def call(request, writer, retried = false) if requests.empty? requests << [request, writer, retried] process_next else requests << [request, writer, retried] end end |
#connection ⇒ Object
40 41 42 |
# File 'lib/restify/adapter/em.rb', line 40 def connection @connection ||= EventMachine::HttpRequest.new(origin) end |
#pipeline? ⇒ Boolean
44 45 46 |
# File 'lib/restify/adapter/em.rb', line 44 def pipeline? @pipeline end |
#process_next ⇒ Object
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 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/restify/adapter/em.rb', line 48 def process_next return if requests.empty? request, writer, retried = pipeline? ? requests.shift : requests.first begin req = connection.send request.method.downcase, keepalive: true, redirects: 3, path: request.uri.normalized_path, query: request.uri.normalized_query, body: request.body, head: request.headers rescue Exception => err writer.reject err requests.shift unless pipeline? return end req.callback do requests.shift unless pipeline? writer.fulfill Response.new( request, req.last_effective_url, req.response_header.status, req.response_header, req.response ) if req.response_header['CONNECTION'] == 'close' @connection = nil @pipeline = false end process_next end req.errback do requests.shift unless pipeline? @connection = nil if pipeline? EventMachine.next_tick do @pipeline = false call request, writer end elsif !retried EventMachine.next_tick { call request, writer } else begin raise RuntimeError.new \ "(#{req.response_header.status}) #{req.error}" rescue => e writer.reject e end end end end |
#requests ⇒ Object
27 28 29 |
# File 'lib/restify/adapter/em.rb', line 27 def requests @requests ||= [] end |