Class: Faraday::Adapter::EMHttp::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/adapter/em_http.rb

Overview

This parallel manager is designed to start an EventMachine loop and block until all registered requests have been completed.

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.

See Also:


228
229
230
# File 'lib/faraday/adapter/em_http.rb', line 228

def initialize
  reset
end

Instance Method Details

#add(&block) ⇒ Object


246
247
248
249
250
251
252
253
# File 'lib/faraday/adapter/em_http.rb', line 246

def add(&block)
  if running?
    perform_request(&block)
  else
    @registered_procs << block
  end
  @num_registered += 1
end

#check_finishedObject


283
284
285
# File 'lib/faraday/adapter/em_http.rb', line 283

def check_finished
  EventMachine.stop if @num_succeeded + @errors.size == @num_registered
end

#perform_requestObject


271
272
273
274
275
276
277
278
279
280
281
# File 'lib/faraday/adapter/em_http.rb', line 271

def perform_request
  client = yield
  client.callback do
    @num_succeeded += 1
    check_finished
  end
  client.errback do
    @errors << client.error
    check_finished
  end
end

#resetObject

Re-initializes instance variables


233
234
235
236
237
238
239
# File 'lib/faraday/adapter/em_http.rb', line 233

def reset
  @registered_procs = []
  @num_registered = 0
  @num_succeeded = 0
  @errors = []
  @running = false
end

#runObject


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/faraday/adapter/em_http.rb', line 255

def run
  if @num_registered.positive?
    @running = true
    EventMachine.run do
      @registered_procs.each do |proc|
        perform_request(&proc)
      end
    end
    unless @errors.empty?
      raise Faraday::ClientError, @errors.first || 'connection failed'
    end
  end
ensure
  reset
end

#running?Boolean

Returns:

  • (Boolean)

242
243
244
# File 'lib/faraday/adapter/em_http.rb', line 242

def running?
  @running
end