Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.28 KB | None | 0 0
  1. module EventMachine
  2.   module RequestRouter
  3.     class Connection < EventMachine::Connection
  4.       def on_response(data, name)
  5.         if self.error?
  6.           debug [:builtin_on_response, "Connection in error state"]
  7.         end
  8.        
  9.         if name.nil? or name == :localhost
  10.           debug [:send_attempt]
  11.           unless data.nil?
  12.             debug [:on_response, "Sending data"]
  13.             self.send_data data
  14.           end
  15.         end
  16.       end
  17.  
  18.  
  19.       def receive_data(data)
  20.         debug [:receive_data, "Connection receive data"]
  21.  
  22.         self.relay_to_backend(processed_data) unless processed_data.nil?
  23.       end
  24.  
  25.       def relay_to_backend(data)
  26.         debug [:relay_to_backend, "Connection relay to backend"]
  27.           servers ||= @servers.values.compact
  28.           servers.each do |s|
  29.             begin
  30.               server = EventMachine::connect(s[:host], s[:port], EventMachine::RequestRouter::Backend, s)
  31.               server.send data unless data.nil?
  32.             rescue ConnectionError
  33.               puts "Unable to connect to server: " + s[:host] + " " + $!
  34.             end
  35.           end
  36.       end
  37.  
  38.       def relay_from_backend(data, name)
  39.         debug [:relay_from_backend, name, data]
  40.  
  41.         self.on_response(data, name)
  42.       end
  43.   end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement