ruby - Net::HTTP – Flush or Close -


i've written consumer payment api. code issues post request , gets response api. i've implemented net::http, here relevant lines of code:

http = net::http.new(uri.host, 443) http.use_ssl = true http.verify_mode = openssl::ssl::verify_none request = net::http::post.new(uri.request_uri) request.set_form_data(params) response = http.request(request) 

this worked years, however, requests have reached timeouts when api under stress. api maintainer came explanation:

we pass on data rabbitmq synchronously after flushing http response. apparently, http libs wait connection closed before program continues on consumer side , think happening here. please reconfigure consumer not wait close continue right after response has been flushed.

i'm not sure how net::http implemented , whether waits close when response has been flushed. docs don't nor there setting control of this. , make matters worse, don't know how simulate this.

any ideas welcome!

i guess following experiment (with ruby 2.3) should give answer, post here in case else stumbles across question in future.

server.rb:

require 'socket'  server = tcpserver.new('localhost', 2345) loop   socket = server.accept   request = socket.gets   stderr.puts request   response = "hello world @ #{time.now}!\n"   socket.print "http/1.1 200 ok\r\n" +                "content-type: text/plain\r\n" +                "content-length: #{response.bytesize}\r\n" +                "connection: close\r\n"   socket.print "\r\n"   socket.print response   socket.flush   sleep 10   socket.close end 

client.rb:

require 'net/http'  http = net::http.new('localhost', 2345) request = net::http::post.new('/') response = http.request(request) puts response.body 

running server, client send 1 request , exit. immediately, flush sufficient have client code continue. restarting client within 10 seconds wait of server, causes client hang until 10 seconds have elapsed, printing hello world , once again exiting.

in other words: such simple net::http client not wait connection close continues execute it's code once server has flushed.


Comments