Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module EventMachine
- module RequestRouter
- class Connection < EventMachine::Connection
- def on_response(data, name)
- if self.error?
- debug [:builtin_on_response, "Connection in error state"]
- end
- if name.nil? or name == :localhost
- debug [:send_attempt]
- unless data.nil?
- debug [:on_response, "Sending data"]
- self.send_data data
- end
- end
- end
- def receive_data(data)
- debug [:receive_data, "Connection receive data"]
- self.relay_to_backend(processed_data) unless processed_data.nil?
- end
- def relay_to_backend(data)
- debug [:relay_to_backend, "Connection relay to backend"]
- servers ||= @servers.values.compact
- servers.each do |s|
- begin
- server = EventMachine::connect(s[:host], s[:port], EventMachine::RequestRouter::Backend, s)
- server.send data unless data.nil?
- rescue ConnectionError
- puts "Unable to connect to server: " + s[:host] + " " + $!
- end
- end
- end
- def relay_from_backend(data, name)
- debug [:relay_from_backend, name, data]
- self.on_response(data, name)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement