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

Optimize server rotation to use primary server first, only rotate on network errors

Co-authored-by: Sunwuyuan <88357633+Sunwuyuan@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-01-19 12:51:06 +00:00
parent bd566cdfe5
commit b3969d1fd7
2 changed files with 83 additions and 13 deletions

View File

@ -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(),
});

View File

@ -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;
}
}