class Kontena::Websocket::Client::Connection

WebSocket::Driver.client(…) API

Attributes

uri[R]

Public Class Methods

new(uri, socket, write_timeout: nil) click to toggle source

@param uri [URI] @param socket [TCPSocket, OpenSSL::SSL::SSLSocket] @param write_timeout [Float] per each write syscall

# File lib/kontena/websocket/client/connection.rb, line 69
def initialize(uri, socket, write_timeout: nil)
  @uri = uri
  @socket = socket
  @write_timeout = write_timeout
end

Public Instance Methods

nonblocking_timeout(timeout = nil) { || ... } click to toggle source

Wait up to timeout before retrying any blocking operation.

@param timeout [Float] default (nil) blocks indefinitely @raise [Kontena::Websocket::TimeoutError]

# File lib/kontena/websocket/client/connection.rb, line 84
def nonblocking_timeout(timeout = nil, &block)
  return yield
rescue IO::WaitReadable
  wait_socket_readable!(@socket, timeout) # raises Kontena::Websocket::TimeoutError
  retry
rescue IO::WaitWritable
  wait_socket_writable!(@socket, timeout) # raises Kontena::Websocket::TimeoutError
  retry
end
read(size, timeout: nil) click to toggle source

@param size [Integer] @param timeout [Float] @raise [EOFError] @return [String] 0..size bytes

# File lib/kontena/websocket/client/connection.rb, line 98
def read(size, timeout: nil)
  buf = nonblocking_timeout(timeout) do
    @socket.read_nonblock(size)
  end

  debug "read size=#{size}: #buf=#{buf.size}"

  return buf
end
url() click to toggle source

@return [String]

# File lib/kontena/websocket/client/connection.rb, line 76
def url
  @uri.to_s
end
write(buf) click to toggle source

@param buf [String]

# File lib/kontena/websocket/client/connection.rb, line 109
def write(buf)
  until buf.empty?
    # it can take more than the timeout to write out the entire buffer
    size = nonblocking_timeout(@write_timeout) do
      @socket.write_nonblock(buf)
    end
    debug "write #buf=#{buf.size}: size=#{size}"
    buf = buf[size..-1]
  end
end