Class: ActionController::Live::Buffer

Inherits:
ActionDispatch::Response::Buffer show all
Includes:
MonitorMixin
Defined in:
lib/action_controller/metal/live.rb

Overview

:nodoc:

Constant Summary

Constants inherited from ActionDispatch::Response::Buffer

ActionDispatch::Response::Buffer::BODY_METHODS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ActionDispatch::Response::Buffer

#body, #closed?, #each, #respond_to?, #to_ary

Constructor Details

#initialize(response) ⇒ Buffer

Returns a new instance of Buffer.



210
211
212
213
214
215
216
# File 'lib/action_controller/metal/live.rb', line 210

def initialize(response)
  super(response, build_queue(self.class.queue_size))
  @error_callback = lambda { true }
  @cv = new_cond
  @aborted = false
  @ignore_disconnect = false
end

Class Attribute Details

.queue_size ⇒ Object

Returns the value of attribute queue_size.



199
200
201
# File 'lib/action_controller/metal/live.rb', line 199

def queue_size
  @queue_size
end

Instance Attribute Details

#ignore_disconnect ⇒ Object

Ignore that the client has disconnected.

If this value is true, calling write after the client disconnects will result in the written content being silently discarded. If this value is false (the default), a ClientDisconnected exception will be raised.



208
209
210
# File 'lib/action_controller/metal/live.rb', line 208

def ignore_disconnect
  @ignore_disconnect
end

Instance Method Details

#abort ⇒ Object

Inform the producer/writing thread that the client has disconnected; the reading thread is no longer interested in anything that's being written.

See also #close.



260
261
262
263
264
265
# File 'lib/action_controller/metal/live.rb', line 260

def abort
  synchronize do
    @aborted = true
    @buf.clear
  end
end

#call_on_error ⇒ Object



279
280
281
# File 'lib/action_controller/metal/live.rb', line 279

def call_on_error
  @error_callback.call
end

#close ⇒ Object

Write a 'close' event to the buffer; the producer/writing thread uses this to notify us that it's finished supplying content.

See also #abort.



248
249
250
251
252
253
254
# File 'lib/action_controller/metal/live.rb', line 248

def close
  synchronize do
    super
    @buf.push nil
    @cv.broadcast
  end
end

#connected? ⇒ Boolean

Is the client still connected and waiting for content?

The result of calling write when this is false is determined by ignore_disconnect.

Returns:

  • (Boolean)


271
272
273
# File 'lib/action_controller/metal/live.rb', line 271

def connected?
  !@aborted
end

#on_error(&block) ⇒ Object



275
276
277
# File 'lib/action_controller/metal/live.rb', line 275

def on_error(&block)
  @error_callback = block
end

#write(string) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/action_controller/metal/live.rb', line 218

def write(string)
  unless @response.committed?
    @response.headers["Cache-Control"] ||= "no-cache"
    @response.delete_header "Content-Length"
  end

  super

  unless connected?
    @buf.clear

    unless @ignore_disconnect
      # Raise ClientDisconnected, which is a RuntimeError (not an IOError), because
      # that's more appropriate for something beyond the developer's control.
      raise ClientDisconnected, "client disconnected"
    end
  end

  string.bytesize
end

#writeln(string) ⇒ Object

Same as write but automatically include a newline at the end of the string.



240
241
242
# File 'lib/action_controller/metal/live.rb', line 240

def writeln(string)
  write string.end_with?("\n") ? string : "#{string}\n"
end