I believe something like this should work
module Udp
module Collector
class UDPHandler < EM::Connection
def http_connection
@http_connection ||= EventMachine::HttpRequest.new('http://databaseurl.com/')
end
def receive_data(data)
http = http_connection.post body: data, keepalive: true
end
end
end
end
But you can't execute parallel request in this manner, so you need to use some kind of connection pool.
The most simplest one without queueing and other stuff is:
module Udp
module Collector
class UDPHandler < EM::Connection
def connection_pool
@connection_pool ||= []
end
def get_connection
conn = connection_pool.shift
conn ||= EventMachine::HttpRequest.new('http://databaseurl.com/')
conn
end
def receive_data(data)
conn = get_connection
request = conn.post body: data, keepalive: true
request.callback do
connection_pool << conn
end
end
end
end
end