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
# File 'lib/httpx/selector.rb', line 19

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

Instance Method Details

#deregister(io) ⇒ Object

deregisters io from selectables.



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

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

#each(&blk) ⇒ Object



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

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

#each_connection(&block) ⇒ Object



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

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

  @selectables.each do |c|
    if c.is_a?(Resolver::Resolver)
      c.each_connection(&block)
    else
      yield c
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/httpx/selector.rb', line 95

def empty?
  @selectables.empty?
end

#find_connection(request_uri, options) ⇒ Object



83
84
85
86
87
# File 'lib/httpx/selector.rb', line 83

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

#find_mergeable_connection(connection) ⇒ Object



89
90
91
92
93
# File 'lib/httpx/selector.rb', line 89

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

#find_resolver(options) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/httpx/selector.rb', line 63

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/httpx/selector.rb', line 28

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

    begin
      select(timeout, &:call)
      @timers.fire
    rescue TimeoutError => e
      @timers.fire(e)
    end
  end
rescue StandardError => e
  emit_error(e)
rescue Exception # rubocop:disable Lint/RescueException
  each_connection(&:force_reset)
  raise
end

#register(io) ⇒ Object

register io.



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

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

  @selectables << io
end

#terminateObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/httpx/selector.rb', line 50

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

  selectables.each(&:terminate)

  until selectables.empty?
    next_tick

    selectables &= @selectables
  end
end