mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-12-23 07:49:19 +08:00
token刷新请求并发合并处理
This commit is contained in:
parent
b4e8b489ec
commit
28bd87e7f0
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user