[Solved] Connection timeout?

The forum for ZeosLib 7.2 Report problems. Ask for help, post proposals for the new version and Zeoslib 7.2 features here. This is a forum that will be edited once the 7.2.x version goes into RC/stable!!

My personal intention for 7.2 is to speed up the internals as optimal a possible for all IDE's. Hope you can help?! Have fun with testing 7.2
ertank
Junior Boarder
Junior Boarder
Posts: 37
Joined: 02.06.2017, 12:00

[Solved] Connection timeout?

Postby ertank » 31.10.2017, 01:16

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

Fr0sT
Zeos Dev Team
Zeos Dev Team
Posts: 120
Joined: 08.05.2014, 12:08

Re: Connection timeout?

Postby Fr0sT » 31.10.2017, 10:01

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.

ertank
Junior Boarder
Junior Boarder
Posts: 37
Joined: 02.06.2017, 12:00

Re: Connection timeout?

Postby ertank » 31.10.2017, 11:12

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.

ertank
Junior Boarder
Junior Boarder
Posts: 37
Joined: 02.06.2017, 12:00

Re: Connection timeout?

Postby ertank » 31.10.2017, 12:36

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/static/libpq-connect.html#LIBPQ-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?

Fr0sT
Zeos Dev Team
Zeos Dev Team
Posts: 120
Joined: 08.05.2014, 12:08

Re: Connection timeout?

Postby Fr0sT » 31.10.2017, 16:32

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?

ertank
Junior Boarder
Junior Boarder
Posts: 37
Joined: 02.06.2017, 12:00

Re: Connection timeout?

Postby ertank » 31.10.2017, 18:43

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');

ertank
Junior Boarder
Junior Boarder
Posts: 37
Joined: 02.06.2017, 12:00

Re: Connection timeout?

Postby ertank » 01.11.2017, 06:06

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


Return to “ZeosLib 7.2 Forum”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest