Class: Mongo::Socket Private

Inherits:
Object
  • Object
show all
Includes:
Socket::Constants
Defined in:
build/ruby-driver-master/lib/mongo/socket.rb,
build/ruby-driver-master/lib/mongo/socket/ssl.rb,
build/ruby-driver-master/lib/mongo/socket/tcp.rb,
build/ruby-driver-master/lib/mongo/socket/unix.rb,
build/ruby-driver-master/lib/mongo/socket/ocsp_cache.rb,
build/ruby-driver-master/lib/mongo/socket/ocsp_verifier.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides additional data around sockets for the driver's use.

Since:

  • 2.0.0

Direct Known Subclasses

SSL, TCP, Unix

Defined Under Namespace

Modules: OcspCache Classes: OcspVerifier, SSL, TCP, Unix

Constant Summary collapse

SSL_ERROR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Deprecated.

Error message for TLS related exceptions.

Since:

  • 2.0.0

'MongoDB may not be configured with TLS support'.freeze
TIMEOUT_ERROR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Deprecated.

Error message for timeouts on socket calls.

Since:

  • 2.0.0

'Socket request timed out'.freeze
TIMEOUT_PACK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The pack directive for timeouts.

Since:

  • 2.0.0

'l_2'.freeze
WRITE_CHUNK_SIZE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Write data to the socket in chunks of this size.

Since:

  • 2.0.0

65536

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout, options) ⇒ Socket

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes common socket attributes.

Parameters:

  • timeout (Float)

    The socket timeout value.

  • options (Hash)

    The options.

Options Hash (options):

  • :connect_timeout (Float)

    Connect timeout.

  • :connection_address (Address)

    Address of the connection that created this socket.

  • :connection_generation (Integer)

    Generation of the connection (for non-monitoring connections) that created this socket.

  • :monitor (true | false)

    Whether this socket was created by a monitoring connection.

Since:

  • 2.0.0



69
70
71
72
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 69

def initialize(timeout, options)
  @timeout = timeout
  @options = options
end

Instance Attribute Details

#familyInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns family The type of host family.

Returns:

  • (Integer)

    family The type of host family.

Since:

  • 2.0.0



75
76
77
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 75

def family
  @family
end

#optionsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The options.

Returns:

  • (Hash)

    The options.

Since:

  • 2.0.0



81
82
83
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 81

def options
  @options
end

#socketSocket (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns socket The wrapped socket.

Returns:

  • (Socket)

    socket The wrapped socket.

Since:

  • 2.0.0



78
79
80
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 78

def socket
  @socket
end

#timeoutFloat (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns timeout The socket timeout.

Returns:

  • (Float)

    timeout The socket timeout.

Since:

  • 2.0.0



84
85
86
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 84

def timeout
  @timeout
end

Instance Method Details

#alive?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deprecated.

Use #connectable? on the connection instead.

Is the socket connection alive?

Examples:

Is the socket alive?

socket.alive?

Returns:

  • (true, false)

    If the socket is alive.

Since:

  • 2.0.0



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 134

def alive?
  sock_arr = [ @socket ]
  if Kernel::select(sock_arr, nil, sock_arr, 0)
    # The eof? call is supposed to return immediately since select
    # indicated the socket is readable. However, if @socket is a TLS
    # socket, eof? can block anyway - see RUBY-2140.
    begin
      Timeout.timeout(0.1) do
        eof?
      end
    rescue ::Timeout::Error
      true
    end
  else
    true
  end
end

#closetrue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Close the socket.

Examples:

Close the socket.

socket.close

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0



160
161
162
163
164
165
166
167
168
169
170
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 160

def close
  begin
    # Sometimes it seems the close call can hang for a long time
    ::Timeout.timeout(5) do
      @socket.close
    end
  rescue
    # Silence all errors
  end
  true
end

#connectable?true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deprecated.

For backwards compatibilty only, do not use.

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0



262
263
264
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 262

def connectable?
  true
end

#connection_addressAddress

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Address of the connection that created this socket.

Returns:

  • (Address)

    Address of the connection that created this socket.

Since:

  • 2.0.0



89
90
91
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 89

def connection_address
  options[:connection_address]
end

#connection_generationInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Generation of the connection (for non-monitoring connections) that created this socket.

Returns:

  • (Integer)

    Generation of the connection (for non-monitoring connections) that created this socket.

Since:

  • 2.0.0



97
98
99
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 97

def connection_generation
  options[:connection_generation]
end

#eof?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Tests if this socket has reached EOF. Primarily used for liveness checks.

Returns:

  • (Boolean)

Since:

  • 2.0.5



251
252
253
254
255
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 251

def eof?
  @socket.eof?
rescue IOError, SystemCallError
  true
end

#gets(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delegates gets to the underlying socket.

Examples:

Get the next line.

socket.gets(10)

Parameters:

  • args (Array<Object>)

    The arguments to pass through.

Returns:

  • (Object)

    The returned bytes.

Since:

  • 2.0.0



182
183
184
185
186
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 182

def gets(*args)
  map_exceptions do
    @socket.gets(*args)
  end
end

#monitor?true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Whether this socket was created by a monitoring connection.

Returns:

  • (true | false)

    Whether this socket was created by a monitoring connection.

Since:

  • 2.0.0



105
106
107
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 105

def monitor?
  !!options[:monitor]
end

#read(length, timeout: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Will read all data from the socket for the provided number of bytes. If no data is returned, an exception will be raised.

Examples:

Read all the requested data from the socket.

socket.read(4096)

Parameters:

  • length (Integer)

    The number of bytes to read.

  • timeout (Numeric) (defaults to: nil)

    The timeout to use for each chunk read.

Returns:

  • (Object)

    The data from the socket.

Raises:

  • (Mongo::SocketError)

    If not all data is returned.

Since:

  • 2.0.0



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 202

def read(length, timeout: nil)
  map_exceptions do
    data = read_from_socket(length, timeout: timeout)
    unless (data.length > 0 || length == 0)
      raise IOError, "Expected to read > 0 bytes but read 0 bytes"
    end
    while data.length < length
      chunk = read_from_socket(length - data.length, timeout: timeout)
      unless (chunk.length > 0 || length == 0)
        raise IOError, "Expected to read > 0 bytes but read 0 bytes"
      end
      data << chunk
    end
    data
  end
end

#readbyteObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Read a single byte from the socket.

Examples:

Read a single byte.

socket.readbyte

Returns:

  • (Object)

    The read byte.

Since:

  • 2.0.0



227
228
229
230
231
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 227

def readbyte
  map_exceptions do
    @socket.readbyte
  end
end

#summaryString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Human-readable summary of the socket for debugging.

Returns:

  • (String)

    Human-readable summary of the socket for debugging.

Since:

  • 2.0.0



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 112

def summary
  fileno = @socket&.fileno rescue '<no socket>' || '<no socket>'
  if monitor?
    indicator = if options[:push]
      'pm'
    else
      'm'
    end
    "#{connection_address};#{indicator};fd=#{fileno}"
  else
    "#{connection_address};c:#{connection_generation};fd=#{fileno}"
  end
end

#write(*args) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes data to the socket instance.

Parameters:

  • args (Array<Object>)

    The data to be written.

Returns:

  • (Integer)

    The length of bytes written to the socket.

Raises:

Since:

  • 2.0.0



242
243
244
245
246
# File 'build/ruby-driver-master/lib/mongo/socket.rb', line 242

def write(*args)
  map_exceptions do
    do_write(*args)
  end
end