1
0
mirror of https://github.com/ZeroCatDev/Classworks.git synced 2026-02-04 16:03:10 +00:00

Remove primary server optimization, use unified rotation logic for all requests

Co-authored-by: Sunwuyuan <88357633+Sunwuyuan@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-01-19 13:25:37 +00:00
parent f56c9b558e
commit 17f7b46d14
3 changed files with 16 additions and 56 deletions

View File

@ -1,6 +1,6 @@
import axios from "@/axios/axios"; import axios from "@/axios/axios";
import {getSetting} from "@/utils/settings"; 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 // Helper function to check if provider is valid for API calls
const isValidProvider = () => { const isValidProvider = () => {
@ -35,9 +35,9 @@ export const getNamespaceInfo = async () => {
} }
try { try {
// Use primary server with fallback for classworkscloud provider // Use rotation for classworkscloud provider
if (isRotationEnabled()) { if (isRotationEnabled()) {
const response = await tryWithPrimaryServer(async (serverUrl) => { const response = await tryWithRotation(async (serverUrl) => {
return await axios.get(`${serverUrl}/kv/_info`, { return await axios.get(`${serverUrl}/kv/_info`, {
headers: getHeaders(), headers: getHeaders(),
}); });

View File

@ -1,7 +1,7 @@
import axios from "@/axios/axios"; import axios from "@/axios/axios";
import {formatResponse, formatError} from "../dataProvider"; import {formatResponse, formatError} from "../dataProvider";
import {getSetting} from "../settings"; import {getSetting} from "../settings";
import {tryWithPrimaryServer, isRotationEnabled} from "../serverRotation"; import {tryWithRotation, isRotationEnabled} from "../serverRotation";
// Helper function to get request headers with kvtoken // Helper function to get request headers with kvtoken
const getHeaders = () => { const getHeaders = () => {
@ -23,9 +23,9 @@ const getHeaders = () => {
export const kvServerProvider = { export const kvServerProvider = {
async loadNamespaceInfo() { async loadNamespaceInfo() {
try { try {
// Use primary server with fallback for classworkscloud provider // Use rotation for classworkscloud provider
if (isRotationEnabled()) { if (isRotationEnabled()) {
return await tryWithPrimaryServer(async (serverUrl) => { return await tryWithRotation(async (serverUrl) => {
const res = await axios.get(`${serverUrl}/kv/_info`, { const res = await axios.get(`${serverUrl}/kv/_info`, {
headers: getHeaders(), headers: getHeaders(),
}); });
@ -52,9 +52,9 @@ export const kvServerProvider = {
async updateNamespaceInfo(data) { async updateNamespaceInfo(data) {
try { try {
// Use primary server with fallback for classworkscloud provider // Use rotation for classworkscloud provider
if (isRotationEnabled()) { if (isRotationEnabled()) {
return await tryWithPrimaryServer(async (serverUrl) => { return await tryWithRotation(async (serverUrl) => {
const res = await axios.put(`${serverUrl}/kv/_info`, data, { const res = await axios.put(`${serverUrl}/kv/_info`, data, {
headers: getHeaders(), headers: getHeaders(),
}); });
@ -78,9 +78,9 @@ export const kvServerProvider = {
async loadData(key) { async loadData(key) {
try { try {
// Use primary server with fallback for classworkscloud provider // Use rotation for classworkscloud provider
if (isRotationEnabled()) { if (isRotationEnabled()) {
return await tryWithPrimaryServer(async (serverUrl) => { return await tryWithRotation(async (serverUrl) => {
const res = await axios.get(`${serverUrl}/kv/${key}`, { const res = await axios.get(`${serverUrl}/kv/${key}`, {
headers: getHeaders(), headers: getHeaders(),
}); });
@ -108,9 +108,9 @@ export const kvServerProvider = {
async saveData(key, data) { async saveData(key, data) {
try { try {
// Use primary server with fallback for classworkscloud provider // Use rotation for classworkscloud provider
if (isRotationEnabled()) { if (isRotationEnabled()) {
return await tryWithPrimaryServer(async (serverUrl) => { return await tryWithRotation(async (serverUrl) => {
await axios.post(`${serverUrl}/kv/${key}`, data, { await axios.post(`${serverUrl}/kv/${key}`, data, {
headers: getHeaders(), headers: getHeaders(),
}); });
@ -171,9 +171,9 @@ export const kvServerProvider = {
skip: skip.toString() skip: skip.toString()
}); });
// Use primary server with fallback for classworkscloud provider // Use rotation for classworkscloud provider
if (isRotationEnabled()) { if (isRotationEnabled()) {
return await tryWithPrimaryServer(async (serverUrl) => { return await tryWithRotation(async (serverUrl) => {
const res = await axios.get(`${serverUrl}/kv/_keys?${params}`, { const res = await axios.get(`${serverUrl}/kv/_keys?${params}`, {
headers: getHeaders(), headers: getHeaders(),
}); });

View File

@ -11,9 +11,6 @@ const CLASSWORKS_CLOUD_SERVERS = [
"https://kv-service.wuyuan.dev", "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 * Get the list of servers to try for the given provider
* @param {string} provider - The provider type * @param {string} provider - The provider type
@ -62,11 +59,6 @@ export async function tryWithRotation(operation, options = {}) {
onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] }); onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] });
} }
// Update primary server on success (for classworkscloud provider)
if (provider === "classworkscloud") {
primaryServerUrl = serverUrl;
}
return result; return result;
} catch (error) { } catch (error) {
lastError = error; lastError = error;
@ -90,8 +82,7 @@ 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 successful in current session) or first server in the list * For classworkscloud, returns the 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
*/ */
@ -99,8 +90,7 @@ export function getEffectiveServerUrl() {
const provider = getSetting("server.provider"); const provider = getSetting("server.provider");
if (provider === "classworkscloud") { if (provider === "classworkscloud") {
// Return primary server if available, otherwise first in list return CLASSWORKS_CLOUD_SERVERS[0];
return primaryServerUrl || CLASSWORKS_CLOUD_SERVERS[0];
} }
return getSetting("server.domain") || ""; return getSetting("server.domain") || "";
@ -114,33 +104,3 @@ export function isRotationEnabled() {
const provider = getSetting("server.provider"); const provider = getSetting("server.provider");
return provider === "classworkscloud"; 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);
}
}