From b3969d1fd7d2db053831b479a73c6eb4b636edc6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 19 Jan 2026 12:51:06 +0000 Subject: [PATCH] Optimize server rotation to use primary server first, only rotate on network errors Co-authored-by: Sunwuyuan <88357633+Sunwuyuan@users.noreply.github.com> --- src/utils/providers/kvServerProvider.js | 22 ++++---- src/utils/serverRotation.js | 74 ++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 13 deletions(-) diff --git a/src/utils/providers/kvServerProvider.js b/src/utils/providers/kvServerProvider.js index 93952d5..1b5f9d9 100644 --- a/src/utils/providers/kvServerProvider.js +++ b/src/utils/providers/kvServerProvider.js @@ -1,7 +1,7 @@ import axios from "@/axios/axios"; import {formatResponse, formatError} from "../dataProvider"; import {getSetting} from "../settings"; -import {tryWithRotation, isRotationEnabled} from "../serverRotation"; +import {tryWithPrimaryServer, isRotationEnabled} from "../serverRotation"; // Helper function to get request headers with kvtoken const getHeaders = () => { @@ -23,9 +23,9 @@ const getHeaders = () => { export const kvServerProvider = { async loadNamespaceInfo() { try { - // Use rotation for classworkscloud provider + // Use primary server with fallback for classworkscloud provider if (isRotationEnabled()) { - return await tryWithRotation(async (serverUrl) => { + return await tryWithPrimaryServer(async (serverUrl) => { const res = await axios.get(`${serverUrl}/kv/_info`, { headers: getHeaders(), }); @@ -52,9 +52,9 @@ export const kvServerProvider = { async updateNamespaceInfo(data) { try { - // Use rotation for classworkscloud provider + // Use primary server with fallback for classworkscloud provider if (isRotationEnabled()) { - return await tryWithRotation(async (serverUrl) => { + return await tryWithPrimaryServer(async (serverUrl) => { const res = await axios.put(`${serverUrl}/kv/_info`, data, { headers: getHeaders(), }); @@ -78,9 +78,9 @@ export const kvServerProvider = { async loadData(key) { try { - // Use rotation for classworkscloud provider + // Use primary server with fallback for classworkscloud provider if (isRotationEnabled()) { - return await tryWithRotation(async (serverUrl) => { + return await tryWithPrimaryServer(async (serverUrl) => { const res = await axios.get(`${serverUrl}/kv/${key}`, { headers: getHeaders(), }); @@ -108,9 +108,9 @@ export const kvServerProvider = { async saveData(key, data) { try { - // Use rotation for classworkscloud provider + // Use primary server with fallback for classworkscloud provider if (isRotationEnabled()) { - return await tryWithRotation(async (serverUrl) => { + return await tryWithPrimaryServer(async (serverUrl) => { await axios.post(`${serverUrl}/kv/${key}`, data, { headers: getHeaders(), }); @@ -171,9 +171,9 @@ export const kvServerProvider = { skip: skip.toString() }); - // Use rotation for classworkscloud provider + // Use primary server with fallback for classworkscloud provider if (isRotationEnabled()) { - return await tryWithRotation(async (serverUrl) => { + return await tryWithPrimaryServer(async (serverUrl) => { const res = await axios.get(`${serverUrl}/kv/_keys?${params}`, { headers: getHeaders(), }); diff --git a/src/utils/serverRotation.js b/src/utils/serverRotation.js index ee0c286..a0717f2 100644 --- a/src/utils/serverRotation.js +++ b/src/utils/serverRotation.js @@ -11,6 +11,9 @@ const CLASSWORKS_CLOUD_SERVERS = [ "https://kv-service.wuyuan.dev", ]; +// Track the current primary server (the one that's currently working) +let primaryServerUrl = null; + /** * Get the list of servers to try for the given provider * @param {string} provider - The provider type @@ -59,6 +62,11 @@ export async function tryWithRotation(operation, options = {}) { onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] }); } + // Update primary server on success (for classworkscloud provider) + if (provider === "classworkscloud") { + primaryServerUrl = serverUrl; + } + return result; } catch (error) { lastError = error; @@ -82,7 +90,7 @@ export async function tryWithRotation(operation, options = {}) { /** * Get the effective server URL for the current provider - * For classworkscloud, returns the first server in the list + * For classworkscloud, returns the primary server (last known working) or first server in the list * For other providers, returns the configured domain * @returns {string} Server URL */ @@ -90,7 +98,8 @@ export function getEffectiveServerUrl() { const provider = getSetting("server.provider"); if (provider === "classworkscloud") { - return CLASSWORKS_CLOUD_SERVERS[0]; + // Return primary server if available, otherwise first in list + return primaryServerUrl || CLASSWORKS_CLOUD_SERVERS[0]; } return getSetting("server.domain") || ""; @@ -104,3 +113,64 @@ export function isRotationEnabled() { const provider = getSetting("server.provider"); return provider === "classworkscloud"; } + +/** + * Check if an error is a network error that should trigger server rotation + * @param {Error} error - The error to check + * @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 {Object} options - Options + * @param {string} options.provider - Provider type (optional, defaults to current setting) + * @returns {Promise} Result from the operation + */ +export async function tryWithPrimaryServer(operation, options = {}) { + const provider = options.provider || getSetting("server.provider"); + + // For non-classworkscloud providers, just use the configured domain + if (provider !== "classworkscloud") { + const serverUrl = getSetting("server.domain"); + return await operation(serverUrl); + } + + // For classworkscloud, try primary server first + const primaryUrl = getEffectiveServerUrl(); + + try { + return await operation(primaryUrl); + } catch (error) { + // Only rotate to other servers if it's a network error + if (isNetworkError(error)) { + 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 + return await tryWithRotation(operation, options); + } + + // For non-network errors (e.g., 404, 401, validation errors), don't retry with other servers + throw error; + } +}