How to set connection timeout for Mongodb using pymongo? - mongodb

I tried setting connectTimeoutMS and socketTimeoutMS to a low value but it still takes about 20 seconds before my script times out. Am I not using the options correctly? I want the script to exit after 5 seconds.
def init_mongo():
mongo_connection = MongoClient('%s' %MONGO_SERVER, connectTimeoutMS=5000, socketTimeoutMS=5000)
if mongo_connection is None:
return
try:
<code>
except:
<code>

So if anyone comes across this later, I was using the wrong option.
What I was looking for is serverSelectionTimeoutMS

The web page:
https://api.mongodb.com/python/current/api/pymongo/mongo_client.html
says:
connectTimeoutMS: (integer or None) Controls how long (in milliseconds) the driver will wait during server monitoring when connecting a new socket to a server before concluding the server is unavailable. Defaults to 20000 (20 seconds)
(Where "server monitoring" is undefined)
So what? Is connectTimeoutMS sort of like a decoy to keep out the amateurs (like me)

Related

C3P0 - Single Connection with maxConnectionAge

I am quite new to C3P0 & was reading some of its properties here : https://www.mchange.com/projects/c3p0/#unreturnedConnectionTimeout
I have a simple question about C3P0 from those who have used it - If i set both minPoolSize = 1 & maxPoolSize = 1 & maxConnectionAge = 30 secs.
Will C3P0 connection destroy the connection after 30 secs & acquire a new connection Or
It will keep the same connection open?
Most probably, yes, but to know for sure, you will probably have to debug the code. You could try to set a break point in the connection constructor of your driver and let the timeout happen. Observe that the break point is hit to verify that it in fact works like that.

Protractor defaultTimeoutInterval doesn't work properly

in my
protractor.conf
file among the other paramteres I have these 3 timeouts:
allScriptsTimout
getPageTimeout
defaultTimeoutInterval
So the problem it that when I set
defaultTimeoutInterval=2000
or
defaultTimeoutInterval=13000
then protractor respects it and even if something last longer period of time I get:
Error: function timed out, ensure the promise resolves within 13000
milliseconds
or
Error: function timed out, ensure the promise resolves within 25000
milliseconds
but if I set a bigger number like around 25000 and higher then I always receive error:
Error: function timed out, ensure the promise resolves within 5000
milliseconds
Why is it set to 5sec in that cases? My tests fail because of that :)
Can you please help?

OperationTimeout /WriteTimeOut on Await Duration.Inf

I am looking at some code where data is written to cassandra using
Await.result(casDB.store(someVal), Duration.Inf)
I am seeing OperationTimeoutException and WriteTimeoutException. Say if we had 5 seconds timeout and server didnt respond within 5 second then i can think of these exception. But when duration is set to Inf not able to understand what is the reason for these exception.
There is a configuration named reference.conf in the akka.jar. The default timeout for akka is 5 seconds.
So you should create a new configuration and named it application.conf. set actor->typed->timeout = 3000s

Socket SO_SNDTIMEO Timout is double the set value

I recently patched my copy of GStreamer 0.10.36 to time out the tcpclientsink if the network connection is switched between wired/wireless (More information at Method to Cancel/Abort GStreamer tcpclientsink Timeout). It's a simple change. I just added the following to the gst_tcp_client_sink_start() function of gsttcpclientsink.c:
struct timeval timeout;
timeout.tv_sec = 60;
timeout.tv_usec = 0;
...
setsockopt (this->sock_fd.fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
The strange thing is that the actual timeout (measured by wall clock time) is always double the value I set. If I disrupt the network connection with the timeout set to 60 seconds, it will take 120 seconds for GStreamer/socket to abort. If I set the timeout to 30 seconds, it will take 60 seconds. If I set the timeout to 180 seconds, it will take 360 seconds. Is there something about sockets that I don't understand that might be causing this behavior? I'd really like to know what's going on here.
This might be a duplicate of Socket SO_RCVTIMEO Timeout is double the set value in C++/VC++
I'm pasting my answer below since I think I had a similar problem.
Pasted answer
SO_RCVTIMEO and SO_SNDTIMEO do not work on all socket operations, you should use non-blocking mode and select.
The behaviour may change on different operating system configurations.
On my system the connect timeouts after two times the value I set in SO_RCVTIMEO. A quick hack like setting SO_RCVTIMEO to x/2 before a connect and x after it works, but the proper solution is using select.
References
Discussion on this problem (read comments to answer):
https://stackoverflow.com/a/4182564/4074995
How to use select to achive the desired result:
http://beej.us/guide/bgnet/output/html/multipage/advanced.html#select
C: socket connection timeout

Boost asio connection trial takes too long

I have the following code:
...
namespace ba = boost::asio;
...
ba::ip::tcp::resolver resolver(ioService);
ba::ip::tcp::socket sock(ioService);
ba::ip::tcp::resolver::query query(the_ip, the_port);
ba::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
ba::ip::tcp::resolver::iterator end;
boost::system::error_code error = ba::error::host_not_found;
while (error && (endpoint_iterator != end))
{
sock.close();
sock.connect(*endpoint_iterator++, error);
}
When the server is up, it connects instantly and the rest of the code flows wonderfully, but when the server is offline or unreachable this while takes forever to exit.
Before the while: 09:04:37
When it exits: 09:05:40
Since I give query ONE IP and ONE PORT, shouldn't it exit instantly, given no connection can be established?
Are there any parameters I didn't configure? Something like "try X times" or "try for Y seconds"?
EDIT:
As seen in the comments, the problem seems to be a search on the internet, but the connection is local! (192.168...)
Why does it search the internet for a local IP?
As #MartinJames said on the comments:
"The internet is quite big, and has all sorts of links, some with very high latency.
Determining unreachability therefore takes a long time.
That is what happens when you connect all the computers on a planet together."
I tried disconnecting from the internet and the while exited instantly.
But the IP I'm using is local (192.168...), so this is still strange.
Thanks, man.