Module: EventStore::HTTP::Connect::Leader

Defined in:
lib/event_store/http/connect/leader.rb

Constant Summary collapse

LeaderUnavailableError =
Class.new StandardError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



5
6
7
8
9
# File 'lib/event_store/http/connect/leader.rb', line 5

def self.build(settings=nil, namespace: nil)
  instance = Connect.build settings, namespace: namespace
  instance.extend self
  instance
end

Instance Method Details

#connect(net_http = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/event_store/http/connect/leader.rb', line 11

def connect(net_http=nil)
  net_http ||= super()

  member_info = Info.(connection: net_http)

  return net_http if member_info.leader?

  cluster_status = Gossip.(connection: net_http)

  if cluster_status.leader.nil?
    error_message = "Leader is unavailable (Host: #{host}, Port: #{port})"
    logger.error { error_message }
    raise LeaderUnavailableError, error_message
  end

  leader_ip_address = cluster_status.leader.external_http_ip

  raw leader_ip_address
end