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.



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

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/makara/proxy.rb', line 98

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_parserObject (readonly)

Returns the value of attribute config_parser.



46
47
48
# File 'lib/makara/proxy.rb', line 46

def config_parser
  @config_parser
end

#error_handlerObject (readonly)

Returns the value of attribute error_handler.



44
45
46
# File 'lib/makara/proxy.rb', line 44

def error_handler
  @error_handler
end

#stickyObject (readonly)

Returns the value of attribute sticky.



45
46
47
# File 'lib/makara/proxy.rb', line 45

def sticky
  @sticky
end

Class Method Details

.hijack_method(*method_names) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# 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
  end
end

.send_to_all(*method_names) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/makara/proxy.rb', line 34

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

#disconnect!Object



133
134
135
136
137
# File 'lib/makara/proxy.rb', line 133

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



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

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)


68
69
70
# File 'lib/makara/proxy.rb', line 68

def hijacked?
  @hijacked
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



74
75
76
77
# File 'lib/makara/proxy.rb', line 74

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

#strategy_class_for(strategy_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/makara/proxy.rb', line 87

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



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

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

#strategy_name_for(role) ⇒ Object



83
84
85
# File 'lib/makara/proxy.rb', line 83

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

#without_stickingObject



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

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