Sha256: f2caf0131ca11bee319fd4ef879c5c89f91bd8fdfca1cfaf4fcfc2857d17da3b

Contents?: true

Size: 1.08 KB

Versions: 27

Compression:

Stored size: 1.08 KB

Contents

require 'timeout'
module RunLoop
  module Fifo
    BUFFER_SIZE = 4096

    class NoReaderConfiguredError < RuntimeError
    end

    class WriteTimedOut < RuntimeError
    end

    def self.write(pipe, msg, options={})
      msg = "#{msg}\n"
      timeout = options[:timeout] || 10
      begin_at = Time.now
      begin
        open(pipe, File::WRONLY | File::NONBLOCK) do |pipe_io|
          bytes_written = 0
          bytes_to_write = msg.length
          until bytes_written >= bytes_to_write do
            begin
              wrote = pipe_io.write_nonblock msg
              bytes_written += wrote
              msg = msg[wrote..-1]
            rescue IO::WaitWritable, Errno::EINTR
              timeout_left = timeout - (Time.now - begin_at)
              raise WriteTimedOut if timeout_left <= 0
              IO.select nil, [pipe_io], nil, timeout_left
            end
          end
        end
      rescue Errno::ENXIO
        sleep(0.5)
        timeout_left = timeout - (Time.now - begin_at)
        raise NoReaderConfiguredError if timeout_left <= 0
        retry
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
run_loop-2.0.9 lib/run_loop/fifo.rb
run_loop-2.0.8 lib/run_loop/fifo.rb
run_loop-2.0.7 lib/run_loop/fifo.rb
run_loop-2.0.6 lib/run_loop/fifo.rb
run_loop-2.0.5 lib/run_loop/fifo.rb
run_loop-2.0.4 lib/run_loop/fifo.rb
run_loop-2.0.3 lib/run_loop/fifo.rb
run_loop-2.0.2 lib/run_loop/fifo.rb
run_loop-2.0.1 lib/run_loop/fifo.rb
run_loop-2.0.0 lib/run_loop/fifo.rb
run_loop-1.5.6 lib/run_loop/fifo.rb
run_loop-1.5.6.pre1 lib/run_loop/fifo.rb
run_loop-1.5.5 lib/run_loop/fifo.rb
run_loop-1.5.4 lib/run_loop/fifo.rb
run_loop-1.5.3 lib/run_loop/fifo.rb
run_loop-1.5.2 lib/run_loop/fifo.rb
run_loop-1.5.1 lib/run_loop/fifo.rb
run_loop-1.5.0 lib/run_loop/fifo.rb
run_loop-1.4.1 lib/run_loop/fifo.rb
run_loop-1.4.0 lib/run_loop/fifo.rb