Code Monkey home page Code Monkey logo

Comments (3)

rlebeau avatar rlebeau commented on June 18, 2024 1

Currently, the keep-alive is infinite by default. If you set a ReadTimeout on the connection, then the connection will be closed if the timeout elapses while waiting for a new request to arrive. There is currently no max request limit implemented.

I do have code written for a future release that will add new KeepAliveTimeout and KeepAliveMaxRequests properties to make this behavior more configurable. That code is not in master at this time.

from indy.

WeberAndre avatar WeberAndre commented on June 18, 2024

Hi,
thanks for the hint with the ReadTimeout. Now I have to find an event or method just after connect to change the Readtimout of the connection, but that would be easy I think.
The number of requests I will look into DoRequest and possible set CloseConnection there. For counting the requests I will create my own TidContext subclass and store there the number of done requests?

from indy.

rlebeau avatar rlebeau commented on June 18, 2024

Hi, thanks for the hint with the ReadTimeout. Now I have to find an event or method just after connect to change the Readtimout of the connection

You can use the OnConnect event for that, eg:

procedure TMyForm.IdHTTPServerConnect(AContext: TIdContext);
begin
  AContext.Connection.IOHandler.ReadTimeout := ...;
end;

The number of requests I will look into DoRequest and possible set CloseConnection there.

It would be easier to just handle that in your OnCommand... event handler(s). You can set AResponseInfo.CloseConnection as desired. Just be sure to also include a custom Keep-Alive header in AResponseInfo.CustomHeaders for responses that you set to CloseConnection=False, so that the client knows what your timeout is and how many remaining requests are allowed, eg:

procedure TMyForm.IdHTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  ...
  AResponseInfo.CloseConnection := ...;
  if not AResponseInfo.CloseConnection then
  begin
    AResponseInfo.CustomHeaders.Params['Keep-Alive', 'timeout'] := IntToStr(AContext.Connection.IOHandler.ReadTimeout div 1000);
    AResponseInfo.CustomHeaders.Params['Keep-Alive', 'max'] := ...;
  end;
  ...
end;

For counting the requests I will create my own TidContext subclass and store there the number of done requests?

Yes, that would be a good way to handle it for now, eg:

type
  TMyContext = class(TIdServerContext)
    NumRequestsAllowed: Integer;
  end;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  IdHTTPServer.ContextClass := TMyContext;
end;

procedure TMyForm.IdHTTPServerConnect(AContext: TIdContext);
begin
  AContext.Connection.IOHandler.ReadTimeout := ...;
  TMyContext(AContext).NumRequestsAllowed := ...;
end;

procedure TMyForm.IdHTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  ...
  Dec(TMyContext(AContext).NumRequestsAllowed);
  AResponseInfo.CloseConnection := (TMyContext(AContext).NumRequestsAllowed < 1);
  if not AResponseInfo.CloseConnection then
  begin
    AResponseInfo.CustomHeaders.Params['Keep-Alive', 'timeout'] := IntToStr(AContext.Connection.IOHandler.ReadTimeout div 1000);
    AResponseInfo.CustomHeaders.Params['Keep-Alive', 'max'] := IntToStr(TMyContext(AContext).NumRequestsAllowed);
  end;
  ...
end;

The new code I have written will handle all of this for you. If you'd like to see what that new code looks like so far, it is currently checked in to IdCustomHTTPServer.pas in the Indy11-preparation branch.

from indy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.