Skip to content
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

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7dd6a4a
add timeout to requests.request
lugino-emeritus Oct 4, 2017
1d915e2
handle requests timeout in listen_forever
lugino-emeritus Oct 4, 2017
98f9ce8
import ReadTimeout from requests.exceptions
lugino-emeritus Oct 6, 2017
83b5047
Update client.py
lugino-emeritus Oct 12, 2017
b73f4ad
Merge pull request #1 from matrix-org/master
lugino-emeritus Oct 16, 2017
545ade3
sync timeout correction
lugino-emeritus Oct 16, 2017
8365a37
Update client.py
lugino-emeritus Oct 16, 2017
456ed7f
Update client.py
lugino-emeritus Oct 17, 2017
1acce6a
Merge pull request #2 from lugino-emeritus/request_timeout
lugino-emeritus Oct 17, 2017
25ac7f2
Update client.py
lugino-emeritus Oct 17, 2017
561e59a
Adding MatrixTimeoutError
lugino-emeritus Oct 23, 2017
1be8337
import MatrixTimeoutError
lugino-emeritus Oct 23, 2017
fd18039
Merge branch 'develop' into master
lugino-emeritus Oct 24, 2017
eba1de2
Update client.py
lugino-emeritus Oct 24, 2017
7214851
restore api.py (temp)
lugino-emeritus Nov 2, 2017
6c762cb
Merge pull request #4 from matrix-org/master
lugino-emeritus Nov 2, 2017
ef1621d
Update errors.py
lugino-emeritus Nov 2, 2017
9000528
Update errors.py
lugino-emeritus Nov 2, 2017
1115d03
Update api.py
lugino-emeritus Nov 2, 2017
1280a99
correct bad_sync_timeout
lugino-emeritus Nov 2, 2017
c90d401
insert empty line
lugino-emeritus Nov 2, 2017
c7d57b9
update MatrixTimeoutError
lugino-emeritus Nov 3, 2017
0f88e26
edit MatrixTimeoutError
lugino-emeritus Nov 3, 2017
0fcd77b
Merge branch 'master' into request_timeout_update
lugino-emeritus Nov 3, 2017
d3c35da
Update code for request timeout
lugino-emeritus Nov 3, 2017
73b4341
pep corrections
lugino-emeritus Nov 3, 2017
81ea5bd
pep corrections
lugino-emeritus Nov 3, 2017
fd4692b
some pep corrections
lugino-emeritus Nov 3, 2017
93f573c
Update api.py
lugino-emeritus Nov 3, 2017
206e2a0
removed whitespace
lugino-emeritus Nov 3, 2017
5464f7a
remove blank line
lugino-emeritus Nov 3, 2017
1e465f8
Merge branch 'master' into master
lugino-emeritus Nov 6, 2017
7f70e5c
Merge pull request #9 from lugino-emeritus/master
lugino-emeritus Dec 14, 2017
021e7dd
Merge pull request #10 from matrix-org/master
lugino-emeritus Dec 14, 2017
18d1824
Update lugino-emeritus/master from matrix-org/master
lugino-emeritus Dec 14, 2017
0712ced
Merge pull request #12 from matrix-org/master
lugino-emeritus Mar 7, 2018
7523266
Update to current version (2018-03-07)
lugino-emeritus Mar 7, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions matrix_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import json
import requests
from time import time, sleep
from .errors import MatrixError, MatrixRequestError
from .errors import MatrixError, MatrixRequestError, MatrixTimeoutError

try:
from urllib import quote
Expand Down Expand Up @@ -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
Copy link
Collaborator

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


response = None
while True:
response = requests.request(
method, endpoint,
params=query_params,
data=content,
headers=headers,
verify=self.validate_cert
)
try:
response = requests.request(
method, endpoint,
params=query_params,
data=content,
headers=headers,
verify=self.validate_cert,
timeout=request_timeout
)
except requests.exceptions.ReadTimeout:
Copy link
Collaborator

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.

raise MatrixTimeoutError(
Copy link
Collaborator

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).

"A timeout occured while waiting for response"
)

if response.status_code == 429:
sleep(response.json()['retry_after_ms'] / 1000)
Expand Down
11 changes: 9 additions & 2 deletions matrix_client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ class MatrixError(Exception):


class MatrixUnexpectedResponse(MatrixError):
"""The home server gave an unexpected response. """
"""The home server gave an unexpected response."""
def __init__(self, content=""):
super(MatrixError, self).__init__(content)
self.content = content


class MatrixRequestError(MatrixError):
""" The home server returned an error response. """
"""The home server returned an error response."""

def __init__(self, code=0, content=""):
super(MatrixRequestError, self).__init__("%d: %s" % (code, content))
self.code = code
self.content = content


class MatrixTimeoutError(MatrixError):
"""A timeout occured while waiting for an answer."""

def __init__(self, msg=""):
super(MatrixTimeoutError, self).__init__(msg)