Class: ActionController::Live::Buffer

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ActionDispatch::Response::Buffer

#closed?

Constructor Details

#initialize(response) ⇒ Buffer

Returns a new instance of Buffer.



124
125
126
127
128
129
130
# File 'lib/action_controller/metal/live.rb', line 124

def initialize(response)
  @error_callback = lambda { true }
  @cv = new_cond
  @aborted = false
  @ignore_disconnect = false
  super(response, SizedQueue.new(10))
end

Instance Attribute Details

#ignore_disconnectObject

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.



122
123
124
# File 'lib/action_controller/metal/live.rb', line 122

def ignore_disconnect
  @ignore_disconnect
end

Instance Method Details

#abortObject

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.



177
178
179
180
181
182
# File 'lib/action_controller/metal/live.rb', line 177

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

#await_closeObject



192
193
194
195
196
# File 'lib/action_controller/metal/live.rb', line 192

def await_close
  synchronize do
    @cv.wait_until { @closed }
  end
end

#call_on_errorObject



202
203
204
# File 'lib/action_controller/metal/live.rb', line 202

def call_on_error
  @error_callback.call
end

#closeObject

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.



164
165
166
167
168
169
170
# File 'lib/action_controller/metal/live.rb', line 164

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)


188
189
190
# File 'lib/action_controller/metal/live.rb', line 188

def connected?
  !@aborted
end

#eachObject



152
153
154
155
156
157
158
# File 'lib/action_controller/metal/live.rb', line 152

def each
  @response.sending!
  while str = @buf.pop
    yield str
  end
  @response.sent!
end

#on_error(&block) ⇒ Object



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

def on_error(&block)
  @error_callback = block
end

#write(string) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/action_controller/metal/live.rb', line 132

def write(string)
  unless @response.committed?
    @response.headers["Cache-Control"] = "no-cache"
    @response.headers.delete "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
end