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:
parent
bf9ff52ee0
commit
b084c80b18
@ -36,7 +36,8 @@ export function getServerList(provider) {
|
|||||||
*/
|
*/
|
||||||
export async function tryWithRotation(operation, options = {}) {
|
export async function tryWithRotation(operation, options = {}) {
|
||||||
const provider = options.provider || getSetting("server.provider");
|
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 servers = getServerList(provider);
|
||||||
const triedServers = [];
|
const triedServers = [];
|
||||||
@ -45,19 +46,25 @@ export async function tryWithRotation(operation, options = {}) {
|
|||||||
for (const serverUrl of servers) {
|
for (const serverUrl of servers) {
|
||||||
try {
|
try {
|
||||||
triedServers.push({ url: serverUrl, status: "trying" });
|
triedServers.push({ url: serverUrl, status: "trying" });
|
||||||
|
if (hasCallback) {
|
||||||
onServerTried({ url: serverUrl, status: "trying", tried: [...triedServers] });
|
onServerTried({ url: serverUrl, status: "trying", tried: [...triedServers] });
|
||||||
|
}
|
||||||
|
|
||||||
const result = await operation(serverUrl);
|
const result = await operation(serverUrl);
|
||||||
|
|
||||||
triedServers[triedServers.length - 1].status = "success";
|
triedServers[triedServers.length - 1].status = "success";
|
||||||
|
if (hasCallback) {
|
||||||
onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] });
|
onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] });
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
lastError = error;
|
lastError = error;
|
||||||
triedServers[triedServers.length - 1].status = "failed";
|
triedServers[triedServers.length - 1].status = "failed";
|
||||||
triedServers[triedServers.length - 1].error = error.message || String(error);
|
triedServers[triedServers.length - 1].error = error.message || String(error);
|
||||||
|
if (hasCallback) {
|
||||||
onServerTried({ url: serverUrl, status: "failed", error, tried: [...triedServers] });
|
onServerTried({ url: serverUrl, status: "failed", error, tried: [...triedServers] });
|
||||||
|
}
|
||||||
|
|
||||||
// Continue to next server
|
// Continue to next server
|
||||||
console.warn(`Server ${serverUrl} failed:`, error.message);
|
console.warn(`Server ${serverUrl} failed:`, error.message);
|
||||||
|
|||||||
@ -35,9 +35,10 @@ export function getSocket() {
|
|||||||
}
|
}
|
||||||
connectedDomain = serverUrl;
|
connectedDomain = serverUrl;
|
||||||
|
|
||||||
// For classworkscloud, create socket with rotation support
|
// For classworkscloud, create socket with the first server in rotation
|
||||||
// The socket will initially connect to the first server
|
// Note: Socket.IO's built-in reconnection will retry the same server URL.
|
||||||
// If connection fails, Socket.IO will handle reconnection
|
// 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"]});
|
socket = io(serverUrl, {transports: ["polling","websocket"]});
|
||||||
|
|
||||||
// Re-attach previously registered event handlers on new socket instance
|
// Re-attach previously registered event handlers on new socket instance
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user