Class: Dalli::Elasticache::AutoDiscovery::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/dalli/elasticache/auto_discovery/base_command.rb

Overview

Base command class for configuration endpoint command. Contains the network logic.

Direct Known Subclasses

ConfigCommand, StatsCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, timeout = nil, ssl_context: nil) ⇒ BaseCommand

Returns a new instance of BaseCommand.



16
17
18
19
20
21
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 16

def initialize(host, port, timeout = nil, ssl_context: nil)
  @host = host
  @port = port
  @timeout = timeout
  @ssl_context = ssl_context
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 14

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



14
15
16
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 14

def port
  @port
end

#ssl_contextObject (readonly)

Returns the value of attribute ssl_context.



14
15
16
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 14

def ssl_context
  @ssl_context
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



14
15
16
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 14

def timeout
  @timeout
end

Instance Method Details

#response_from_socket(socket) ⇒ Object

timeout bounds the entire multi-line read as a single wall-clock deadline, not each individual readline. A per-line readiness check (e.g. wait_readable) can't tell buffered-but-unread bytes apart from an empty socket once a buffering layer (Ruby's IO buffer, or OpenSSL::Buffering for TLS) sits between the fd and readline, so it can't be used to gate individual lines reliably.



42
43
44
45
46
47
48
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 42

def response_from_socket(socket)
  return read_lines(socket) unless timeout

  Timeout.timeout(timeout, Timeout::Error, "Auto discovery read timed out after #{timeout}s") do
    read_lines(socket)
  end
end

#send_commandObject

Send an ASCII command to the endpoint

Returns the raw response as a String



26
27
28
29
30
31
32
33
34
35
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 26

def send_command
  tcp_socket = ::Socket.tcp(@host, @port, connect_timeout: timeout)
  begin
    socket = ssl_context ? wrap_with_ssl(tcp_socket) : tcp_socket
    socket.puts command
    response_from_socket(socket)
  ensure
    (socket || tcp_socket).close
  end
end