From e6c5855827e25e292b94c27dc2a261271ee1bdaf Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 17 Apr 2026 13:51:20 +0000 Subject: [PATCH] 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> --- src/utils/serverRotation.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/utils/serverRotation.js b/src/utils/serverRotation.js index d720194..ebc0c97 100644 --- a/src/utils/serverRotation.js +++ b/src/utils/serverRotation.js @@ -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;