Closed tonytonyjan closed 4 years ago
I found the following example would reuse the same connection:
Async do |task|
client = Async::HTTP::Client.new(Async::HTTP::Endpoint.parse('http://localhost:9292'))
3.times do
response = client.get('/')
response.read
task.sleep 1
end
end.wait
To reuse the same connection, calling response#read
is necessary.
Response#close
will immediately terminate the response. If there is no response body, this will still be able to reuse the connection. Otherwise, maybe the server will send 1GB of data. We have no choice but to close the connection.
For HTTP/2, the stream will be reset, which still allows the underlying connection to be reused.
It seems like neither
Async::HTTP::Internet
norAsync::HTTP::Client
can reuse the same http connection for sending multiple request.It can be done by built-in
net/http
:with
Async::HTTP::Internet
, the following example will create conneciton per request:with
Async::HTTP::Client
, the following example will create conneciton per request:I use the command below to see if requests' port are the same which means it uses the same TCP connection.