Indy idHttp freezes - How to work with keep-alive?

I developed a web server that uses idHttpServer and a client application that uses idHTTP .

I am using Delphi 2010 and the latest source indy svn from trunk.

This application sends about 1000 requests to the web server in a loop. Due to TIME_WAITS and the overhead of connecting to the web server, I need to use keep-alive . The problem is this: after creating about 700 requests to the server, my application (client side) hangs for almost 10 minutes when sending data to the web server (which happens almost every time).

So, I need to know how to use keep-alive with indy correctly.

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 cause a client application to freeze and not fill out a mail request?

I tried to debug the server when the client freezes, but the OnClientRead method OnClientRead not start. It seems to me that the client is having problems connecting to the web server.

If I change the client code to:

 oIndyHttpClient.ProtocolVersion := pv1_0; oIndyHttpClient.Request.Connection := 'close'; 

The client application does not freeze, and everything works well.

Should I clear IOHandler.InputBuffer before sending a request to the server? Anything else I need to do?

thanks

+4
source share
1 answer

You do not need to manage accounts manually on the server side. TIdHTTPServer handles this for you. Just set the TIdHTTPServer.KeepAlive property to True (the default is False, and your code sets it to False anyway) and do not set the AResponseInfo.CloseConnection property AResponseInfo.CloseConnection all. TIdHTTPServer decides what value to set for each request before firing the OnCommandGet event.

+8
source

Source: https://habr.com/ru/post/1383717/


All Articles