Method: Async::IO::Stream#flush

Defined in:
lib/async/io/stream.rb

#flushObject

Flushes buffered data to the stream.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/async/io/stream.rb', line 152

def flush
  return if @write_buffer.empty?
  
  @writing.acquire do
    # Flip the write buffer and drain buffer:
    @write_buffer, @drain_buffer = @drain_buffer, @write_buffer
    
    begin
      @io.write(@drain_buffer)
    ensure
      # If the write operation fails, we still need to clear this buffer, and the data is essentially lost.
      @drain_buffer.clear
    end
  end
end