token刷新请求并发合并处理

This commit is contained in:
Vinlic 2024-03-13 18:41:07 +08:00
parent b4e8b489ec
commit 28bd87e7f0
2 changed files with 54 additions and 29 deletions

View File

@ -34,6 +34,8 @@ const FAKE_HEADERS = {
const FILE_MAX_SIZE = 100 * 1024 * 1024; const FILE_MAX_SIZE = 100 * 1024 * 1024;
// access_token映射 // access_token映射
const accessTokenMap = new Map(); const accessTokenMap = new Map();
// access_token请求队列映射
const accessTokenRequestQueueMap: Record<string, Function[]> = {};
/** /**
* access_token * access_token
@ -43,6 +45,11 @@ const accessTokenMap = new Map();
* @param refreshToken access_token的refresh_token * @param refreshToken access_token的refresh_token
*/ */
async function requestToken(refreshToken: string) { async function requestToken(refreshToken: string) {
if (accessTokenRequestQueueMap[refreshToken])
return new Promise(resolve => accessTokenRequestQueueMap[refreshToken].push(resolve));
accessTokenRequestQueueMap[refreshToken] = [];
logger.info(`Refresh token: ${refreshToken}`);
const result = await (async () => {
const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', { const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', {
headers: { headers: {
Authorization: `Bearer ${refreshToken}`, Authorization: `Bearer ${refreshToken}`,
@ -61,6 +68,25 @@ async function requestToken(refreshToken: string) {
refreshToken: refresh_token, refreshToken: refresh_token,
refreshTime: util.unixTimestamp() + ACCESS_TOKEN_EXPIRES refreshTime: util.unixTimestamp() + ACCESS_TOKEN_EXPIRES
} }
})()
.then(result => {
if(accessTokenRequestQueueMap[refreshToken]) {
accessTokenRequestQueueMap[refreshToken].forEach(resolve => resolve(result));
delete accessTokenRequestQueueMap[refreshToken];
}
logger.success(`Refresh successful`);
return result;
})
.catch(err => {
if(accessTokenRequestQueueMap[refreshToken]) {
accessTokenRequestQueueMap[refreshToken].forEach(resolve => resolve(err));
delete accessTokenRequestQueueMap[refreshToken];
}
return err;
});
if(_.isError(result))
throw result;
return result;
} }
/** /**

View File

@ -17,7 +17,6 @@ export default {
.validate('headers.authorization', _.isString) .validate('headers.authorization', _.isString)
const token = request.headers.authorization; const token = request.headers.authorization;
const refreshToken = token.replace('Bearer ', ''); const refreshToken = token.replace('Bearer ', '');
logger.info(`Refresh token: ${refreshToken}`);
const messages = request.body.messages; const messages = request.body.messages;
if (request.body.stream) { if (request.body.stream) {
const stream = await chat.createCompletionStream(request.body.messages, refreshToken, request.body.use_search); const stream = await chat.createCompletionStream(request.body.messages, refreshToken, request.body.use_search);