Class: Puma::Reactor

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/reactor.rb

Constant Summary collapse

DefaultSleepFor =
5

Instance Method Summary collapse

Constructor Details

#initialize(server, app_pool) ⇒ Reactor

Returns a new instance of Reactor.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/puma/reactor.rb', line 8

def initialize(server, app_pool)
  @server = server
  @events = server.events
  @app_pool = app_pool

  @mutex = Mutex.new
  @ready, @trigger = Puma::Util.pipe
  @input = []
  @sleep_for = DefaultSleepFor
  @timeouts = []

  @sockets = [@ready]
end

Instance Method Details

#add(c) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/puma/reactor.rb', line 170

def add(c)
  @mutex.synchronize do
    @input << c
    @trigger << "*"

    if c.timeout_at
      @timeouts << c
      @timeouts.sort! { |a,b| a.timeout_at <=> b.timeout_at }

      calculate_sleep
    end
  end
end

#calculate_sleepObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/puma/reactor.rb', line 156

def calculate_sleep
  if @timeouts.empty?
    @sleep_for = DefaultSleepFor
  else
    diff = @timeouts.first.timeout_at.to_f - Time.now.to_f

    if diff < 0.0
      @sleep_for = 0
    else
      @sleep_for = diff
    end
  end
end

#clear!Object

Close all watched sockets and clear them from being watched



185
186
187
188
189
190
# File 'lib/puma/reactor.rb', line 185

def clear!
  begin
    @trigger << "c"
  rescue IOError
  end
end

#runObject



134
135
136
137
138
139
# File 'lib/puma/reactor.rb', line 134

def run
  run_internal
ensure
  @trigger.close
  @ready.close
end

#run_in_threadObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/puma/reactor.rb', line 141

def run_in_thread
  @thread = Thread.new do
    begin
      run_internal
    rescue StandardError => e
      STDERR.puts "Error in reactor loop escaped: #{e.message} (#{e.class})"
      STDERR.puts e.backtrace
      retry
    ensure
      @trigger.close
      @ready.close
    end
  end
end

#shutdownObject



192
193
194
195
196
197
198
199
# File 'lib/puma/reactor.rb', line 192

def shutdown
  begin
    @trigger << "!"
  rescue IOError
  end

  @thread.join
end