Recent

Author Topic: [SOLVED] synapse: how to get a timeout for http-downloads to work?  (Read 342 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 613
My program shall download a website with synapse. Sometimes this website is not available. Then my code runs into a timeout of 130 seconds, which is too much. I want a timeout of max. 4 seconds.

Here is a demo of my code from a bigger program:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses sysutils, classes, httpsend;
  4.  
  5. const URL = 'http://xxx.yyy.com';
  6.  
  7. function myHttpGetText(url: string; HTTP: THTTPSend; var SL: TStringList): boolean;
  8.    {loads URL 'url' from a Webserver into a StringList}
  9.    var ok: boolean;
  10.    begin
  11.    ok:=false;
  12.    SL:=TStringList.Create;
  13.    HTTP.Clear; {Reset headers, document and Mimetype}
  14.  
  15.    if HTTP.HTTPMethod('GET',url) then
  16.        begin
  17.        SL.LoadFromStream(HTTP.Document);
  18.        ok:=true;
  19.        end;
  20.    exit(ok);
  21.    end;
  22.  
  23. function download_Website(url: string; var SL: TStringList): boolean;
  24.    var HTTP: THTTPSend;
  25.        ok: boolean;
  26.    begin
  27.    HTTP:=THTTPSend.Create;
  28.    HTTP.Timeout:=4000;
  29.    HTTP.Sock.SetTimeout(4000);
  30.    ok:=myHttpGetText(url, HTTP, SL);
  31.    HTTP.Free;
  32.    exit(ok);
  33.    end;
  34.  
  35. var SL: TStringList;
  36.     t: TDateTime;
  37.     sec: double;
  38.     ok: boolean;
  39.  
  40. begin
  41. t:=now;
  42. ok:=download_Website(URL, SL);
  43. sec:=(now-t)*86400.0; {time in seconds}
  44. writeln('Time: ', sec:0:2, ' seconds');
  45.  
  46. if not ok then writeln('ERROR') else writeln(SL.Text);
  47. SL.Free;
  48. end.

I use FPC 3.2.0 on Linux Ubuntu 18.04 with synapse stable version 40 and trunk r266 (both have the same result).

What I'm doing wrong? Thanks in advance.
« Last Edit: December 08, 2022, 05:51:27 pm by Hartmut »

GetMem

  • Hero Member
  • *****
  • Posts: 3953
Re: synapse: how to get a timeout for http-downloads to work?
« Reply #1 on: December 08, 2022, 03:00:59 pm »
@Hartmut
On windows immediately returns with the message "Error". I don't have to wait even 4 seconds.

Hartmut

  • Hero Member
  • *****
  • Posts: 613
Re: synapse: how to get a timeout for http-downloads to work?
« Reply #2 on: December 08, 2022, 04:25:16 pm »
Thank you GetMem for your reply. Yes, if the URL does not exist at all, then immediately "ERROR" is returned. But with my real URL - if it is currently unavailable - then I have a timeout of 130 seconds, which is too long for me.

rvk

  • Hero Member
  • *****
  • Posts: 5074
Re: synapse: how to get a timeout for http-downloads to work?
« Reply #3 on: December 08, 2022, 04:27:02 pm »
You probably want to set HTTP.Sock.ConnectionTimeout to whatever you want.

See my post here:
Sorry, I spoke too soon. What is said above still holds but the connect timeout was discussed on the Synapse list in 2012 and the non-blockingmode (which is mentioned above) was implemented in revision #160.

You also need to set the HTTPSender.Sock.ConnectionTimeout which handles the Connect timeout in non-blockingmode after which the socket is switched back to blocking mode.

Example:
Code: Pascal  [Select][+][-]
  1. const
  2.   URL = '10.255.255.1'; // <-- this is always non response
  3. var
  4.   HTTPSender: THTTPSend;
  5.   HTTPGetResult: boolean;
  6.   Tm: TDateTime;
  7. begin
  8.   HTTPSender := THTTPSend.Create;
  9.   try
  10.     HTTPSender.Timeout := 1 * 1000;
  11.     HTTPSender.Sock.ConnectionTimeout := 1 * 1000;
  12.     Tm := Now;
  13.     HTTPGetResult := HTTPSender.HTTPMethod('GET', URL);
  14.     Tm := Now - Tm;
  15.     ShowMessage(Format('%d: done in %.3f seconds', [HTTPSender.ResultCode, Tm * 24 * 60 * 60]));
  16.   finally
  17.     HTTPSender.Free;
  18.   end;
  19. end;

PS. When ConnectionTimeOut > 0 then Synapse will connect in non-blocking mode.
In blocking mode (the default) there is no way to set the timeout.
See http://www.ararat.cz/synapse/doku.php/public:howto:connecttimeout
« Last Edit: December 08, 2022, 04:32:39 pm by rvk »

Hartmut

  • Hero Member
  • *****
  • Posts: 613
Re: synapse: how to get a timeout for http-downloads to work?
« Reply #4 on: December 08, 2022, 05:51:05 pm »
Great! 'HTTPSender.Sock.ConnectionTimeout' works perfectly. Thanks a lot rvk.
Again I like synapse and it's capabilities.

 

TinyPortal © 2005-2018