Class: HTTPX::Selector

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

Overview

Implements the selector loop, where it registers and monitors "Selectable" objects.

A Selectable object is an object which can calculate the interests (:r, :w or :rw, respectively "read", "write" or "read-write") it wants to monitor for, and returns (via to_io method) an IO object which can be passed to functions such as IO.select . More exhaustively, a Selectable must implement the following methods:

state

returns the state as a Symbol, must return :closed when disposed of resources.

to_io

returns the IO object.

call

gets called when the IO is ready.

interests

returns the current interests to monitor for, as described above.

timeout

returns nil or an integer, representing how long to wait for interests.

handle_socket_timeout(Numeric)

called when waiting for interest times out.

Constant Summary collapse

HAS_FIBER_SCHEDULER =

Returns:

  • (Boolean)
Fiber.respond_to?(:scheduler)
READABLE =

Returns:

  • (Array[io_interests])
%i[rw r].freeze
WRITABLE =

Returns:

  • (Array[io_interests])
%i[rw w].freeze
MAX_BUSY_ITERATIONS =

max number of consecutive ticks allowed to neither wait on IO nor call a ready selectable.

Returns:

  • (Integer)
1000

Instance Method Summary collapse

Instance Method Details

#deregister(io) ⇒ selectable?

deregisters io from selectables.

Parameters:

  • io (selectable)

Returns:

  • (selectable, nil)


150
151
152
# File 'lib/httpx/selector.rb', line 150

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

#each ⇒ ::Enumerator[selectable, self] #each ⇒ self

Overloads:

  • #each ⇒ ::Enumerator[selectable, self]

    Returns:

    • (::Enumerator[selectable, self])
  • #each ⇒ self

    Returns:

    • (self)

Yields:

Yield Parameters:

  • arg0 (selectable)

Yield Returns:

  • (void)


50
51
# File 'sig/selector.rbs', line 50

def each: () -> ::Enumerator[selectable, self]
| () { (selectable) -> void } -> self

#each_connection ⇒ void #each_connection ⇒ Enumerable[Connection]

Overloads:

  • #each_connection ⇒ void

    This method returns an undefined value.

  • #each_connection ⇒ Enumerable[Connection]

    Returns:

Yields:

Yield Parameters:

Yield Returns:

  • (void)


124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/httpx/selector.rb', line 124

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

#empty? ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/httpx/selector.rb', line 48

def empty?
  @selectables.empty? && @timers.empty?
end

#find_connection(request_uri, options) ⇒ Connection?

Parameters:

  • request_uri (http_uri)
  • options (Options)

Returns:



137
138
139
140
141
# File 'lib/httpx/selector.rb', line 137

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

#find_mergeable_connection(connection) ⇒ Connection?

Parameters:

Returns:



143
144
145
146
147
# File 'lib/httpx/selector.rb', line 143

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

#find_resolver(options) ⇒ resolver?

Parameters:

Returns:

  • (resolver, nil)


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

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

  res.multi if res
end

#initial_call ⇒ Object

first time the registered selectables are added, there's probably work to do.



53
54
55
# File 'lib/httpx/selector.rb', line 53

def initial_call
  @selectables.each(&:initial_call)
end

#next_tick ⇒ void

This method returns an undefined value.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/httpx/selector.rb', line 57

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

    begin
      selected = false

      select(timeout) do |c|
        selected = true

        c.log(level: 2) { "[#{c.state}] selected from selector##{object_id}#{" after #{timeout} secs" unless timeout.nil?}..." }

        c.call
      end

      @timers.fire
    rescue TimeoutError => e
      @timers.fire(e)

      @busy_iterations = 0
    else
      # a tick is healthy if it either called a ready selectable, or actually blocked waiting for one.
      # without a timeout, #select only blocks when something registered interests; with one,
      # it always sleeps or selects, but a zero timeout just polls.
      waited = timeout.nil? ? @last_select_registered.to_i.positive? : timeout.positive?

      if selected || waited
        @busy_iterations = 0
      else
        busy_loop_check(timeout)
      end
    end
  end
end

#register(io) ⇒ void

This method returns an undefined value.

register io.

Parameters:

  • io (selectable)


155
156
157
158
159
# File 'lib/httpx/selector.rb', line 155

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

  @selectables << io
end

#terminate(selectables = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • (Array[selectable])


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/httpx/selector.rb', line 97

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

  selectables.delete_if do |sel|
    sel.terminate
    # TODO: this check is racy: connections check in to the pool on termination, and may check out and
    # change state immediately after. find a more idempotent way of doing this.
    sel.current_selector != self
  end

  until selectables.empty?
    next_tick

    selectables &= @selectables
  end
end