Class: Restify::Adapter::PooledEM

Inherits:
Base
  • Object
show all
Includes:
Logging
Defined in:
lib/restify/adapter/pooled_em.rb

Defined Under Namespace

Classes: Pool

Instance Method Summary collapse

Methods included from Logging

#_fmt, #_log_prefix, #debug, #logger

Methods inherited from Base

#call

Constructor Details

#initialize(**kwargs) ⇒ PooledEM

Returns a new instance of PooledEM.



199
200
201
# File 'lib/restify/adapter/pooled_em.rb', line 199

def initialize(**kwargs)
  @pool = Pool.new(**kwargs)
end

Instance Method Details

#call_native(request, writer) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/BlockLength



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/restify/adapter/pooled_em.rb', line 206

def call_native(request, writer)
  next_tick do
    defer = @pool.get(request)

    defer.errback do |error|
      writer.reject(error)
    end

    defer.callback do |conn|
      req = conn.send request.method.downcase,
        keepalive: true,
        redirects: 3,
        path: request.uri.normalized_path,
        query: request.uri.normalized_query,
        body: request.body,
        head: request.headers

      req.callback do
        writer.fulfill Response.new(
          request,
          req.last_effective_url,
          req.response_header.status,
          req.response_header,
          req.response
        )

        if req.response_header['CONNECTION'] == 'close'
          @pool.remove(conn)
        else
          @pool << conn
        end
      end

      req.errback do
        @pool.remove(conn)
        writer.reject(req.error)
      end
    rescue Exception => e # rubocop:disable Lint/RescueException
      @pool.remove(conn)
      writer.reject(e)
    end
  end
end