From 3d6d94e38f7570c3db1267beb6a0380d995d23d9 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 1 Feb 2026 16:33:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E5=87=8F=E5=B0=91=E9=A6=96?= =?UTF-8?q?=E5=B1=8F=E6=B8=B2=E6=9F=93=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/public/index.html | 24 +++++++------- frontend/src/api/base.js | 40 ++++++++++++++++++++---- frontend/src/api/chat/ChatClientRelay.js | 5 +-- frontend/src/main.js | 4 +-- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/frontend/public/index.html b/frontend/public/index.html index 0bc75e6..b7041db 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -15,21 +15,21 @@ if (process.env.LIB_USE_CDN) { if (process.env.NODE_ENV === 'production') { %> - - - - - - + + + + + + <% } else { %> - - - - - - + + + + + + <% } } diff --git a/frontend/src/api/base.js b/frontend/src/api/base.js index 3c6cb56..9f1d030 100644 --- a/frontend/src/api/base.js +++ b/frontend/src/api/base.js @@ -9,9 +9,10 @@ export const apiClient = axios.create({ }) export let init +export let ensureBaseUrlInited export let getBaseUrl if (!process.env.BACKEND_DISCOVERY) { - init = async function() {} + init = function() {} const onRequest = config => { config.baseURL = getBaseUrl() @@ -24,16 +25,19 @@ if (!process.env.BACKEND_DISCOVERY) { apiClient.interceptors.request.use(onRequest, onRequestError, { synchronous: true }) + ensureBaseUrlInited = async function() {} + getBaseUrl = function() { return window.location.origin } } else { - init = async function() { - return updateBaseUrls() + init = function() { + updateBaseUrls() } - const onRequest = config => { + const onRequest = async config => { + await firstInitPromise let baseUrl = getBaseUrl() if (baseUrl === null) { throw new Error('No available endpoint') @@ -64,7 +68,7 @@ if (!process.env.BACKEND_DISCOVERY) { return promise } - apiClient.interceptors.request.use(onRequest, onRequestError, { synchronous: true }) + apiClient.interceptors.request.use(onRequest, onRequestError) apiClient.interceptors.response.use(onResponse, onResponseError) const DISCOVERY_URLS = process.env.NODE_ENV === 'production' ? [ @@ -85,6 +89,13 @@ if (!process.env.BACKEND_DISCOVERY) { let curBaseUrl = null let baseUrlToCircuitBreaker = new Map() + let firstInitResolve = null + let firstInitPromise = new Promise(resolve => { + firstInitResolve = resolve + }).then(() => { + firstInitResolve = null + }) + const doUpdateBaseUrls = async() => { async function requestGetUrls(discoveryUrl) { try { @@ -115,6 +126,14 @@ if (!process.env.BACKEND_DISCOVERY) { let url = `${baseUrl}/api/ping` await axios.get(url, { timeout: 3 * 1000 }) sortedBaseUrls.push(baseUrl) + + if (firstInitResolve) { + if (curBaseUrl === null) { + curBaseUrl = baseUrl + console.log('Switch server endpoint to', curBaseUrl) + } + firstInitResolve() + } } catch { errorBaseUrls.push(baseUrl) } @@ -124,14 +143,23 @@ if (!process.env.BACKEND_DISCOVERY) { sortedBaseUrls = sortedBaseUrls.concat(errorBaseUrls) baseUrls = sortedBaseUrls - if (baseUrls.indexOf(curBaseUrl) === -1) { + if (curBaseUrl !== null && baseUrls.indexOf(curBaseUrl) === -1) { curBaseUrl = null } + if (firstInitResolve) { + // 全失败了则在这里resolve + firstInitResolve() + } + console.log('Found server endpoints:', baseUrls) } const updateBaseUrls = _.throttle(doUpdateBaseUrls, 3 * 60 * 1000) + ensureBaseUrlInited = async function() { + return firstInitPromise + } + getBaseUrl = function() { updateBaseUrls() diff --git a/frontend/src/api/chat/ChatClientRelay.js b/frontend/src/api/chat/ChatClientRelay.js index 4cf26aa..111d42f 100644 --- a/frontend/src/api/chat/ChatClientRelay.js +++ b/frontend/src/api/chat/ChatClientRelay.js @@ -1,4 +1,4 @@ -import { getBaseUrl } from '@/api/base' +import { ensureBaseUrlInited, getBaseUrl } from '@/api/base' import * as chat from '.' import * as chatModels from './models' @@ -46,13 +46,14 @@ export default class ChatClientRelay { this.msgHandler.onDebugMsg(new chatModels.DebugMsg({ content })) } - wsConnect() { + async wsConnect() { if (this.isDestroying) { return } this.addDebugMsg('Connecting') + await ensureBaseUrlInited() let baseUrl = getBaseUrl() if (baseUrl === null) { this.addDebugMsg('No available endpoint') diff --git a/frontend/src/main.js b/frontend/src/main.js index 2242bbc..88ee381 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -10,6 +10,8 @@ import * as i18n from './i18n' import App from './App' import NotFound from './views/NotFound' +apiBase.init() + if (!process.env.LIB_USE_CDN) { Vue.use(VueRouter) Vue.use(ElementUI) @@ -61,8 +63,6 @@ const router = new VueRouter({ ] }) -await apiBase.init() - new Vue({ render: h => h(App), router,