load balancing strategy
This commit is contained in:
+26
-5
@@ -1,21 +1,42 @@
|
||||
class SessionManager {
|
||||
constructor(provider) {
|
||||
this.sessions = provider.sessions;
|
||||
this.provider = provider;
|
||||
|
||||
// 初始化账号锁定状态
|
||||
for (const username in this.sessions) {
|
||||
this.sessions[username].locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有可用会话
|
||||
// 获取所有可用且未锁定会话
|
||||
getAvailableSessions() {
|
||||
return Object.keys(this.sessions).filter(username => this.sessions[username].valid);
|
||||
const currentMode = this.provider.currentMode;
|
||||
|
||||
return Object.keys(this.sessions).filter(username => {
|
||||
const session = this.sessions[username];
|
||||
return session.valid && !session.locked && session.modeStatus[currentMode];
|
||||
});
|
||||
}
|
||||
|
||||
// 随机选择会话
|
||||
// 随机选择一个可用会话
|
||||
getRandomSession() {
|
||||
const availableSessions = this.getAvailableSessions();
|
||||
if (availableSessions.length === 0) {
|
||||
throw new Error('没有可用的会话');
|
||||
}
|
||||
const randomIndex = Math.floor(Math.random() * availableSessions.length);
|
||||
return availableSessions[randomIndex];
|
||||
const selectedUsername = availableSessions[randomIndex];
|
||||
// 锁定账号
|
||||
this.sessions[selectedUsername].locked = true;
|
||||
return selectedUsername;
|
||||
}
|
||||
|
||||
// 释放账号锁定
|
||||
releaseSession(username) {
|
||||
if (this.sessions[username]) {
|
||||
this.sessions[username].locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 策略
|
||||
@@ -27,4 +48,4 @@ class SessionManager {
|
||||
}
|
||||
}
|
||||
|
||||
export default SessionManager;
|
||||
export default SessionManager;
|
||||
|
||||
Reference in New Issue
Block a user