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



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

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



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

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



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

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)


71
72
73
# File 'lib/makara/proxy.rb', line 71

def hijacked?
  @hijacked
end

#stick_to_master!(write_to_cache = true) ⇒ Object



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

def stick_to_master!(write_to_cache = true)
  @master_context = Makara::Context.get_current
  Makara::Context.stick(@master_context, @id, @ttl) if write_to_cache
end

#strategy_class_for(strategy_name) ⇒ Object



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

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



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

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

#strategy_name_for(role) ⇒ Object



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

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

#without_stickingObject



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

def without_sticking
  before_context = @master_context
  @master_context = nil
  @skip_sticking = true
  yield
ensure
  @skip_sticking = false
  @master_context ||= before_context
end