Search code examples
delphikeep-aliveindy10

Setting KeepAlive Timeout in Delphi 2010 with indy 10.5.8


I'm using Delphi 2010 and Indy 10.5.8

I'm trying to understand how do we use keepalive feature. I came across a method SetKeepAliveValues where we explicitly set the keepalive timeout period & also found a link according to which connection's ReadTimeout or SessionTimeOut will mimic the same. In my server program I had defined a SessionTimeOut for IdHttpServer

  WebServer := TIdHTTPServer.Create(nil);
  WebServer.SessionState := True;
  WebServer.SessionTimeOut := 30000;
  WebServer.AutoStartSession := True;
  WebServer.OnCommandGet := CustomOnCommandGet;

and in CustomOnCommandGet I'm checking and setting the CloseConnection property of TIdHTTPResponseInfo as

  if LowerCase(ARequestInfo.Connection) = 'keep-alive' then begin
    AResponseInfo.CloseConnection := False;
  end
  else begin
    AResponseInfo.CloseConnection := True;
  end;

Will this suffice the requirement of setting keepalive or I have to compulsorily set the timeout in SetKeepAliveValues, guide me in setting up the same any additional pointers will be very helpfull

Thanks,


Solution

  • TIdHTTPServer already manages the AResponseInfo.CloseConnection property for you. If you want to allow HTTP keepalives, simply set the TIdHTTPServer.KeepAlive property to true, then the server will analyze client requests to decide whether keepalives are being requested so it can decide whether or not to close the socket connection after sending a response. If TIdHTTPServer.KeepAlive is false, all connections are always closed. When HTTP keepalives are used, a client can send multiple requests over the same socket connection, which improves network performance. In HTTP 0.9, keepalives don't exist. In HTTP 1.0, they are off by default, they have to be explicitly requested. In HTTP 1.1, they are on by default, they have to be explicitly disabled.

    SetKeepAliveValues() deals with TCP keepalives instead. While an active socket connection is idle (such as when an HTTP client has requested an HTTP keepalive and has not sent a new request yet), TCP itself will periodically send little heartbeat packets in the background to make sure both peers are still alive and responsive. Applications don't see these packets, they are handled by the OS. SetKeepAliveValues() allows you to specify how long an idle connection waits before sending heartbeats, and the interval between heartbeats (the defaults are typically quite large). If a heartbeat fails, the OS closes the connection.

    Sessions have nothing to do with keepalives. A Session is merely a mechanism for persisting data between requests. Sessions exist only in application memory, and have their own timeout so they can be cleaned up when not used for awhile. When a new session is created, it is assigned a unique ID that gets sent to the client in an HTTP cookie. Subsequent requests can then include that cookie to retrieve the session if it still exists. This allows the OnCommand... events to access persistent data via the ARequestInfo.Session and AResponseInfo.Session properties.