Class: EventStore::HTTP::Connect

Inherits:
Object
  • Object
show all
Includes:
Log::Dependency
Defined in:
lib/event_store/http/connect/log.rb,
lib/event_store/http/connect/connect.rb,
lib/event_store/http/connect/defaults.rb,
lib/event_store/http/connect/net_http.rb,
lib/event_store/http/connect/settings.rb,
lib/event_store/http/connect/telemetry.rb,
lib/event_store/http/connect/assertions.rb,
lib/event_store/http/connect/controls/host.rb,
lib/event_store/http/connect/controls/port.rb,
lib/event_store/http/connect/log_attributes.rb,
lib/event_store/http/connect/controls/cluster.rb,
lib/event_store/http/connect/controls/hostname.rb,
lib/event_store/http/connect/controls/net_http.rb,
lib/event_store/http/connect/controls/settings.rb,
lib/event_store/http/connect/controls/timeouts.rb,
lib/event_store/http/connect/controls/ip_address.rb,
lib/event_store/http/connect/controls/cluster/size.rb,
lib/event_store/http/connect/controls/resolve_host.rb,
lib/event_store/http/connect/controls/cluster/hostname.rb,
lib/event_store/http/connect/controls/cluster/ip_address.rb,
lib/event_store/http/connect/controls/ip_address/loopback.rb,
lib/event_store/http/connect/controls/cluster/resolve_host.rb,
lib/event_store/http/connect/controls/ip_address/unavailable.rb,
lib/event_store/http/connect/controls/ip_address/non_routable.rb,
lib/event_store/http/connect/controls/ip_address/loopback/alias.rb

Defined Under Namespace

Modules: Assertions, Controls, Defaults, LogAttributes, NetHTTP, Telemetry Classes: Log, Settings

Constant Summary collapse

ConnectionError =
Class.new StandardError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(settings = nil, namespace: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/event_store/http/connect/connect.rb', line 17

def self.build(settings=nil, namespace: nil)
  settings ||= Settings.build
  namespace = Array(namespace)

  instance = new
  DNS::ResolveHost.configure instance
  ::Telemetry.configure instance
  settings.set instance, *namespace
  instance
end

.call(settings = nil, namespace: nil, host: nil, &block) ⇒ Object



28
29
30
31
# File 'lib/event_store/http/connect/connect.rb', line 28

def self.call(settings=nil, namespace: nil, host: nil, &block)
  instance = build settings, namespace: namespace
  instance.(host, &block)
end

.configure(receiver, settings = nil, namespace: nil, connect: nil, attr_name: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/event_store/http/connect/connect.rb', line 33

def self.configure(receiver, settings=nil, namespace: nil, connect: nil, attr_name: nil)
  attr_name ||= :connect

  if connect.nil?
    instance = build settings, namespace: namespace
  else
    instance = connect
  end

  receiver.public_send "#{attr_name}=", instance

  instance
end

.register_telemetry_sink(instance) ⇒ Object



47
48
49
50
51
# File 'lib/event_store/http/connect/connect.rb', line 47

def self.register_telemetry_sink(instance)
  sink = Telemetry::Sink.new
  instance.telemetry.register sink
  sink
end

Instance Method Details

#call(host = nil, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/event_store/http/connect/connect.rb', line 53

def call(host=nil, &block)
  log_attributes = LogAttributes.get self, host: host

  host ||= self.host

  logger.trace { "Establishing HTTP connection to EventStore (#{log_attributes})" }

  begin
    ip_addresses = resolve_ip_address host
  rescue DNS::ResolveHost::ResolutionError => error
    error_message = "Could not connect to EventStore (#{log_attributes}, Error: #{error.class})"
    logger.error error_message

    record_host_resolution_failed host, error

    raise ConnectionError, error
  end

  record_host_resolved host, ip_addresses

  net_http = nil

  ip_addresses.each_with_index do |ip_address, index|
    net_http = new_connection ip_address

    begin
      net_http.start

      record_connection_established host, ip_address, net_http

      break
    rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, SocketError => error
      record_connection_attempt_failed host, ip_address, error

      error_message = "Could not connect to EventStore (#{log_attributes}, IPAddress: #{ip_address} (#{index.next} of #{ip_addresses.count}), Error: #{error.class})"

      if index + 1 == ip_addresses.count
        logger.error error_message
        raise ConnectionError, error_message
      else
        logger.warn error_message
        net_http = nil
      end
    end
  end

  if block.nil?
    return_value = net_http
  else
    begin
      return_value = block.(net_http)
    ensure
      net_http.finish
    end
  end

  logger.info { "HTTP connection to EventStore established (#{log_attributes}, IPAddress: #{net_http.address})" }

  return_value
end

#new_connection(ip_address) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/event_store/http/connect/connect.rb', line 158

def new_connection(ip_address)
  net_http = Net::HTTP.new ip_address, port

  net_http.continue_timeout = continue_timeout unless continue_timeout.nil?
  net_http.keep_alive_timeout = keep_alive_timeout unless keep_alive_timeout.nil?
  net_http.open_timeout = open_timeout unless open_timeout.nil?
  net_http.read_timeout = read_timeout unless read_timeout.nil?

  net_http
end

#portObject



169
170
171
# File 'lib/event_store/http/connect/connect.rb', line 169

def port
  @port ||= Defaults.port
end

#record_connection_attempt_failed(host, ip_address, error) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/event_store/http/connect/connect.rb', line 134

def record_connection_attempt_failed(host, ip_address, error)
  record = Telemetry::ConnectionAttemptFailed.new host, ip_address, port, error

  telemetry.record :connection_attempt_failed, record

  record
end

#record_connection_established(host, ip_address, connection) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/event_store/http/connect/connect.rb', line 126

def record_connection_established(host, ip_address, connection)
  record = Telemetry::ConnectionEstablished.new host, ip_address, port, connection

  telemetry.record :connection_established, record

  record
end

#record_host_resolution_failed(host, error) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/event_store/http/connect/connect.rb', line 150

def record_host_resolution_failed(host, error)
  record = Telemetry::HostResolutionFailed.new host, error

  telemetry.record :host_resolution_failed, record

  record
end

#record_host_resolved(host, ip_addresses) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/event_store/http/connect/connect.rb', line 142

def record_host_resolved(host, ip_addresses)
  record = Telemetry::HostResolved.new host, ip_addresses

  telemetry.record :host_resolved, record

  record
end

#resolve_ip_address(host) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/event_store/http/connect/connect.rb', line 114

def resolve_ip_address(host)
  logger.trace { "Resolving IP address from host (Host: #{host})" }

  ip_addresses = resolve_host.(host) do |dns_resolver|
    dns_resolver.timeouts = open_timeout
  end

  logger.debug { "Resolved IP address from host (Host: #{host}, IPAddresses: #{ip_addresses.inspect})" }

  ip_addresses
end