Delphi: Why does IdHTTP.ConnectTimeout make requests slower?
I discovered that when setting the ConnectTimeoout property for a TIdHTTP component, it makes the requests (GET and POST) become about 120ms slower?
Why is this, and can I avoid/bypass this somehow?
Env: D2010 with shipped Indy components, all updates installed for D2010. OS is WinXP (32bi开发者_如何学编程t) SP3 with most patches...
My timing routine is:
Procedure DoGet;
Var
Freq,T1,T2 : Int64;
Cli : TIdHTTP;
S : String;
begin
QueryPerformanceFrequency(Freq);
Try
QueryPerformanceCounter(T1);
Cli := TIdHTTP.Create( NIL );
Cli.ConnectTimeout := 1000; // without this we get < 15ms!!
S := Cli.Get('http://127.0.0.1/empty_page.php');
Finally
FreeAndNil(Cli);
QueryPerformanceCounter(T2);
End;
Memo1.Lines.Add('Time = '+FormatFloat('0.000',(T2-T1)/Freq) );
End;
With the ConnectTimeout set in code I get avg. times of 130-140ms, without it's about 5-15ms ...
When ConnectTimeout
is zero (and TIdAntifreeze
is not in effect), Indy simply connects. Otherwise, TIdIOHandlerStack.ConnectClient
calls DoConnectTimeout
, which creates a new thread to do the connecting while the calling thread sleeps and processes TIdAntifreeze
operations, waiting for the connection to be established. If there's not connection by the time the timeout elapses, it throws an exception.
Threads aren't free, and the calling thread will always sleep before checking whether the connection thread has accomplished its task. The default sleep duration is 125 ms. (To use something else, activate TIdAntifreeze
and set its IdleTimeout
property lower than 125.)
精彩评论