Delphi:为什么IdHTTP.ConnectTimeout会使请求变慢? [英] Delphi: Why does IdHTTP.ConnectTimeout make requests slower?

查看:415
本文介绍了Delphi:为什么IdHTTP.ConnectTimeout会使请求变慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,为TIdHTTP组件设置ConnectTimeoout属性时,它会使请求(GET和POST)的速度变慢约120ms吗?

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:带有Indy组件的D2010,为D2010安装了所有更新.操作系统是带有大多数补丁的WinXP(32bit)SP3 ...

Env: D2010 with shipped Indy components, all updates installed for D2010. OS is WinXP (32bit) SP3 with most patches...

我的计时程序是:

    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-140毫秒,没有时间大约是5-15毫秒...

With the ConnectTimeout set in code I get avg. times of 130-140ms, without it's about 5-15ms ...

推荐答案

ConnectTimeout为零(并且TIdAntifreeze无效)时,Indy会简单地连接.否则,TIdIOHandlerStack.ConnectClient调用DoConnectTimeout,它创建一个新线程来进行连接,而调用线程处于睡眠状态并处理TIdAntifreeze操作,等待与之连接.被建立.如果超时之前没有连接,则会引发异常.

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.

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

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

这篇关于Delphi:为什么IdHTTP.ConnectTimeout会使请求变慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆