Indy idHttp freezes - How to work with keep-alive?
I developed a Webserver that uses idHttpServer and a client application that uses idHTTP.
I am using Delphi 2010 and the latest indy svn source from trunk.
This application sends about 1000 requests to the Web Server in a loop. Because of TIME_WAITS and the overhead of connecting to a webserver, I need to use keep-alive. The problem is: after making about 700 requests to the server, my application (the client side) hangs for almost 10 minutes when posting data to the webserver (that happens almost every time).
So, I need to know how to properly use keep-alive with indy.
So far I have this code:
On the client side:
oIndyHttpClient := TIdHTTP.Create(nil); oIndyHttpClient.ProxyParams.Clear; oIndyHttpClient.Request.CacheControl := 'no-cache'; oIndyHttpClient.ProtocolVersion := pv1_1; oIndyHttpClient.HTTPOptions := oIndyHttpClient.HTTPOptions + [hoKeepOrigProtocol]; oIndyHttpClient.ReuseSocket := rsOSDependent; oIndyHttpClient.Request.Connection := 'keep-alive';
And on the server side:
oIdHttpServer.OnCommandGet := Self.OnClientRead; oIdHttpServer.AutoStartSession := False; oIdHttpServer.KeepAlive := False; procedure TPLKWSServerSocketIndy.OnClientRead(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); begin //do some stuff here if LowerCase(ARequestInfo.Connection) = 'keep-alive' then begin AResponseInfo.CloseConnection := False; end else begin AResponseInfo.CloseConnection := True; end; end;
Am i doing it right? What can be causing the client application do freeze and do not complete the post request?
I tried do debug the server when the client freezes, but the OnClientReadmethod does not get fired. It seems to me that the client is having issues trying to connect do the web server.
If I modify the client code to:
oIndyHttpClient.ProtocolVersion := pv1_0; oIndyHttpClient.Request.Connection := 'close';
The client app does not freeze and everything works nice.
Should I clear IOHandler.InputBuffer before sending a request to the server? Is there anything else I need to do?
You do not need to manage keep-alives manually on the server side. TIdHTTPServer handles that for you. Simply set the TIdHTTPServer.KeepAlive property to True (it is False by default, and your code is setting it to False anyway) and do not set the AResponseInfo.CloseConnection property at all. TIdHTTPServer decides what value to set it to, on a per-request basis, before firing the OnCommandGet event.