mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 21:38:18 +08:00
fix(wechatmp): add clear_quota_lock to ensure thread safe
This commit is contained in:
@@ -12,6 +12,8 @@ class WechatMPClient(WeChatClient):
|
|||||||
def __init__(self, appid, secret, access_token=None, session=None, timeout=None, auto_retry=True):
|
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)
|
super(WechatMPClient, self).__init__(appid, secret, access_token, session, timeout, auto_retry)
|
||||||
self.fetch_access_token_lock = threading.Lock()
|
self.fetch_access_token_lock = threading.Lock()
|
||||||
|
self.clear_quota_lock = threading.Lock()
|
||||||
|
self.last_clear_quota_time = -1
|
||||||
|
|
||||||
def clear_quota(self):
|
def clear_quota(self):
|
||||||
return self.post("clear_quota", data={"appid": self.appid})
|
return self.post("clear_quota", data={"appid": self.appid})
|
||||||
@@ -35,6 +37,13 @@ class WechatMPClient(WeChatClient):
|
|||||||
return super()._request(method, url_or_endpoint, **kwargs)
|
return super()._request(method, url_or_endpoint, **kwargs)
|
||||||
except APILimitedException as e:
|
except APILimitedException as e:
|
||||||
logger.error("[wechatmp] API quata has been used up. {}".format(e))
|
logger.error("[wechatmp] API quata has been used up. {}".format(e))
|
||||||
response = self.clear_quota_v2()
|
if self.last_clear_quota_time == -1 or time.time() - self.last_clear_quota_time > 60:
|
||||||
logger.debug("[wechatmp] API quata has been cleard, {}".format(response))
|
with self.clear_quota_lock:
|
||||||
return super()._request(method, url_or_endpoint, **kwargs)
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user