diff --git a/src/utils/api.js b/src/utils/api.js index 370e0b5..d4847a2 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -1,6 +1,6 @@ import axios from "@/axios/axios"; import {getSetting} from "@/utils/settings"; -import {tryWithPrimaryServer, isRotationEnabled} from "@/utils/serverRotation"; +import {tryWithRotation, isRotationEnabled} from "@/utils/serverRotation"; // Helper function to check if provider is valid for API calls const isValidProvider = () => { @@ -35,9 +35,9 @@ export const getNamespaceInfo = async () => { } try { - // Use primary server with fallback for classworkscloud provider + // Use rotation for classworkscloud provider if (isRotationEnabled()) { - const response = await tryWithPrimaryServer(async (serverUrl) => { + const response = await tryWithRotation(async (serverUrl) => { return await axios.get(`${serverUrl}/kv/_info`, { headers: getHeaders(), }); diff --git a/src/utils/providers/kvServerProvider.js b/src/utils/providers/kvServerProvider.js index 1b5f9d9..93952d5 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 {tryWithPrimaryServer, isRotationEnabled} from "../serverRotation"; +import {tryWithRotation, 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 primary server with fallback for classworkscloud provider + // Use rotation for classworkscloud provider if (isRotationEnabled()) { - return await tryWithPrimaryServer(async (serverUrl) => { + return await tryWithRotation(async (serverUrl) => { const res = await axios.get(`${serverUrl}/kv/_info`, { headers: getHeaders(), }); @@ -52,9 +52,9 @@ export const kvServerProvider = { async updateNamespaceInfo(data) { try { - // Use primary server with fallback for classworkscloud provider + // Use rotation for classworkscloud provider if (isRotationEnabled()) { - return await tryWithPrimaryServer(async (serverUrl) => { + return await tryWithRotation(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 primary server with fallback for classworkscloud provider + // Use rotation for classworkscloud provider if (isRotationEnabled()) { - return await tryWithPrimaryServer(async (serverUrl) => { + return await tryWithRotation(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 primary server with fallback for classworkscloud provider + // Use rotation for classworkscloud provider if (isRotationEnabled()) { - return await tryWithPrimaryServer(async (serverUrl) => { + return await tryWithRotation(async (serverUrl) => { await axios.post(`${serverUrl}/kv/${key}`, data, { headers: getHeaders(), }); @@ -171,9 +171,9 @@ export const kvServerProvider = { skip: skip.toString() }); - // Use primary server with fallback for classworkscloud provider + // Use rotation for classworkscloud provider if (isRotationEnabled()) { - return await tryWithPrimaryServer(async (serverUrl) => { + return await tryWithRotation(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 cc1aa15..ee0c286 100644 --- a/src/utils/serverRotation.js +++ b/src/utils/serverRotation.js @@ -11,9 +11,6 @@ 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 @@ -62,11 +59,6 @@ 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; @@ -90,8 +82,7 @@ export async function tryWithRotation(operation, options = {}) { /** * Get the effective server URL for the current provider - * 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 classworkscloud, returns the first server in the list * For other providers, returns the configured domain * @returns {string} Server URL */ @@ -99,8 +90,7 @@ export function getEffectiveServerUrl() { const provider = getSetting("server.provider"); if (provider === "classworkscloud") { - // Return primary server if available, otherwise first in list - return primaryServerUrl || CLASSWORKS_CLOUD_SERVERS[0]; + return CLASSWORKS_CLOUD_SERVERS[0]; } return getSetting("server.domain") || ""; @@ -114,33 +104,3 @@ export function isRotationEnabled() { const provider = getSetting("server.provider"); return provider === "classworkscloud"; } - -/** - * Try operation with primary server first, fallback to rotation on any error - * Uses the last successful server first for efficiency - * @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) { - // On any error, try rotation with all servers - console.warn(`Primary server ${primaryUrl} failed, trying rotation...`); - // Use full rotation, which will update the primary server if a different one succeeds - return await tryWithRotation(operation, options); - } -}