Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

Building a Standalone WebBroker Server for Linux – ThreadPool Management

Digital Innovation Partner

Building a Standalone WebBroker Server for Linux – ThreadPool Management

With Delphi you can create a Linux stand alone webBrocker application server as you can see in the following video:

 

 

Yuo can also specify for you WebBrocker app the threadpool managing, that can help you to increase performance of your app server.

In this example we configure a thread pool of 50 preallocated thread to manage connection shortly.

To use thread pool you need to add “IdSchedulerOfThreadPool” unit in you code and you can associate “TIdSchedulerOfThreadPool” on server start:

 

procedure RunServer(APort: Integer);
var
  LServer: TIdHTTPWebBrokerBridge;
  LidScheduler: TIdSchedulerOfThreadPool;
  LResponse: string;

  LGetSSLPassword: TGetSSLPassword;
  LIOHandleSSL: TIdServerIOHandlerSSLOpenSSL;

begin
  WriteCommands;
  LServer := TIdHTTPWebBrokerBridge.Create(nil);

  if Application = nil then
    WriteLn('Application is null');




  WriteLn('Srv Connections:' + LServer.MaxConnections.ToString);// + ' App Connections:' + Application.MaxConnections.ToString);
  try
    LServer.DefaultPort := APort;

    if (DmShared.Settings.certificate_file_name.Replace('-','').Trim <> '') then
     Begin
      if LServer.IOHandler <> nil then
       begin
         try LServer.IOHandler.Free; except end;
         LServer.IOHandler := nil;
       end;

      LGetSSLPassword := TGetSSLPassword.Create;
      LIOHandleSSL := TIdServerIOHandlerSSLOpenSSL.Create(LServer);
      LIOHandleSSL.SSLOptions.CertFile := DmShared.Settings.certificate_file_name;
      LIOHandleSSL.SSLOptions.RootCertFile := DmShared.Settings.certificate_root_file_name;
      LIOHandleSSL.SSLOptions.KeyFile := DmShared.Settings.certificate_key_file_name;
      LIOHandleSSL.SSLOptions.SSLVersions := [sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];
      LIOHandleSSL.OnGetPassword := LGetSSLPassword.OnGetSSLPassword;
      LServer.IOHandler := LIOHandleSSL;
      LServer.OnQuerySSLPort := DmShared.onQuerySSLPort;

      // IVAN : 2022 05 29: INSERISCO LA GESTIONE DEI THREAD POOL - BOH VEDIAMO
      LidScheduler := TIdSchedulerOfThreadPool.Create;
      LidScheduler.MaxThreads := 0;
      LidScheduler.PoolSize := 100;
      LServer.Scheduler := LidScheduler;
      LServer.MaxConnections := 4096;
     End;

    StartServer(LServer);
    while True do
    begin
      Readln(LResponse);
      LResponse := LowerCase(LResponse);
      if LResponse.StartsWith(cCommandSetPort) then
        SetPort(LServer, LResponse)
      else if sametext(LResponse, cCommandStart) then
        StartServer(LServer)
      else if sametext(LResponse, cCommandStatus) then
        WriteStatus(LServer)
      else if sametext(LResponse, 'h') then
       begin
        show_headers := not show_headers;
        if show_headers then
         writeln('SHOW HEADERS: On')
        Else
         writeln('SHOW HEADERS: Off');
       end
      else if sametext(LResponse, 'd') then
       begin
        debug_mode := not debug_mode;
        if debug_mode then
         writeln('DEBUG: On')
        Else
         writeln('DEBUG: Off');
       end
      else if sametext(LResponse, cCommandStop) then
        StopServer(LServer)
      else if sametext(LResponse, cCommandHelp) then
        WriteCommands
      else if sametext(LResponse, cCommandExit) then
        if LServer.Active then
        begin
          StopServer(LServer);
          thLog.Terminate;
          thLog.WaitFor;
          FreeAndNil(thLog);
          break
        end
        else
          break
      else
      begin
        Writeln(sInvalidCommand);
        Write(cArrow);
      end;
    end;
  finally
    LServer.Free;
  end;
end;