JRPete
JRPete

Reputation: 3144

Rails non-blocking post

I need to POST to another server rather frequently in my Rails app, but it takes foreeever. The catch is that I don't need the response. Is there any way to speed things up by sending the request and then dropping the connection without waiting for a response, without getting into delayed job or event machine? If those are the only option, I can deal with it, but it would make life simpler not to have to.

Here's what I have now:

remoteAddress = 'http://remote.server.address/'

params = {
  'a' => a,
  'b' => b }

url = URI.parse(remoteAddress)
Net::HTTP.post_form(url, params)

Upvotes: 0

Views: 504

Answers (1)

Ryan Barton
Ryan Barton

Reputation: 313

If you instantiate a Net::HTTP object before making the call, you can set its read_timeout attribute to a very small number, and then catch the resulting Timeout::Error exception and move on.

Here's an example that uses individual parts of the URL and query-string-formatted POST data:

endpoint = Net::HTTP.new(host, port)
endpoint.read_timeout = 0.001
endpoint.post(path, params_as_query_string) rescue Timeout::Error

Upvotes: 3

Related Questions