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

Simplify server rotation to retry on any error, not just network errors

Co-authored-by: Sunwuyuan <88357633+Sunwuyuan@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-01-19 13:07:22 +00:00
parent 45c48cdf82
commit f56c9b558e

View File

@ -90,7 +90,8 @@ export async function tryWithRotation(operation, options = {}) {
/** /**
* Get the effective server URL for the current provider * Get the effective server URL for the current provider
* For classworkscloud, returns the primary server (last known working) or first server in the list * For classworkscloud, returns the primary server (last successful in current session) or first server in the list
* Note: Primary server tracking resets on page reload
* For other providers, returns the configured domain * For other providers, returns the configured domain
* @returns {string} Server URL * @returns {string} Server URL
*/ */
@ -115,34 +116,8 @@ export function isRotationEnabled() {
} }
/** /**
* Check if an error is a network error that should trigger server rotation * Try operation with primary server first, fallback to rotation on any error
* @param {Error} error - The error to check * Uses the last successful server first for efficiency
* @returns {boolean}
*/
function isNetworkError(error) {
// Network errors from axios typically have no response or specific error codes
if (!error.response) {
return true; // No response = network issue
}
// Server timeout or connection errors
if (error.code === 'ECONNABORTED' || error.code === 'ETIMEDOUT' ||
error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
return true;
}
// 5xx errors might indicate server issues worth retrying
const status = error.response?.status;
if (status >= 500) {
return true;
}
return false;
}
/**
* Try operation with primary server first, fallback to rotation on network errors only
* This is more efficient than always trying rotation for every request
* @param {Function} operation - Async function that takes a serverUrl and returns a promise * @param {Function} operation - Async function that takes a serverUrl and returns a promise
* @param {Object} options - Options * @param {Object} options - Options
* @param {string} options.provider - Provider type (optional, defaults to current setting) * @param {string} options.provider - Provider type (optional, defaults to current setting)
@ -163,14 +138,9 @@ export async function tryWithPrimaryServer(operation, options = {}) {
try { try {
return await operation(primaryUrl); return await operation(primaryUrl);
} catch (error) { } catch (error) {
// Only rotate to other servers if it's a network error // On any error, try rotation with all servers
if (isNetworkError(error)) { console.warn(`Primary server ${primaryUrl} failed, trying rotation...`);
console.warn(`Primary server ${primaryUrl} failed with network error, trying rotation...`);
// Use full rotation, which will update the primary server if a different one succeeds // Use full rotation, which will update the primary server if a different one succeeds
return await tryWithRotation(operation, options); return await tryWithRotation(operation, options);
} }
// For non-network errors (e.g., 404, 401, validation errors), don't retry with other servers
throw error;
}
} }