Delphi:为什么 IdHTTP.ConnectTimeout 使请求变慢?

发布于 2024-08-31 21:24:22 字数 879 浏览 6 评论 0原文

我发现当为 TIdHTTP 组件设置 ConnectTimeoout 属性时,它会使请求(GET 和 POST)变慢约 120 毫秒?

为什么会这样,我可以以某种方式避免/绕过它吗?

Env:D2010 附带 Indy 组件,所有更新均针对 D2010 安装。操作系统是 WinXP(32 位)SP3,带有大多数补丁...

我的计时例程是:

    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;

通过在代码中设置 ConnectTimeout,我得到平均。 130-140ms的时间,没有的话大约5-15ms......

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 (32bit) 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 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2024-09-07 21:24:22

ConnectTimeout 为零(且 TIdAntifreeze 未生效)时,Indy 只会进行连接。否则,TIdIOHandlerStack.ConnectClient 调用 DoConnectTimeout,它创建一个新线程来在调用线程休眠时进行连接并处理TIdAntifreeze操作,等待建立连接。如果超时后仍未建立连接,则会引发异常。

线程不是空闲的,调用线程在检查连接线程是否完成其任务之前将始终处于睡眠状态。默认睡眠持续时间为125 毫秒。 (要使用其他功能,请激活 TIdAntifreeze 并将其 IdleTimeout 属性设置为低于 125。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文