Class: Makara::Proxy

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/makara/proxy.rb

Constant Summary collapse

METHOD_MISSING_SKIP =
[ :byebug, :puts ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Proxy

Returns a new instance of Proxy.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/makara/proxy.rb', line 65

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



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/makara/proxy.rb', line 123

def method_missing(m, *args, &block)
  if METHOD_MISSING_SKIP.include?(m)
    return super
  end

  any_connection do |con|
    if con.respond_to?(m, true)
      con.send(m, *args, &block)
    else
      super
    end
  end
end

Instance Attribute Details

#config_parserObject (readonly)

Returns the value of attribute config_parser.



62
63
64
# File 'lib/makara/proxy.rb', line 62

def config_parser
  @config_parser
end

#controlObject (readonly)

Returns the value of attribute control.



63
64
65
# File 'lib/makara/proxy.rb', line 63

def control
  @control
end

#error_handlerObject (readonly)

Returns the value of attribute error_handler.



60
61
62
# File 'lib/makara/proxy.rb', line 60

def error_handler
  @error_handler
end

#stickyObject (readonly)

Returns the value of attribute sticky.



61
62
63
# File 'lib/makara/proxy.rb', line 61

def sticky
  @sticky
end

Class Method Details

.control_method(*method_names) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/makara/proxy.rb', line 46

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

    ruby2_keywords method_name if Module.private_method_defined?(:ruby2_keywords)
  end
end

.hijack_method(*method_names) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/makara/proxy.rb', line 21

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

    ruby2_keywords method_name if Module.private_method_defined?(:ruby2_keywords)
  end
end

.send_to_all(*method_names) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/makara/proxy.rb', line 36

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

    ruby2_keywords method_name if Module.private_method_defined?(:ruby2_keywords)
  end
end

Instance Method Details

#default_shard_for(role) ⇒ Object



108
109
110
# File 'lib/makara/proxy.rb', line 108

def default_shard_for(role)
  @config_parser.makara_config["#{role}_default_shard".to_sym]
end

#disconnect!Object



156
157
158
159
160
# File 'lib/makara/proxy.rb', line 156

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



145
146
147
148
149
150
151
152
153
154
# File 'lib/makara/proxy.rb', line 145

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

Returns:

  • (Boolean)


85
86
87
# File 'lib/makara/proxy.rb', line 85

def hijacked?
  @hijacked
end

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
# File 'lib/makara/proxy.rb', line 139

def respond_to_missing?(m, include_private = false)
  any_connection do |con|
    con._makara_connection.respond_to?(m, true)
  end
end

#shard_aware_for(role) ⇒ Object



104
105
106
# File 'lib/makara/proxy.rb', line 104

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



91
92
93
94
# File 'lib/makara/proxy.rb', line 91

def stick_to_master!(persist = true)
  stickiness_duration = persist ? @ttl : 0
  Makara::Context.stick(@id, stickiness_duration)
end

#strategy_class_for(strategy_name) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/makara/proxy.rb', line 112

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



96
97
98
# File 'lib/makara/proxy.rb', line 96

def strategy_for(role)
  strategy_class_for(strategy_name_for(role)).new(self)
end

#strategy_name_for(role) ⇒ Object



100
101
102
# File 'lib/makara/proxy.rb', line 100

def strategy_name_for(role)
  @config_parser.makara_config["#{role}_strategy".to_sym]
end

#without_stickingObject



78
79
80
81
82
83
# File 'lib/makara/proxy.rb', line 78

def without_sticking
  @skip_sticking = true
  yield
ensure
  @skip_sticking = false
end