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.



112
113
114
# File 'lib/httpx/selector.rb', line 112

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



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/httpx/selector.rb', line 86

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



99
100
101
102
103
# File 'lib/httpx/selector.rb', line 99

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

#find_mergeable_connection(connection) ⇒ Object



105
106
107
108
109
# File 'lib/httpx/selector.rb', line 105

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

#find_resolver(options) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/httpx/selector.rb', line 78

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.



117
118
119
120
121
# File 'lib/httpx/selector.rb', line 117

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

  @selectables << io
end

#terminateObject



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

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

  selectables.delete_if do |sel|
    sel.terminate
    sel.state == :closed
  end

  until selectables.empty?
    next_tick

    selectables &= @selectables
  end
end