Closed DaniloOliveira28 closed 3 years ago
Hi @DaniloOliveira28!
Have you tried to put a time.sleep(0.01)
before your request? This answer says that sometimes these types of errors can be corrected with a time.sleep(0.01)
.
I have tried to put time.sleep(0.01)
before the get_view_result
method, but it does not work. The error still happening.
News about the error.
The error only happens if there is a big time gap between the calls to couchdb. My scenario, in each iteration I call couchdb to get a specific doc. To avoid make many calls, I did a local cache (a dict with the keys and the response). I removed this cache, so now, every time couchdb is being called. In this scenario, the error did not happen.
In my case, the gap between the requests is ~ 410s
The error only happens if there is a big time gap between the calls to couchdb. My scenario, in each iteration I call couchdb to get a specific doc. To avoid make many calls, I did a local cache (a dict with the keys and the response). I removed this cache, so now, every time couchdb is being called. In this scenario, the error did not happen.
If you are leaving connections idle longer than the server will keep them alive you need to disable HTTP keep-alive or handle the error and then retry the request.
You can probably disable the HTTP keep-alive by client.r_session.keep_alive = False
, or with the Connection: close
request header if requests
does not overwrite that.
Another option would be to configure your server/proxy to keep connections alive for longer.
How can I know if I am leaving connections idle? I though that the python-cloudant or request would manage these kind of behaviours automatically.
@mojito317 I could fix it!
The error was happening because the server was killing our connection. To avoid it, we need to add the following header on the client.py, as you suggested.
What do you think of a PR to add a param on CouchDB class to allow the user decide if he want to put the header or not?
What do you think of a PR to add a param on CouchDB class to allow the user decide if he want to put the header or not?
I think there is no need to do that, since the r_session
is already exposed to the user:
import os
from cloudant.client import Cloudant
client = Cloudant(os.environ['SERVER_USERNAME'], os.environ['SERVER_PASSWORD'], url=os.environ['SERVER_URL'], connect=True)
# you can set this way:
client.r_session.headers.update({'Connection': 'close'})
Edit: I double-checked it, and the client.r_session.keep_alive
option did not work (maybe it was a feature of requests
1.x) but setting the headers does like in the example above.
Ty @mojito317 .
Bug Description
We are using the cloudant-python lib to handle our connections with our database. Randomly the code raises a ConnectionResetError.
1. Steps to reproduce and the simplest code sample possible to demonstrate the issue
I could not create a external scenario to reproduce it, but I will outline the context of the error scenario.
I have an code that make iterative calls to our couchdb. Every loop make the same call to an specific view, but with different keys.
Sometimes, the request is not sent and the program return the error below.
At the first I thought that could be something related with our proxy - Haproxy. But, I have tested the script connecting directly with the database and the error continued. On the next, without the HAPROXY, I replaced the request call (from the python-cloudant) to a simple request (from request package) and the error disappeared, so I think that the error is related with this lib. In addition, my couchdb server does not log any request call, so, its is one more info that suggest that the error is on the client side.
2. What you expected to happen
The script should make the requests and finish without any error.
3. What actually happened
Eventually in some requests the script raise the error below.
Environment details