Просмотр исходного кода

fix(wechatmp): add clear_quota_lock to ensure thread safe

master
lanvent 1 год назад
Родитель
Сommit
79d10be8a0
1 измененных файлов: 12 добавлений и 3 удалений
  1. +12
    -3
      channel/wechatmp/wechatmp_client.py

+ 12
- 3
channel/wechatmp/wechatmp_client.py Просмотреть файл

@@ -12,6 +12,8 @@ class WechatMPClient(WeChatClient):
def __init__(self, appid, secret, access_token=None, session=None, timeout=None, auto_retry=True):
super(WechatMPClient, self).__init__(appid, secret, access_token, session, timeout, auto_retry)
self.fetch_access_token_lock = threading.Lock()
self.clear_quota_lock = threading.Lock()
self.last_clear_quota_time = -1

def clear_quota(self):
return self.post("clear_quota", data={"appid": self.appid})
@@ -35,6 +37,13 @@ class WechatMPClient(WeChatClient):
return super()._request(method, url_or_endpoint, **kwargs)
except APILimitedException as e:
logger.error("[wechatmp] API quata has been used up. {}".format(e))
response = self.clear_quota_v2()
logger.debug("[wechatmp] API quata has been cleard, {}".format(response))
return super()._request(method, url_or_endpoint, **kwargs)
if self.last_clear_quota_time == -1 or time.time() - self.last_clear_quota_time > 60:
with self.clear_quota_lock:
if self.last_clear_quota_time == -1 or time.time() - self.last_clear_quota_time > 60:
self.last_clear_quota_time = time.time()
response = self.clear_quota_v2()
logger.debug("[wechatmp] API quata has been cleard, {}".format(response))
return super()._request(method, url_or_endpoint, **kwargs)
else:
logger.error("[wechatmp] last clear quota time is {}, less than 60s, skip clear quota")
raise e

Загрузка…
Отмена
Сохранить