Class: Restify::Adapter::Typhoeus

Inherits:
Object
  • Object
show all
Defined in:
lib/restify/adapter/typhoeus.rb

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Typhoeus

Returns a new instance of Typhoeus.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/restify/adapter/typhoeus.rb', line 9

def initialize(**options)
  @queue   = Queue.new
  @hydra   = ::Typhoeus::Hydra.new
  @options = options

  Thread.new do
    begin
      loop do
        while (req = convert(*@queue.pop))
          @hydra.queue req

          if @queue.size == 0
            @hydra.run
          end
        end
      end
    rescue Exception => e
      puts "#{self.class}: #{e.message}"
    end
  end
end

Instance Method Details

#call(request) ⇒ Object



44
45
46
47
48
# File 'lib/restify/adapter/typhoeus.rb', line 44

def call(request)
  Promise.create do |writer|
    queue request, writer
  end
end

#queue(request, writer) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/restify/adapter/typhoeus.rb', line 35

def queue(request, writer)
  if sync?
    @hydra.queue convert request, writer
    @hydra.run
  else
    @queue.push [request, writer]
  end
end

#sync?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/restify/adapter/typhoeus.rb', line 31

def sync?
  @options.fetch :sync, false
end