Class: HTTPX::Selector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/httpx/selector.rb

Instance Method Summary collapse

Constructor Details

#initializeSelector

Returns a new instance of Selector.



19
20
21
22
23
# File 'lib/httpx/selector.rb', line 19

def initialize
  @timers = Timers.new
  @selectables = []
  @is_timer_interval = false
end

Instance Method Details

#deregister(io) ⇒ Object

deregisters io from selectables.



109
110
111
# File 'lib/httpx/selector.rb', line 109

def deregister(io)
  @selectables.delete(io)
end

#each(&blk) ⇒ Object



25
26
27
# File 'lib/httpx/selector.rb', line 25

def each(&blk)
  @selectables.each(&blk)
end

#each_connection(&block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/httpx/selector.rb', line 83

def each_connection(&block)
  return enum_for(__method__) unless block

  @selectables.each do |c|
    case c
    when Resolver::Resolver
      c.each_connection(&block)
    when Connection
      yield c
    end
  end
end

#find_connection(request_uri, options) ⇒ Object



96
97
98
99
100
# File 'lib/httpx/selector.rb', line 96

def find_connection(request_uri, options)
  each_connection.find do |connection|
    connection.match?(request_uri, options)
  end
end

#find_mergeable_connection(connection) ⇒ Object



102
103
104
105
106
# File 'lib/httpx/selector.rb', line 102

def find_mergeable_connection(connection)
  each_connection.find do |ch|
    ch != connection && ch.mergeable?(connection)
  end
end

#find_resolver(options) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/httpx/selector.rb', line 75

def find_resolver(options)
  res = @selectables.find do |c|
    c.is_a?(Resolver::Resolver) && options == c.options
  end

  res.multi if res
end

#next_tickObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/httpx/selector.rb', line 29

def next_tick
  catch(:jump_tick) do
    timeout = next_timeout
    if timeout && timeout.negative?
      @timers.fire
      throw(:jump_tick)
    end

    begin
      select(timeout) do |c|
        c.log(level: 2) { "[#{c.state}] selected#{" after #{timeout} secs" unless timeout.nil?}..." }

        c.call
      end

      @timers.fire
    rescue TimeoutError => e
      @timers.fire(e)
    end
  end
rescue StandardError => e
  each_connection do |c|
    c.emit(:error, e)
  end
rescue Exception # rubocop:disable Lint/RescueException
  each_connection do |conn|
    conn.force_reset
    conn.disconnect
  end

  raise
end

#register(io) ⇒ Object

register io.



114
115
116
117
118
# File 'lib/httpx/selector.rb', line 114

def register(io)
  return if @selectables.include?(io)

  @selectables << io
end

#terminateObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/httpx/selector.rb', line 62

def terminate
  # array may change during iteration
  selectables = @selectables.reject(&:inflight?)

  selectables.each(&:terminate)

  until selectables.empty?
    next_tick

    selectables &= @selectables
  end
end