Skip to content

Commit

Permalink
fix:key分配不平均
Browse files Browse the repository at this point in the history
  • Loading branch information
story-x committed Jan 5, 2025
1 parent 0c4faf1 commit 91c558e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions models/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,26 @@ async function getKey (keys = Config.steam.apiKey) {
if (i.length === 1) {
return { retKeys: i, retKey: key }
}
let count = await redis.get(`${redisUseKey}${now}:${key}`) || 0
const keyNowUses = await redis.keys(`${redisUseKey}${now}:*`)
if (keyNowUses.length === 0) {
return { retKeys: i, retKey: key }
}
const keyUses = await redis.mGet(keyNowUses)
const keyUseMap = new Map()
for (let i = 0; i < keyNowUses.length; i++) {
keyUseMap.set(keyNowUses[i].split(':').pop(), Number(keyUses[i]))
}
let count = 0
// 获取使用次数最少的key
for (const k of i) {
if (k === key) {
continue
const use = keyUseMap.get(`${k}`)
if (!use) {
key = k
break
}
const c = await redis.get(`${redisUseKey}${now}:${k}`) || 0
if (c < count) {
if (use < count) {
count = use
key = k
count = c
}
}
return { retKeys: i, retKey: key }
Expand Down

0 comments on commit 91c558e

Please sign in to comment.