-
-
Save martincik/222235 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
require 'redis' | |
require 'hash_ring' | |
class RedisProcz < EventMachine::Connection | |
def initialize | |
@ring = HashRing.new | |
end | |
def receive_data(data) | |
p [:recieve_data, data] | |
cmd, key, _ = data.split(/\s/) | |
if cmd && key | |
key = key.chomp | |
p [:cmd, cmd] | |
p [:key, key] | |
redis = node_for_key(key) | |
redis.send_data data | |
end | |
end | |
def redis_server(host, port) | |
srv = EventMachine::connect(host, port, RedisProczBackend) do |c| | |
c.client = self | |
end | |
@ring.add_node(srv) | |
end | |
def node_for_key(key) | |
key = $1 if key =~ /\{(.*)?\}/ | |
p ["choosing node for key: #{key}"] | |
@ring.get_node(key) | |
end | |
def proxy_from_backend(data) | |
p [:proxy_from_backend, data] | |
send_data data unless data.nil? | |
end | |
end | |
class RedisProczBackend < EventMachine::Connection | |
attr_accessor :client | |
def receive_data(data) | |
@client.proxy_from_backend(data) | |
end | |
end | |
EM.run { | |
EM.start_server("127.0.0.1", 1024, RedisProcz) {|conn| | |
conn.redis_server('localhost', 6379) | |
conn.redis_server('localhost', 6380) | |
conn.redis_server('localhost', 6381) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment