Skip to content

Commit 59f4345

Browse files
committed
Slowly increasing timeout
Starting timeout a lot smaller and slowly increasing on each timeout instead.
1 parent 5474886 commit 59f4345

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

pymyq/api.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
LOGIN_ENDPOINT = "api/v4/User/Validate"
1515
DEVICE_LIST_ENDPOINT = "api/v4/UserDeviceDetails/Get"
1616

17-
DEFAULT_TIMEOUT = 10
18-
DEFAULT_UPDATE_RETRIES = 3
17+
DEFAULT_TIMEOUT = 1
18+
DEFAULT_REQUEST_RETRIES = 3
1919

20-
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=DEFAULT_TIMEOUT)
20+
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
2121

2222
DEFAULT_USER_AGENT = "Chamberlain/3773 (iPhone; iOS 11.0.3; Scale/2.00)"
2323

@@ -86,32 +86,36 @@ async def _request(
8686
'User-Agent': DEFAULT_USER_AGENT,
8787
})
8888

89-
_LOGGER.debug('Initiating request to %s', url)
89+
start_request_time = datetime.time(datetime.now())
90+
_LOGGER.debug('%s Initiating request to %s', start_request_time, url)
9091
async with self._request_lock:
91-
for attempt in range(0, DEFAULT_UPDATE_RETRIES - 1):
92+
timeout = DEFAULT_TIMEOUT
93+
# Repeat twice amount of max requests retries for timeout errors.
94+
for attempt in range(0, (DEFAULT_REQUEST_RETRIES * 2) - 1):
9295
try:
9396
async with self._websession.request(
9497
method, url, headers=headers, params=params,
95-
data=data, json=json, timeout=DEFAULT_TIMEOUT,
98+
data=data, json=json, timeout=timeout,
9699
**kwargs) as resp:
97100
resp.raise_for_status()
98101
return await resp.json(content_type=None)
99-
except (asyncio.TimeoutError, ClientError) as err:
100-
if isinstance(err, ClientError):
101-
type_exception = 'ClientError'
102-
else:
103-
type_exception = 'TimeOut'
104-
105-
if attempt == DEFAULT_UPDATE_RETRIES - 1:
106-
raise RequestError(
107-
'{} requesting data from {}: {}'.format(
108-
type_exception, endpoint, err))
109-
110-
_LOGGER.warning('%s for %s; retrying: %s',
111-
type_exception, endpoint, err)
102+
except asyncio.TimeoutError:
103+
timeout = timeout * 2
104+
_LOGGER.warning('%s Timeout requesting from %s',
105+
start_request_time, endpoint)
106+
except ClientError as err:
107+
if attempt == DEFAULT_REQUEST_RETRIES - 1:
108+
raise RequestError('{} Client Error while requesting '
109+
'data from {}: {}'.format(
110+
start_request_time, endpoint,
111+
err))
112+
113+
_LOGGER.warning('%s Error requesting from %s; retrying: '
114+
'%s', start_request_time, endpoint, err)
112115
await asyncio.sleep(5)
113116

114-
_LOGGER.debug('Request to %s completed', url)
117+
raise RequestError('{} Constant timeouts while requesting data '
118+
'from {}'.format(start_request_time, endpoint))
115119

116120
async def _update_device_state(self) -> None:
117121
async with self._update_lock:

0 commit comments

Comments
 (0)