Class: Gitlab::Database::LoadBalancing::HostList

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/load_balancing/host_list.rb

Overview

A list of database hosts to use for connections.

Instance Method Summary collapse

Constructor Details

#initialize(hosts = []) ⇒ HostList

hosts - The list of secondary hosts to add.



9
10
11
12
13
14
15
16
# File 'lib/gitlab/database/load_balancing/host_list.rb', line 9

def initialize(hosts = [])
  @hosts = hosts.shuffle
  @index = 0
  @mutex = Mutex.new
  @hosts_gauge = Gitlab::Metrics.gauge(:db_load_balancing_hosts, 'Current number of load balancing hosts')

  set_metrics!
end

Instance Method Details

#host_names_and_portsObject



32
33
34
# File 'lib/gitlab/database/load_balancing/host_list.rb', line 32

def host_names_and_ports
  @mutex.synchronize { @hosts.map { |host| [host.host, host.port] } }
end

#hostsObject



18
19
20
# File 'lib/gitlab/database/load_balancing/host_list.rb', line 18

def hosts
  @mutex.synchronize { @hosts.dup }
end

#hosts=(hosts) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/gitlab/database/load_balancing/host_list.rb', line 36

def hosts=(hosts)
  @mutex.synchronize do
    @hosts = hosts
    unsafe_shuffle
  end

  set_metrics!
end

#lengthObject



28
29
30
# File 'lib/gitlab/database/load_balancing/host_list.rb', line 28

def length
  @mutex.synchronize { @hosts.length }
end

#nextObject

Sets metrics before returning next host



46
47
48
49
50
# File 'lib/gitlab/database/load_balancing/host_list.rb', line 46

def next
  next_host.tap do |_|
    set_metrics!
  end
end

#shuffleObject



22
23
24
25
26
# File 'lib/gitlab/database/load_balancing/host_list.rb', line 22

def shuffle
  @mutex.synchronize do
    unsafe_shuffle
  end
end