HttpUrlConnection taking exactly 2 minutes for timeout - httpurlconnection

Whenever the URL is trying to load, it takes larger than the time set in the connection timeout, but setting the connection timeout to 20 seconds does not work it happens only after 2 minutes, and also whatever the time that is set in the connection time out, its taking exactly 2 minutes for the server to connection time out.
URL urlConnect = new URL(url.toString());
HttpURLConnection urlc = (HttpURLConnection) urlConnect.openConnection();
urlc.setDoInput(true);
urlc.setConnectTimeout(1000 * 20); // Timeout is in MILLI seconds
urlc.connect();
Or whether the 2 minutes is the default values for timeout? how to set the timeout to exactly 20 seconds. It doesnot have any redirect urls or anythings its a plain url. Why is it taking exactly 2 minutes for the connection to timeout?

Related

Session time out set by Curator Framework

I have just started using curator framework and found something that was very interesting and wanted to check if I my assumption is correct. Session time out in Zookeeper is twice the tikr time. So in my local zookeeper config file my tikr time is 8 seconds so the session time out becomes 16 seconds. Now when I create curator framework and set session time out to a value less than 16 seconds the negotiated session time out is set to 16 seconds but if I set the session timeout in the curator framework to say 25 seconds then the negotiated session time out is 25 seconds. Is it right to assume that the greater of the 2 values is set as session time out value?
The client sends a requested timeout, the ZK server responds with the timeout that it can give the client.
The current implementation requires that the timeout be a minimum of 2 times the tickTime (as set in the server configuration) and a maximum of 20 times the tickTime.
So a value less than 16 (8 * 2) will be increased to 16, and 25 is okay.
See https://zookeeper.apache.org/doc/current/zookeeperProgrammers.html#ch_zkSessions for more details

Behaviour of Hikari setConnectionTimeout

Just looking for an explanation of rationale for this bit of code (PoolUtiltites:293 in version 2.2.4):
dataSource.setLoginTimeout((int) TimeUnit.MILLISECONDS.toSeconds(Math.min(1000L, connectionTimeout)));
This code and the setConnectionTimeout method means that I get this behaviour:
connectionTimeout == 0, then loginTimeout = Integer.MAX_VALUE
connectionTimeout > 0 && < 100, then HikariConfig throws IllegalArgumentException
connectionTimeout >= 100 && <= 1000, then loginTimeout = connectionTimeout
connectionTeimout > 1000, then loginTimeout = 1000
That looks really weird to me!
It's almost like the Math.min should be Math.max ???
In my current project I'd like to fail connections after 30s, which is impossible in the current setup.
I'm using the 4.1 postgres jdbc driver, but I think this is not relevant to the issue above.
Many thanks - and cool pooling library!!!
Ok, there are a couple of moving parts here. First, Math.min() is a bug, it should be Math.max(). In light of that (it will be fixed) consider the following:
It is important to note that connections are created asynchronously in the pool. The setConnectionTimeout() sets the maximum time (in milliseconds) that a call to getConnection() will wait for a connection before timing out.
The DataSource loginTimeout is the maximum time that physical connection initiation to the database can take before timing out. Because HikariCP obtains connections asynchronously, if the connection attempt fails, HikariCP will continue to retry, but your calls to getConnection() will timeout appropriately. We are using the connectionTimeout in kind of a double duty for loginTimeout.
For example, lets say the pool is completely empty, and you have configured a connectionTimeout of 30 seconds. When you call getConnection() HikariCP, realizing that there are no idle connections available, starts trying to obtain a new one. There is little point in having a loginTimeout exceeding 30 seconds, in this case.
The intent of the Math.max() call is to ensure that we never set loginTimeout to 0 if the user has configured connectionTimeout to 250ms. TimeUnit.MILLESECONDS.toSeconds() would return 0 without the Math.max(). If the user has configured a connectionTimeout of 0, meaning they never want to timeout, the time conversion of Integer.MAX_VALUE results in several thousand years as a timeout (virtually never).
Having said that, and in light of how HikariCP connections to the database are obtained asynchronously, even without the Math.max() fix, you should be able to achieve application-level connection timeouts of 30s. Unless physical connections to your database exceed 1000ms you would be unaffected by the Math.min().
We are putting out a 2.2.5-rc3 release candidate in the next few hours. I will slot this fix in.

Increasing WS Promise timeout

I'm calling a REST ws that takes more time than 5 seconds and I'm getting a timeout for the Promise response. How can I increase this time above 5 seconds?
You can pass a Long to the get method of the promise that corresponds to the timeout of this method. For example:
promise.get(120000L);
would set a 2 minutes timeout for the get method.

NSURLRequest setTimeoutInterval does not work as expected

I have kept NSURLRequest timeout interval 30sec. And I have also configured the auto retry 3 times for request. So if any problem comes in request then total time should be 120 seconds (i.e. 2 mins).
But When i have kept Debug point on my server and if i request to server then my time out period is getting 3 minutes.
Can I configure this timeout??? Or it should work??
Is this default timeout interval Once request is made to Server??
Please note that 30 seconds times 3 is 90 seconds, or one and a half minutes, not 2 minutes.
As for the timeout problem, try to init your NSURLRequest with initWithURL:cachePolicy:timeoutInterval:. Make sure to check the status of your NSURLConnection delegate methods, especially in connection:didFailWithError:.
Also, maybe you should explain how you measure the time out period on the server.

Problem with nsurlconnection timeout

I've got a wierd problem with NSURLConnection. I've set the connection time out of 20 seconds like this.
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
NSURLConnection *con= [[NSURLConnection alloc] initWithRequest:request delegate:self];
I'm implementing the delegate methods for the NSURLConnection as well.
Now when I ran the app, the connection did not time out after 20 seconds but after 2-3 minutes it gave error for 'No internet connection'. Isn't it supposed to give a timeout error after 20 seconds?
The discussion for timeoutInterval says that it starts (is set to 0) when a process of load activity occurs:
The timeout interval specifies the
limit on the idle
interval alloted to a request in the process of loading. The "idle
interval" is defined as the period of time that has passed since the
last instance of load activity occurred for a request that is in the
process of loading. Hence, when an instance of load activity occurs
(e.g. bytes are received from the network for a request), the idle
interval for a request is reset to 0. If the idle interval ever
becomes greater than or equal to the timeout interval, the request
is considered to have timed out. This timeout interval is measured
in seconds.
No internet connection is an error. So probably the timeout will actually occur in 20 seconds (set time) if it gets a connection but takes more time to load...