socketry / async-http

MIT License
298 stars 45 forks source link

Timeout questions #27

Closed ayanko closed 3 years ago

ayanko commented 4 years ago

This is interesting lib but I have few questions :)

I could do it with

Async do |task|
  response = task.with_timeout(10) do
     endpoint = Async::HTTP::Endpoint.parse(url)
     client = Async::HTTP::Client.new endpoint
     client.get('/')
  end

  body = task.with_timeout(15) do
     response.read
  end
end.wait

But it is probably not correct...

ioquatix commented 4 years ago

That's reasonable.

However, you should only use timeouts if you need it. Persistent connections will already detect and reconnect if needed. It shouldn't be a user facing issue in most cases. If the underlying TCP connection is still valid, you shouldn't need a timeout on the response.read operation. Otherwise, if the response is 1GB, it will surely timeout.

ioquatix commented 3 years ago

Let me know if you have more questions, but using with_timeout is the correct way. I would suggest putting it around reading each chunk from the body.

ayanko commented 3 years ago

But is with_timeout block async one?

ioquatix commented 3 years ago

Yes.