From 2df556419a749063b1d20069d63e2596e51fd5b8 Mon Sep 17 00:00:00 2001 From: Harjot Gill Date: Sat, 26 Sep 2026 14:33:16 -0700 Subject: [PATCH] perf(mfe): remove standalone five-query throttle from host proxy --- .../core/services/FetchQueueWorker.test.ts | 35 +++++++++++++++++-- public/app/core/services/FetchQueueWorker.ts | 14 ++++++-- public/app/core/services/backend_srv.ts | 2 +- public/app/core/specs/backend_srv.test.ts | 20 ++++++++++- 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/public/app/core/services/FetchQueueWorker.test.ts b/public/app/core/services/FetchQueueWorker.test.ts index c261cce698acc..a9767d31eb7b0 100644 --- a/public/app/core/services/FetchQueueWorker.test.ts +++ b/public/app/core/services/FetchQueueWorker.test.ts @@ -6,7 +6,7 @@ import { FetchQueue, FetchQueueUpdate, FetchStatus } from './FetchQueue'; import { FetchQueueWorker } from './FetchQueueWorker'; import { ResponseQueue } from './ResponseQueue'; -const getTestContext = (http2Enabled = false) => { +const getTestContext = (http2Enabled = false, isHostProxied = () => false) => { const config: GrafanaBootConfig = { http2Enabled } as unknown as GrafanaBootConfig; const dataUrl = 'http://localhost:3000/api/ds/query?=abc'; const apiUrl = 'http://localhost:3000/api/alerts?state=all'; @@ -25,12 +25,43 @@ const getTestContext = (http2Enabled = false) => { getResponses: jest.fn(), } as unknown as ResponseQueue; - new FetchQueueWorker(queueMock, responseQueueMock, config); + new FetchQueueWorker(queueMock, responseQueueMock, config, isHostProxied); return { dataUrl, apiUrl, updates, queueMock, addMock }; }; describe('FetchQueueWorker', () => { + it('uses host-proxy concurrency even when enabled after singleton construction', () => { + let hostProxied = false; + const { updates, addMock, dataUrl } = getTestContext(false, () => hostProxied); + const state = Object.fromEntries( + Array.from({ length: 22 }, (_, index) => [ + `panel-${index}`, + { state: FetchStatus.Pending, options: { url: dataUrl } }, + ]) + ); + hostProxied = true; + updates.next({ noOfPending: 22, noOfInProgress: 5, state }); + expect(addMock).toHaveBeenCalledTimes(22); + }); + + it('retains API precedence and the existing multiplexed ceiling for host-proxied requests', () => { + const { updates, addMock, dataUrl, apiUrl } = getTestContext(false, () => true); + updates.next({ + noOfPending: 3, + noOfInProgress: 998, + state: { + first: { state: FetchStatus.Pending, options: { url: dataUrl } }, + second: { state: FetchStatus.Pending, options: { url: dataUrl } }, + api: { state: FetchStatus.Pending, options: { url: apiUrl } }, + }, + }); + expect(addMock.mock.calls).toEqual([ + ['api', { url: apiUrl }], + ['first', { url: dataUrl }], + ]); + }); + describe('when an update is pushed in the stream', () => { describe('and queue has no pending entries', () => { it('then nothing should be added to the responseQueue', () => { diff --git a/public/app/core/services/FetchQueueWorker.ts b/public/app/core/services/FetchQueueWorker.ts index 5a4c4794c4914..6457654c4b0a0 100644 --- a/public/app/core/services/FetchQueueWorker.ts +++ b/public/app/core/services/FetchQueueWorker.ts @@ -13,9 +13,12 @@ interface WorkerEntry { } export class FetchQueueWorker { - constructor(fetchQueue: FetchQueue, responseQueue: ResponseQueue, config: GrafanaBootConfig) { - const maxParallelRequests = config?.http2Enabled ? 1000 : 5; // for tests that don't mock GrafanaBootConfig the config param will be undefined - + constructor( + fetchQueue: FetchQueue, + responseQueue: ResponseQueue, + config: GrafanaBootConfig, + isHostProxied: () => boolean = () => false + ) { // This will create an implicit live subscription for as long as this class lives. // But as FetchQueueWorker is used by the singleton backendSrv that also lives for as long as Grafana app lives // I think this ok. We could add some disposable pattern later if the need arises. @@ -26,6 +29,11 @@ export class FetchQueueWorker { // Using concatMap instead of mergeMap so that the order with apiRequests first is preserved // https://rxjs.dev/api/operators/concatMap concatMap(({ state, noOfInProgress }) => { + // The embedded app sends queries through the host's authenticated proxy, + // not Grafana's server transport. Its minimal boot config has no HTTP/2 + // flag. Do not serialize those queries behind the standalone HTTP/1 cap. + // Read the mode here: the singleton is constructed before MFE startup. + const maxParallelRequests = config?.http2Enabled || isHostProxied() ? 1000 : 5; const apiRequests = Object.keys(state) .filter((k) => state[k].state === FetchStatus.Pending && !isDataQuery(state[k].options.url)) .reduce((all, key) => { diff --git a/public/app/core/services/backend_srv.ts b/public/app/core/services/backend_srv.ts index 83cbcb416b3b5..0df71a817bc04 100644 --- a/public/app/core/services/backend_srv.ts +++ b/public/app/core/services/backend_srv.ts @@ -92,7 +92,7 @@ export class BackendSrv implements BackendService { this.initGrafanaDeviceID(); - new FetchQueueWorker(this.fetchQueue, this.responseQueue, getConfig()); + new FetchQueueWorker(this.fetchQueue, this.responseQueue, getConfig(), () => this.grafanaPrefix); } private async initGrafanaDeviceID() { diff --git a/public/app/core/specs/backend_srv.test.ts b/public/app/core/specs/backend_srv.test.ts index ac96cfc2572d0..ff4f79b074b46 100644 --- a/public/app/core/specs/backend_srv.test.ts +++ b/public/app/core/specs/backend_srv.test.ts @@ -1,4 +1,4 @@ -import { Observable, of, lastValueFrom } from 'rxjs'; +import { Observable, of, lastValueFrom, NEVER } from 'rxjs'; import { fromFetch } from 'rxjs/fetch'; import { delay } from 'rxjs/operators'; @@ -90,7 +90,25 @@ jest.mock('app/core/utils/auth', () => ({ hasSessionExpiry: () => true, })); +// Backend transport tests do not exercise dashboard persistence. Loading that +// adapter also initializes the complete dashboard scene/store module graph. +jest.mock('app/features/dashboard/api/dashboard_api', () => ({ getDashboardAPI: jest.fn() })); + describe('backendSrv', () => { + it.each([false, true])('dispatches dashboard queries with host proxy mode %s', (hostProxied) => { + const { backendSrv, fromFetchMock } = getTestContext(); + fromFetchMock.mockReturnValue(NEVER); + backendSrv.setGrafanaPrefix(hostProxied); + const subscriptions = Array.from({ length: 22 }, (_, panel) => + backendSrv.fetch({ url: '/api/ds/query', method: 'POST', data: { panel } }).subscribe() + ); + try { + expect(fromFetchMock).toHaveBeenCalledTimes(hostProxied ? 22 : 5); + } finally { + subscriptions.forEach((subscription) => subscription.unsubscribe()); + } + }); + describe('parseRequestOptions', () => { it.each` retry | url | headers | orgId | noBackendCache | expected