Class: Makara::Pool
- Inherits:
-
Object
- Object
- Makara::Pool
- Defined in:
- lib/makara/pool.rb
Instance Attribute Summary collapse
-
#blacklist_errors ⇒ Object
readonly
Returns the value of attribute blacklist_errors.
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#default_shard ⇒ Object
readonly
Returns the value of attribute default_shard.
-
#disabled ⇒ Object
there are cases when we understand the pool is busted and we essentially want to skip all execution.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#shard_strategy_class ⇒ Object
readonly
Returns the value of attribute shard_strategy_class.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
Instance Method Summary collapse
-
#add(config) ⇒ Object
Add a connection to this pool, wrapping the connection with a Makara::ConnectionWrapper.
- #completely_blacklisted? ⇒ Boolean
-
#initialize(role, proxy) ⇒ Pool
constructor
A new instance of Pool.
-
#provide ⇒ Object
Provide a connection that is not blacklisted and connected.
-
#send_to_all(method, *args, &block) ⇒ Object
send this method to all available nodes send nil to just yield with each con if there is block.
Constructor Details
#initialize(role, proxy) ⇒ Pool
Returns a new instance of Pool.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/makara/pool.rb', line 19 def initialize(role, proxy) @role = role @proxy = proxy @connections = [] @blacklist_errors = [] @disabled = false if proxy.shard_aware_for(role) @strategy = Makara::Strategies::ShardAware.new(self) @shard_strategy_class = proxy.strategy_class_for(proxy.strategy_name_for(role)) @default_shard = proxy.default_shard_for(role) else @strategy = proxy.strategy_for(role) end end |
Instance Attribute Details
#blacklist_errors ⇒ Object (readonly)
Returns the value of attribute blacklist_errors.
12 13 14 |
# File 'lib/makara/pool.rb', line 12 def blacklist_errors @blacklist_errors end |
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
14 15 16 |
# File 'lib/makara/pool.rb', line 14 def connections @connections end |
#default_shard ⇒ Object (readonly)
Returns the value of attribute default_shard.
17 18 19 |
# File 'lib/makara/pool.rb', line 17 def default_shard @default_shard end |
#disabled ⇒ Object
there are cases when we understand the pool is busted and we essentially want to skip all execution
11 12 13 |
# File 'lib/makara/pool.rb', line 11 def disabled @disabled end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
13 14 15 |
# File 'lib/makara/pool.rb', line 13 def role @role end |
#shard_strategy_class ⇒ Object (readonly)
Returns the value of attribute shard_strategy_class.
16 17 18 |
# File 'lib/makara/pool.rb', line 16 def shard_strategy_class @shard_strategy_class end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
15 16 17 |
# File 'lib/makara/pool.rb', line 15 def strategy @strategy end |
Instance Method Details
#add(config) ⇒ Object
Add a connection to this pool, wrapping the connection with a Makara::ConnectionWrapper
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/makara/pool.rb', line 42 def add(config) config[:name] ||= "#{@role}/#{@connections.length + 1}" connection = yield # already wrapped because of initial error if connection.is_a?(Makara::ConnectionWrapper) connection.config = config # to add :name wrapper = connection else wrapper = Makara::ConnectionWrapper.new(@proxy, connection, config) end @connections << wrapper @strategy.connection_added(wrapper) wrapper end |
#completely_blacklisted? ⇒ Boolean
34 35 36 37 38 39 |
# File 'lib/makara/pool.rb', line 34 def completely_blacklisted? @connections.each do |connection| return false unless connection._makara_blacklisted? end true end |
#provide ⇒ Object
Provide a connection that is not blacklisted and connected. Handle any errors that may occur within the block.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/makara/pool.rb', line 100 def provide attempt = 0 begin provided_connection = self.next # nil implies that it's blacklisted if provided_connection value = @proxy.error_handler.handle(provided_connection) do yield provided_connection end @blacklist_errors = [] value # if we've made any connections within this pool, we should report the blackout. elsif connection_made? err = Makara::Errors::AllConnectionsBlacklisted.new(self, @blacklist_errors) @blacklist_errors = [] raise err else raise Makara::Errors::NoConnectionsAvailable.new(@role) unless @disabled end # when a connection causes a blacklist error within the provided block, we blacklist it then retry rescue Makara::Errors::BlacklistConnection => e @blacklist_errors.insert(0, e) in_transaction = self.role == "master" && provided_connection._makara_in_transaction? provided_connection._makara_blacklist! raise Makara::Errors::BlacklistedWhileInTransaction.new(@role) if in_transaction attempt += 1 if attempt < @connections.length retry elsif connection_made? err = Makara::Errors::AllConnectionsBlacklisted.new(self, @blacklist_errors) @blacklist_errors = [] raise err else raise Makara::Errors::NoConnectionsAvailable.new(@role) unless @disabled end end end |
#send_to_all(method, *args, &block) ⇒ Object
send this method to all available nodes send nil to just yield with each con if there is block
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 |
# File 'lib/makara/pool.rb', line 63 def send_to_all(method, *args, &block) ret = nil one_worked = false # actually found one that worked errors = [] @connections.each do |con| next if con._makara_blacklisted? begin ret = @proxy.error_handler.handle(con) do if block yield con else con.send(method, *args) end end one_worked = true rescue Makara::Errors::BlacklistConnection => e errors.insert(0, e) con._makara_blacklist! end end if !one_worked if connection_made? raise Makara::Errors::AllConnectionsBlacklisted.new(self, errors) else raise Makara::Errors::NoConnectionsAvailable.new(@role) unless @disabled end end ret end |