mirror of
https://github.com/ZeroCatDev/Classworks.git
synced 2026-05-13 19:35:07 +00:00
refactor: extract setCachedPreference helper to remove duplication in serverRotation
Agent-Logs-Url: https://github.com/Moonrend/Classworks/sessions/d254def7-bda7-413a-83b1-c553c1571523 Co-authored-by: Sunwuyuan <88357633+Sunwuyuan@users.noreply.github.com>
This commit is contained in:
parent
ef1ff77977
commit
e6c5855827
@ -25,6 +25,15 @@ const serverPreference = {
|
||||
probing: false, // Whether a background probe is currently running
|
||||
};
|
||||
|
||||
/**
|
||||
* Update the preference cache to mark the given server URL as preferred.
|
||||
* @param {string} url
|
||||
*/
|
||||
function setCachedPreference(url) {
|
||||
serverPreference.preferred = url;
|
||||
serverPreference.cachedAt = Date.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether an error should trigger rotation to the next server.
|
||||
* Network errors and 5xx server errors warrant rotation.
|
||||
@ -81,8 +90,7 @@ async function updateServerPreference() {
|
||||
.filter((r) => r.latency < Infinity)
|
||||
.sort((a, b) => a.latency - b.latency);
|
||||
if (reachable.length > 0) {
|
||||
serverPreference.preferred = reachable[0].url;
|
||||
serverPreference.cachedAt = Date.now();
|
||||
setCachedPreference(reachable[0].url);
|
||||
}
|
||||
} catch {
|
||||
// Probe failure is non-fatal; keep existing preference
|
||||
@ -169,8 +177,7 @@ export async function tryWithRotation(operation, options = {}) {
|
||||
|
||||
// Remember this server as the preferred one for future requests
|
||||
if (provider === "classworkscloud") {
|
||||
serverPreference.preferred = serverUrl;
|
||||
serverPreference.cachedAt = Date.now();
|
||||
setCachedPreference(serverUrl);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user