-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure get access_token thread-safe
- Loading branch information
Showing
4 changed files
with
29 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import threading | ||
import time | ||
|
||
from wechatpy.enterprise import WeChatClient | ||
|
||
|
||
class WechatComAppClient(WeChatClient): | ||
def __init__(self, corp_id, secret, access_token=None, session=None, timeout=None, auto_retry=True): | ||
super(WechatComAppClient, self).__init__(corp_id, secret, access_token, session, timeout, auto_retry) | ||
self.fetch_access_token_lock = threading.Lock() | ||
|
||
def fetch_access_token(self): # 重载父类方法,加锁避免多线程重复获取access_token | ||
with self.fetch_access_token_lock: | ||
access_token = self.session.get(self.access_token_key) | ||
if access_token: | ||
if not self.expires_at: | ||
return access_token | ||
timestamp = time.time() | ||
if self.expires_at - timestamp > 60: | ||
return access_token | ||
return super().fetch_access_token() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters