adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
530 stars 127 forks source link

Timeout support in HttpRequest #283

Closed GallaFrancesco closed 3 years ago

GallaFrancesco commented 3 years ago

The current class HttpRequest does not support timeout configuration: the default timeout for each request is hardcoded to 10 seconds, which can render the client unusable on slow connections.

The proposed changes allow a timeout to be set on a HttpRequest object using the non-empty constructor or by modifying the timeout field directly after a request has been initialized. Example:

import std.stdio;
import core.time;
import arsd.http2;

void main()
{
    auto client = new HttpClient();
    auto request = client.navigateTo(Uri("http://dlang.org")); // what if dlang.org takes >10 seconds to load...
    request.timeout = 20.seconds;
    auto response = request.waitForCompletion();
    assert(response.wasSuccessful, response.codeText);
    writeln(response.contentText);
}
adamdruppe commented 3 years ago

Something like this was on my list but the advanceConnections thing actually needs a little more care than this, the way you coded it here it will take the timeout of the last thing to call it, but other pending operations will be ignored.

I'll probably do a couple more tweaks to it. It is really supposed to be the smallest timeout of all pending connections passed there...

adamdruppe commented 3 years ago

ok, I think I got it fixed for multiple requests. I renamed it to a function setTimeout(value) to set it so then I can keep separate internal variables for the period and the remaining real time.