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)


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

def alive?
  return false if closed?

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

#closeObject



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

def close
  return if closed?
  super()
end

#connected?Boolean

Returns:

  • (Boolean)


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

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, offset = 0) ⇒ Object



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

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

#read_from_socket(length) ⇒ Object



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

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

#timeout=(timeout) ⇒ Object



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

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

#write(buffer, length) ⇒ Object



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

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



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

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