1
0
mirror of https://github.com/ZeroCatDev/Classworks.git synced 2026-02-04 07:53:11 +00:00

Address code review feedback: optimize callbacks and clarify Socket.IO limitation

Co-authored-by: Sunwuyuan <88357633+Sunwuyuan@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-01-19 11:57:14 +00:00
parent bf9ff52ee0
commit b084c80b18
2 changed files with 15 additions and 7 deletions

View File

@ -36,7 +36,8 @@ export function getServerList(provider) {
*/
export async function tryWithRotation(operation, options = {}) {
const provider = options.provider || getSetting("server.provider");
const onServerTried = options.onServerTried || (() => {});
const onServerTried = options.onServerTried;
const hasCallback = typeof onServerTried === 'function';
const servers = getServerList(provider);
const triedServers = [];
@ -45,19 +46,25 @@ export async function tryWithRotation(operation, options = {}) {
for (const serverUrl of servers) {
try {
triedServers.push({ url: serverUrl, status: "trying" });
onServerTried({ url: serverUrl, status: "trying", tried: [...triedServers] });
if (hasCallback) {
onServerTried({ url: serverUrl, status: "trying", tried: [...triedServers] });
}
const result = await operation(serverUrl);
triedServers[triedServers.length - 1].status = "success";
onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] });
if (hasCallback) {
onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] });
}
return result;
} catch (error) {
lastError = error;
triedServers[triedServers.length - 1].status = "failed";
triedServers[triedServers.length - 1].error = error.message || String(error);
onServerTried({ url: serverUrl, status: "failed", error, tried: [...triedServers] });
if (hasCallback) {
onServerTried({ url: serverUrl, status: "failed", error, tried: [...triedServers] });
}
// Continue to next server
console.warn(`Server ${serverUrl} failed:`, error.message);

View File

@ -35,9 +35,10 @@ export function getSocket() {
}
connectedDomain = serverUrl;
// For classworkscloud, create socket with rotation support
// The socket will initially connect to the first server
// If connection fails, Socket.IO will handle reconnection
// For classworkscloud, create socket with the first server in rotation
// Note: Socket.IO's built-in reconnection will retry the same server URL.
// Server rotation is handled at the HTTP request level, not Socket.IO level.
// If the Socket.IO server goes down, the connection will fail until the server recovers.
socket = io(serverUrl, {transports: ["polling","websocket"]});
// Re-attach previously registered event handlers on new socket instance