0

I have a TMyIdHTTPServer server object extends TIdHTTPServer. My TMyIdHTTPServer has some private variable shared in a multithreading context (i think TIdHTTPServer creates a new thread for each request).

I'm not a delphi or multithreading programming expert and i'm not sure that my approach is safe and/or has good performance.

What happens when some threads read same variable, there is performance degradation? there is a risk for thread conflict?

Basic example:

type
    TMyLogObject = class
       // write string LogMessage in LogFilePath
       procedure WriteLog(const LogMessage, LogFilePath: string);
    end;

    TMyIdHTTPServer = class(TIdHTTPServer)
    private
       FMyLogObject : TMyLogObject;
       FLogPath     : string; 
       procedure  ServerStart;
       procedure  ServerStop;
       procedure  OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
       procedure  OnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
    public
       constructor Create(const ConfigINI string); reintroduce;
    end;

implementation

   // ConfigINI is a absolute path of server_config.ini
   constructor Create(const ConfigINI string);
   var
     aConfigINI: TIniFile;
   begin
     inherited;

     aConfigINI := TIniFile.Create(ConfigINI);
     try
       // set the path of server log file
       FLogPath := FConfigINI.ReadString('CONFIG', 'LOG_PATH', '');
     finally
       aConfigINI.free;
     end;
   end;

   procedure TMyIdHTTPServer.ServerStart;
   begin
       self.Active  := True;
       FMyLogObject := TMyLogObject.Create;
   end;

   procedure TMyIdHTTPServer.ServerStop;
   begin
       self.Active  := False;
       FMyLogObject.Free;
   end;

   procedure TMyIdHTTPServer.OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
   begin
       AContext.Connection.OnWorkEnd := OnWorkEnd;

       FMyLogObject.WriteLog('StartRequest', FLogPath + 'log.txt');

       AResponseInfo.ContentText := 'Hello!';
   end;

   procedure TMyIdHTTPServer.OnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
   begin
       FMyLogObject.WriteLog('EndRequest', FLogPath + 'log.txt');
   end;
ar099968
  • 6,963
  • 12
  • 64
  • 127
  • 1
    There is no issue with multiple threads reading your FLogPath. You will want to make your TMyLogObject (or use thereof) thread safe. If you are dumping entries out to the log.txt file without any locking mechanism you will eventually find a very interesting jumble of text in your log. – Anthony Eischens Feb 10 '16 at 15:49
  • 1
    I would pass the log file path and name only once, in the constructor of TMyLogObject, not with every log statement – mjn Feb 10 '16 at 15:58
  • @AnthonyEischens right... thanks! – ar099968 Feb 10 '16 at 15:59
  • @mjn right... thanks! – ar099968 Feb 10 '16 at 16:00
  • 1
    *TIdHTTPServer creates a new thread for each request*: it is not that simple, there is no 1:1 relation between requests and threads in TIdHTTPServer – mjn Feb 10 '16 at 16:01
  • @mjn i'm not sure.. i'm studying indy stack :( – ar099968 Feb 10 '16 at 16:08

0 Answers0