mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-02-07 00:22:25 +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):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user