Class: Makara::Proxy
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Makara::Proxy
- Defined in:
- lib/makara/proxy.rb
Direct Known Subclasses
Constant Summary collapse
- METHOD_MISSING_SKIP =
[ :byebug, :puts ]
Instance Attribute Summary collapse
-
#config_parser ⇒ Object
readonly
Returns the value of attribute config_parser.
-
#control ⇒ Object
readonly
Returns the value of attribute control.
-
#error_handler ⇒ Object
readonly
Returns the value of attribute error_handler.
-
#sticky ⇒ Object
readonly
Returns the value of attribute sticky.
Class Method Summary collapse
- .control_method(*method_names) ⇒ Object
- .hijack_method(*method_names) ⇒ Object
- .send_to_all(*method_names) ⇒ Object
Instance Method Summary collapse
- #default_shard_for(role) ⇒ Object
- #disconnect! ⇒ Object
- #graceful_connection_for(config) ⇒ Object
- #hijacked? ⇒ Boolean
-
#initialize(config) ⇒ Proxy
constructor
A new instance of Proxy.
- #method_missing(m, *args, &block) ⇒ Object
- #shard_aware_for(role) ⇒ Object
-
#stick_to_master!(persist = true) ⇒ Object
If persist is true, we stick the proxy to master for subsequent requests up to master_ttl duration.
- #strategy_class_for(strategy_name) ⇒ Object
- #strategy_for(role) ⇒ Object
- #strategy_name_for(role) ⇒ Object
- #without_sticking ⇒ Object
Constructor Details
#initialize(config) ⇒ Proxy
Returns a new instance of Proxy.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/makara/proxy.rb', line 61 def initialize(config) @config = config.symbolize_keys @config_parser = Makara::ConfigParser.new(@config) @id = @config_parser.id @ttl = @config_parser.makara_config[:master_ttl] @sticky = @config_parser.makara_config[:sticky] @hijacked = false @error_handler ||= ::Makara::ErrorHandler.new @skip_sticking = false instantiate_connections super(config) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/makara/proxy.rb', line 119 def method_missing(m, *args, &block) if METHOD_MISSING_SKIP.include?(m) return super(m, *args, &block) end any_connection do |con| if con.respond_to?(m) con.public_send(m, *args, &block) elsif con.respond_to?(m, true) con.__send__(m, *args, &block) else super(m, *args, &block) end end end |
Instance Attribute Details
#config_parser ⇒ Object (readonly)
Returns the value of attribute config_parser.
58 59 60 |
# File 'lib/makara/proxy.rb', line 58 def config_parser @config_parser end |
#control ⇒ Object (readonly)
Returns the value of attribute control.
59 60 61 |
# File 'lib/makara/proxy.rb', line 59 def control @control end |
#error_handler ⇒ Object (readonly)
Returns the value of attribute error_handler.
56 57 58 |
# File 'lib/makara/proxy.rb', line 56 def error_handler @error_handler end |
#sticky ⇒ Object (readonly)
Returns the value of attribute sticky.
57 58 59 |
# File 'lib/makara/proxy.rb', line 57 def sticky @sticky end |
Class Method Details
.control_method(*method_names) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/makara/proxy.rb', line 43 def control_method(*method_names) self.control_methods = self.control_methods || [] self.control_methods |= method_names method_names.each do |method_name| define_method method_name do |*args, &block| control&.send(method_name, *args, &block) end end end |
.hijack_method(*method_names) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/makara/proxy.rb', line 22 def hijack_method(*method_names) self.hijack_methods = self.hijack_methods || [] self.hijack_methods |= method_names method_names.each do |method_name| define_method method_name do |*args, &block| appropriate_connection(method_name, args) do |con| con.send(method_name, *args, &block) end end end end |
.send_to_all(*method_names) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/makara/proxy.rb', line 35 def send_to_all(*method_names) method_names.each do |method_name| define_method method_name do |*args| send_to_all method_name, *args end end end |
Instance Method Details
#default_shard_for(role) ⇒ Object
104 105 106 |
# File 'lib/makara/proxy.rb', line 104 def default_shard_for(role) @config_parser.makara_config["#{role}_default_shard".to_sym] end |
#disconnect! ⇒ Object
154 155 156 157 158 |
# File 'lib/makara/proxy.rb', line 154 def disconnect! send_to_all(:disconnect!) rescue ::Makara::Errors::AllConnectionsBlacklisted, ::Makara::Errors::NoConnectionsAvailable # all connections are already down, nothing to do here end |
#graceful_connection_for(config) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/makara/proxy.rb', line 143 def graceful_connection_for(config) fake_wrapper = Makara::ConnectionWrapper.new(self, nil, config) @error_handler.handle(fake_wrapper) do connection_for(config) end rescue Makara::Errors::BlacklistConnection => e fake_wrapper.initial_error = e.original_error fake_wrapper end |
#hijacked? ⇒ Boolean
81 82 83 |
# File 'lib/makara/proxy.rb', line 81 def hijacked? @hijacked end |
#shard_aware_for(role) ⇒ Object
100 101 102 |
# File 'lib/makara/proxy.rb', line 100 def shard_aware_for(role) @config_parser.makara_config["#{role}_shard_aware".to_sym] end |
#stick_to_master!(persist = true) ⇒ Object
If persist is true, we stick the proxy to master for subsequent requests up to master_ttl duration. Otherwise we just stick it for the current request
87 88 89 90 |
# File 'lib/makara/proxy.rb', line 87 def stick_to_master!(persist = true) stickiness_duration = persist ? @ttl : 0 Makara::Context.stick(@id, stickiness_duration) end |
#strategy_class_for(strategy_name) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/makara/proxy.rb', line 108 def strategy_class_for(strategy_name) case strategy_name when 'round_robin', 'roundrobin', nil, '' ::Makara::Strategies::RoundRobin when 'failover' ::Makara::Strategies::PriorityFailover else strategy_name.constantize end end |
#strategy_for(role) ⇒ Object
92 93 94 |
# File 'lib/makara/proxy.rb', line 92 def strategy_for(role) strategy_class_for(strategy_name_for(role)).new(self) end |
#strategy_name_for(role) ⇒ Object
96 97 98 |
# File 'lib/makara/proxy.rb', line 96 def strategy_name_for(role) @config_parser.makara_config["#{role}_strategy".to_sym] end |
#without_sticking ⇒ Object
74 75 76 77 78 79 |
# File 'lib/makara/proxy.rb', line 74 def without_sticking @skip_sticking = true yield ensure @skip_sticking = false end |