Module: Aerospike::Socket::Base

Included in:
SSL, TCP
Defined in:
lib/aerospike/socket/base.rb

Instance Method Summary collapse

Instance Method Details

#alive?Boolean

Returns whether the connection to the server is alive.

It is useful to call this method before making a call to the server that would change data on the server.

Note: This method is only useful if the server closed the connection or if a previous connection failure occurred. If the server is hard killed this will still return true until one or more writes are attempted.

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aerospike/socket/base.rb', line 72

def alive?
  return false if closed?

  if IO.select([self], nil, nil, 0)
    !eof? rescue false
  else
    true
  end
rescue IOError
  false
end

#closeObject



84
85
86
87
# File 'lib/aerospike/socket/base.rb', line 84

def close
  return if closed?
  super()
end

#connected?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/aerospike/socket/base.rb', line 60

def connected?
  !closed?
end

#initialize(*args) ⇒ Object



23
24
25
26
# File 'lib/aerospike/socket/base.rb', line 23

def initialize(*args)
  super(*args)
  @timeout = nil
end

#read(buffer, length) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/aerospike/socket/base.rb', line 28

def read(buffer, length)
  bytes_read = 0
  until bytes_read >= length
    result = read_from_socket(length - bytes_read)
    buffer.write_binary(result, bytes_read)
    bytes_read += result.bytesize
  end
end

#read_from_socket(length) ⇒ Object



37
38
39
40
41
# File 'lib/aerospike/socket/base.rb', line 37

def read_from_socket(length)
  with_timeout(@timeout) do
    read_nonblock(length)
  end
end

#timeout=(timeout) ⇒ Object



56
57
58
# File 'lib/aerospike/socket/base.rb', line 56

def timeout=(timeout)
  @timeout = timeout && timeout > 0 ? timeout : nil
end

#write(buffer, length) ⇒ Object



43
44
45
46
47
48
# File 'lib/aerospike/socket/base.rb', line 43

def write(buffer, length)
  bytes_written = 0
  until bytes_written >= length
    bytes_written += write_to_socket(buffer.read(bytes_written, length - bytes_written))
  end
end

#write_to_socket(data) ⇒ Object



50
51
52
53
54
# File 'lib/aerospike/socket/base.rb', line 50

def write_to_socket(data)
  with_timeout(@timeout) do
    write_nonblock(data)
  end
end