Page 1 of 1

[Solved] Connection timeout?

Posted: 31.10.2017, 01:16
by ertank
Hello,

Using SVN 4052 of library with Lazarus 1.8.0RC3 (actually 1.6.4 bugfix branch) on a Raspberry Pi 3, Raspbian.
Linux raspberrypi 4.9.35-v7+ #1014 SMP Fri Jun 30 14:47:43 BST 2017 armv7l GNU/Linux

I had difficulties programmatic controlling of an IP number to identify if it's up (couldn't parse ping shell command output) and directly used TZConnection to check if remote system is up. If connection establishes, this means system is up. There are more than one single IP number and they are provided by another application (there is no human interaction). I have no option but to check them all, find first one up. Report if all fails.

This worked nicely until recently systems started to be mostly down and users started to wait and wait long times. For example Application running on 192.168.1.90 and IP number to check appear as 192.168.56.1. There are no routers between and that IP number is simply wrongly defined by remote system and due to fail connecting.

For a wrong provided *single* IP number, users have to wait 2 minute default TCP/IP timeout before TZConnection fails and application continue its check with next IP number in the list. On the other hand, if IP is fine, test finishes less than 110 milliseconds.

I also tried to use TZConnection.Ping() and TZConnection.PingServer() functions. I failed to find their right usage as they both return False even if IP number is fine and working with a TZConnection.Connect(). I believe they are to provide an option for keep-alive or something.

I am desperately trying to figure a way to shorten that time.

One solution comes up in mind is: if it is possible to provide a ConnectionTimeout value (maybe 2 seconds) to TZConnection? Or, maybe, it can be defined as an overloaded TZConnection.Connect(const ConnectionTimeout: Integer) method having ConnectionTimeout in milliseconds as a parameter?

I have no knowledge of TCP/IP connection details of ZeosLib or if there are any components used for that matter. However, I am willing to participate if it is doable.

I appreciate any help.

Thanks.

--Ertan

Re: Connection timeout?

Posted: 31.10.2017, 10:01
by Fr0sT
What DB driver do you use? Zeos provides generic interface but implementation is specific to driver. Generally Zeos calls some api_connect_to_server function and has no control over its timeout. You should check docs for your driver for an appropriate option.
In general case, controlling connection timeout in internal blocking soсket calls (not talking of those inside a library) isn't possible. If driver doesn't provide such option, the only solution is trying to connect in a thread and killing it when timeout occurs.

Regarding PingServer method, it is intended to work on an already established connection.

Re: Connection timeout?

Posted: 31.10.2017, 11:12
by ertank
Hello Fr0sT,

I forgot to mention about it. I am using PostgreSQL 9.4.13 on that Raspberry Pi. I never thought about threading. I need to think about it a little before implementing anything as users may not be able to use database system even if they are able to use software.

Thanks.

Re: Connection timeout?

Posted: 31.10.2017, 12:36
by ertank
Fr0sT wrote:You should check docs for your driver for an appropriate option.
I think it is possible to provide a connection timeout using libpq (PostgreSQL connectivity library) as to documentation in below link:
https://www.postgresql.org/docs/9.5/sta ... CONNSTRING

I am still volunteer for providing TZConnection.Connect(const ConnectionTimeout: Integer); overload implementation. Question remains here is: This method should check database type and raise meaningful exception upon usage with databases which does not have this feature.

Or, maybe it would be better to add a new ConnectionTimeout parameter in TZConnection. Use it with available drivers only?

Re: Connection timeout?

Posted: 31.10.2017, 16:32
by Fr0sT
Hello ertank,
actually the parameter is used
ZDbcPostgreSql.pas

Code: Select all

function TZPostgreSQLConnection.BuildConnectStr: AnsiString;
...
  { Sets a connection timeout. }
  ConnectTimeout := StrToIntDef(Info.Values['timeout'], -1);
  if ConnectTimeout >= 0 then
    AddParamToResult('connect_timeout', ZFastCode.IntToStr(ConnectTimeout));
...
Have you tried setting 'timeout' parameter?

Re: Connection timeout?

Posted: 31.10.2017, 18:43
by ertank
Hello Fr0sT,

I do not see any TZConnection.Timeout property. I am not sure how to set it. Should I do something like below?

Code: Select all

TZConnection.Properties.Add('timeout=2');

Re: Connection timeout?

Posted: 01.11.2017, 06:06
by ertank
Hello,

After testing I confirm that connection timeout is indeed present in ZeosLib for PostgreSQL database system. Usage is just like in my previous post and it is working very nicely.

I appreciate all help.

Thanks.
Ertan