Class: Multidb::Balancer

Inherits:
Object
  • Object
show all
Defined in:
lib/multidb/balancer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Balancer

Returns a new instance of Balancer.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/multidb/balancer.rb', line 32

def initialize(configuration)
  @candidates = {}.with_indifferent_access
  @configuration = configuration
  if @configuration
    (@configuration.raw_configuration[:databases] || {}).each_pair do |name, config|
      configs = config.is_a?(Array) ? config : [config]
      configs.each do |config|          
        candidate = Candidate.new(@configuration.default_adapter.merge(config))
        @candidates[name] ||= []
        @candidates[name].push(candidate)
      end
    end
    if @configuration.raw_configuration.include?(:fallback)
      @fallback = @configuration.raw_configuration[:fallback]
    elsif defined?(Rails)
      @fallback = %w(development test).include?(Rails.env)
    else
      @fallback = false
    end
    @default_candidate = Candidate.new(@configuration.default_pool)
    unless @candidates.include?(:default)
      @candidates[:default] = [@default_candidate]
    end
  end
end

Class Method Details

.current_connectionObject



103
104
105
# File 'lib/multidb/balancer.rb', line 103

def current_connection
  Multidb.balancer.current_connection
end

.disconnect!Object



107
108
109
# File 'lib/multidb/balancer.rb', line 107

def disconnect!
  Multidb.balancer.disconnect!
end

.use(name, &block) ⇒ Object



99
100
101
# File 'lib/multidb/balancer.rb', line 99

def use(name, &block)
  Multidb.balancer.use(name, &block)
end

Instance Method Details

#current_connectionObject



94
95
96
# File 'lib/multidb/balancer.rb', line 94

def current_connection
  Thread.current[:multidb_connection] || @default_candidate.connection
end

#disconnect!Object



58
59
60
61
62
# File 'lib/multidb/balancer.rb', line 58

def disconnect!
  @candidates.values.each do |candidate|
    candidate.connection_pool.disconnect!
  end
end

#get(name, &block) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
# File 'lib/multidb/balancer.rb', line 64

def get(name, &block)
  candidates = @candidates[name]
  candidates ||= @fallback ? @candidates[:default] : []
  raise ArgumentError, "No such database connection '#{name}'" if candidates.empty?
  candidate = candidates.respond_to?(:sample) ? 
    candidates.sample : candidates[rand(candidates.length)]
  block_given? ? yield(candidate) : candidate
end

#use(name, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/multidb/balancer.rb', line 73

def use(name, &block)
  result = nil
  get(name) do |candidate|
    if block_given?
      candidate.connection do |connection|
        previous_connection, Thread.current[:multidb_connection] = 
          Thread.current[:multidb_connection], connection
        begin
          result = yield
        ensure
          Thread.current[:multidb_connection] = previous_connection
        end
        result
      end
    else
      result = Thread.current[:multidb_connection] = candidate.connection
    end
  end
  result
end