-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding request timeout #157
base: master
Are you sure you want to change the base?
Conversation
requests exception handling in listen_forever
update to matrix-org master
bad_sync_timeout was initialized with 5000 (seconds), should be only 5 seconds.
There should probably be some form of retry limit so that it doesn't hang forever if a server is/goes down. |
My basic idea was: when using this sdk do design a bot, and the server is offline even for a few hours the client should reconnect again. So maybe the following is usefull: Option to define a maximal time until But then we have to discuss the following:
And also (design):
I would prefer the following:
|
Although reconnect/retry mechanisms are very useful and user-friendly I don't think this kind of logic belong in the SDK. If you want a bot that automatically reconnects when the server comes back online you should add some kind of heartbeat functionality in the bot and not force it on everyone else using the SDK. If it's decided to include this in the SDK please make it optional and make sure exceptions still are able to bubble up to the client implementations. |
Well, currently listen_forever is running endless, even if the server is not reachable. So I just did things to be compatible with old versions of this sdk. I did this pull request for a timeout in request, to make sure that requests.request is not running endless. (See #129) And I added the handling of the new timeout Errors because of the second comment in #149. Assume you are running listen_forever, and your router is changing the ip (a lot routers do that all 3 days). Now, do you want that the sdk is reconnecting again or not? I only changed things for that situation. So adding a max_retry_time (or something like that) would be a new feature, and IMHO that should be handled in a different pull request / issue. |
I don't mind the timeout. I just don't want to have auto-reconnect logic in the SDK. At the least the retry should be optional and have a set limit. If you only added the request timeout everything would be fine by me. But you also decided to catch |
I agree with you, the My suggestion:
|
original client.py
listen_forever with new exception handler
If Synapse is down you shouldn't get any response, a Are you running some form of proxy in-front of Synapse? |
Yes, I'm running nginx as proxy. And I get a 502 'Bad Gateway' error. Makes sense, of course. However, I implemented a new exception handler. Of course it would be prettier to remove the old exception handler. But then it would not be compatible to old versions. What do you think, should I do a pull request? But first this one should be merged. |
That explains the confusion. The problem here is not that listener isn't catching the exceptions correctly but rather that the API assumes that the HTTP errors are generated by the Matrix server and not something else (the nginx proxy in your case). @non-Jedi @Half-Shot any ideas? |
Okay. So I haven't read the actual changes yet, so I'm not commenting on that,
Any other questions I missed? |
@lugino-emeritus could you squash these commits and sign-off per |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default timeout should be user-tunable for every method of MatrixHttpApi
.
Either a new kwarg on every method or a default_timeout
kwarg on instantiation
of the object.
I'm also not sure 10 seconds is the correct default value. 30 seconds might make
more sense.
matrix_client/api.py
Outdated
@@ -582,6 +582,8 @@ def _send(self, method, path, content=None, query_params={}, headers={}, | |||
|
|||
if headers["Content-Type"] == "application/json" and content is not None: | |||
content = json.dumps(content) | |||
|
|||
request_timeout = 10.0 + query_params.get("timeout", 30000) / 1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason we're forcing this to be a float instead of an int?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no. It is just because the requests_timeout could be a float. I will change it.
matrix_client/api.py
Outdated
@@ -590,7 +592,8 @@ def _send(self, method, path, content=None, query_params={}, headers={}, | |||
params=query_params, | |||
data=content, | |||
headers=headers, | |||
verify=self.validate_cert | |||
verify=self.validate_cert, | |||
timeout=request_timeout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whatever error is raised by requests for a timeout needs to be handled and
reraised as a subclass of MatrixError
. (maybe a new error class
MatrixTimeoutError
?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also handle the ConnectTimeout next to the ReadTimeout here? With the same exception class? Maybe the ConnectTimeout should be handled with this pull request.
@non-Jedi I agree with you that making errors propagate is the best course of action. Also, trying to do something fancy to distinguish between actual Matrix server errors and errors spawned by an ingress service or such might be tricky. But, since HTTP errors are directly translated into Matrix errors rather than kept as request exceptions it might be worth doing something to avoid this kind of confusion. A short mention in the |
@simonklb would absolutely be a good thing, and I would accept a PR for that immediately. :) |
Signed-off-by: Niklas Tittjung <[email protected]>
Signed-off-by: Niklas Tittjung <[email protected]>
Just have to learn some github things... Sorry for changing this file temporary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than that, it LGTM. Fix those things, squash your commits, and rebase on current master, and we'll see if we can get this one merged before the next release. :)
matrix_client/api.py
Outdated
verify=self.validate_cert, | ||
timeout=request_timeout | ||
) | ||
except requests.exceptions.ReadTimeout: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catch requests.exceptions.Timeout
here instead.
matrix_client/api.py
Outdated
@@ -582,16 +582,24 @@ def _send(self, method, path, content=None, query_params={}, headers={}, | |||
|
|||
if headers["Content-Type"] == "application/json" and content is not None: | |||
content = json.dumps(content) | |||
|
|||
request_timeout = 30 + query_params.get("timeout", 30000) / 1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new kwarg default_timeout=25
to __init__
, and then do:
request_timeout = 5 + query_params.get("timeout", self.default_timeout * 1000) / 1000
matrix_client/api.py
Outdated
timeout=request_timeout | ||
) | ||
except requests.exceptions.ReadTimeout: | ||
raise MatrixTimeoutError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass the requests
exception into the constructor for MatrixTimeoutError
so that people can debug easier. Also consider passing in the full URL that
was called and the method to be inserted into the error's msg
(See
MatrixRequestError
for an example of this).
update to matrix-org/master
solved bug in MatrixHttpLibError
Signed-off-by: Niklas Tittjung [email protected]
Signed-off-by: Niklas Tittjung [email protected]
Signed-off-by: Niklas Tittjung [email protected]
request_timeout to master
master to request_timeout
Signed-off-by: Niklas Tittjung [email protected]
update from matrix.org
Signed-off-by: Niklas Tittjung [email protected]
_send
inapi.py
10 + query_params.get("timeout", 30000) / 1000
-> OK? IMHO it shouldn't be constant, since it has to be larger than a possible server side timeoutlisten_forever
This should solve #129 and #131