Class: Kontena::Websocket::Client::Connection

Inherits:
Object
  • Object
show all
Includes:
Waitable, Waitable_Ruby2_2, Logging
Defined in:
lib/kontena/websocket/client/connection.rb

Overview

WebSocket::Driver.client(…) API

Defined Under Namespace

Modules: Waitable, Waitable_Ruby2_2

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Waitable_Ruby2_2

#wait_socket_readable!, #wait_socket_writable!

Methods included from Waitable

#wait_socket_readable!, #wait_socket_writable!

Methods included from Logging

#debug, #error, #info, initialize_logger, #logger, logger, logger=, #logging_prefix, #warn

Constructor Details

#initialize(uri, socket, write_timeout: nil) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • uri (URI)
  • socket (TCPSocket, OpenSSL::SSL::SSLSocket)
  • write_timeout (Float) (defaults to: nil)

    per each write syscall



69
70
71
72
73
# 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

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



64
65
66
# File 'lib/kontena/websocket/client/connection.rb', line 64

def uri
  @uri
end

Instance Method Details

#nonblocking_timeout(timeout = nil, &block) ⇒ Object

Wait up to timeout before retrying any blocking operation.

Parameters:

  • timeout (Float) (defaults to: nil)

    default (nil) blocks indefinitely

Raises:



84
85
86
87
88
89
90
91
92
# 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) ⇒ String

Returns 0..size bytes.

Parameters:

  • size (Integer)
  • timeout (Float) (defaults to: nil)

Returns:

  • (String)

    0..size bytes

Raises:



98
99
100
101
102
103
104
105
106
# 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

#urlString

Returns:

  • (String)


76
77
78
# File 'lib/kontena/websocket/client/connection.rb', line 76

def url
  @uri.to_s
end

#write(buf) ⇒ Object

Parameters:

  • buf (String)


109
110
111
112
113
114
115
116
117
118
# 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