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.



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

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



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puma/reactor.rb', line 152

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



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/puma/reactor.rb', line 138

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



167
168
169
170
171
172
# File 'lib/puma/reactor.rb', line 167

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

#runObject



116
117
118
119
120
121
# File 'lib/puma/reactor.rb', line 116

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

#run_in_threadObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/puma/reactor.rb', line 123

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



174
175
176
177
178
179
180
181
# File 'lib/puma/reactor.rb', line 174

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

  @thread.join
end