mirror of https://github.com/caronc/apprise
Fix throttling in matrix plugin (#1352)
parent
cf12873be2
commit
ce90151051
|
@ -177,7 +177,7 @@ class NotifyMatrix(NotifyBase):
|
||||||
default_retries = 2
|
default_retries = 2
|
||||||
|
|
||||||
# The number of micro seconds to wait if we get a 429 error code and
|
# The number of micro seconds to wait if we get a 429 error code and
|
||||||
# the server doesn't remind us how long we shoul wait for
|
# the server doesn't remind us how long we should wait for
|
||||||
default_wait_ms = 1000
|
default_wait_ms = 1000
|
||||||
|
|
||||||
# Our default is to no not use persistent storage beyond in-memory
|
# Our default is to no not use persistent storage beyond in-memory
|
||||||
|
@ -1279,6 +1279,9 @@ class NotifyMatrix(NotifyBase):
|
||||||
fn = requests.post if method == 'POST' else (
|
fn = requests.post if method == 'POST' else (
|
||||||
requests.put if method == 'PUT' else requests.get)
|
requests.put if method == 'PUT' else requests.get)
|
||||||
|
|
||||||
|
# Always call throttle before any remote server i/o is made
|
||||||
|
self.throttle()
|
||||||
|
|
||||||
# Define how many attempts we'll make if we get caught in a throttle
|
# Define how many attempts we'll make if we get caught in a throttle
|
||||||
# event
|
# event
|
||||||
retries = self.default_retries if self.default_retries > 0 else 1
|
retries = self.default_retries if self.default_retries > 0 else 1
|
||||||
|
@ -1316,25 +1319,24 @@ class NotifyMatrix(NotifyBase):
|
||||||
response = loads(r.content)
|
response = loads(r.content)
|
||||||
|
|
||||||
if r.status_code == requests.codes.too_many_requests:
|
if r.status_code == requests.codes.too_many_requests:
|
||||||
wait = self.default_wait_ms / 1000
|
wait_ms = self.default_wait_ms
|
||||||
try:
|
try:
|
||||||
wait = response['retry_after_ms'] / 1000
|
wait_ms = response['retry_after_ms']
|
||||||
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
try:
|
try:
|
||||||
errordata = response['error']
|
errordata = response['error']
|
||||||
wait = errordata['retry_after_ms'] / 1000
|
wait_ms = errordata['retry_after_ms']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
'Matrix server requested we throttle back {}ms; '
|
f'Matrix server requested we throttle back '
|
||||||
'retries left {}.'.format(wait, retries))
|
f'{wait_ms}ms; retries left {retries}.')
|
||||||
self.logger.debug(
|
self.logger.debug(f'Response Details:\r\n{r.content}')
|
||||||
'Response Details:\r\n{}'.format(r.content))
|
|
||||||
|
|
||||||
# Throttle for specified wait
|
# Throttle for specified wait
|
||||||
self.throttle(wait=wait)
|
self.throttle(wait=wait_ms / 1000)
|
||||||
|
|
||||||
# Try again
|
# Try again
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue