Skip to content

Commit 179593e

Browse files
committed
✨ Add AVOID_FETCH_KEEPALIVE experimental feature and update request handling
1 parent 3c18298 commit 179593e

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

packages/core/src/tools/experimentalFeatures.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export enum ExperimentalFeature {
1818
TRACK_INTAKE_REQUESTS = 'track_intake_requests',
1919
USER_ACCOUNT_TRACE_HEADER = 'user_account_trace_header',
2020
WRITABLE_RESOURCE_GRAPHQL = 'writable_resource_graphql',
21+
AVOID_FETCH_KEEPALIVE = 'avoid_fetch_keepalive',
2122
}
2223

2324
const enabledExperimentalFeatures: Set<ExperimentalFeature> = new Set()

packages/core/src/transport/httpRequest.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import type { Request } from '../../test'
1010
import type { EndpointBuilder } from '../domain/configuration'
1111
import { createEndpointBuilder } from '../domain/configuration'
12+
import { addExperimentalFeatures, resetExperimentalFeatures, ExperimentalFeature } from '../tools/experimentalFeatures'
1213
import { noop } from '../tools/utils/functionUtils'
1314
import { createHttpRequest, fetchKeepAliveStrategy, fetchStrategy } from './httpRequest'
1415
import type { HttpRequest } from './httpRequest'
@@ -263,3 +264,48 @@ describe('httpRequest intake parameters', () => {
263264
expect(requests.length).toEqual(2)
264265
})
265266
})
267+
268+
describe('httpRequest with FETCH_KEEPALIVE_EXIT_ONLY feature flag', () => {
269+
const BATCH_BYTES_LIMIT = 100
270+
const ENDPOINT_URL = 'http://my.website'
271+
let interceptor: ReturnType<typeof interceptRequests>
272+
let requests: Request[]
273+
let endpointBuilder: EndpointBuilder
274+
let request: HttpRequest
275+
276+
beforeEach(() => {
277+
interceptor = interceptRequests()
278+
requests = interceptor.requests
279+
endpointBuilder = mockEndpointBuilder(ENDPOINT_URL)
280+
})
281+
282+
afterEach(() => {
283+
resetExperimentalFeatures()
284+
})
285+
286+
it('should use regular fetch (without keepalive) when feature flag is enabled', async () => {
287+
addExperimentalFeatures([ExperimentalFeature.AVOID_FETCH_KEEPALIVE])
288+
request = createHttpRequest(endpointBuilder, BATCH_BYTES_LIMIT, noop)
289+
290+
request.send({ data: '{"foo":"bar"}', bytesCount: 10 })
291+
await interceptor.waitForAllFetchCalls()
292+
293+
expect(requests.length).toEqual(1)
294+
expect(requests[0].type).toBe('fetch')
295+
expect(requests[0].url).toContain(ENDPOINT_URL)
296+
})
297+
298+
it('should use fetch keepalive when feature flag is not enabled', async () => {
299+
if (!interceptor.isFetchKeepAliveSupported()) {
300+
pending('no fetch keepalive support')
301+
}
302+
303+
request = createHttpRequest(endpointBuilder, BATCH_BYTES_LIMIT, noop)
304+
305+
request.send({ data: '{"foo":"bar"}', bytesCount: 10 })
306+
await interceptor.waitForAllFetchCalls()
307+
308+
expect(requests.length).toEqual(1)
309+
expect(requests[0].type).toBe('fetch-keepalive')
310+
})
311+
})

packages/core/src/transport/httpRequest.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { EndpointBuilder } from '../domain/configuration'
22
import type { Context } from '../tools/serialisation/context'
33
import { monitor, monitorError } from '../tools/monitor'
44
import type { RawError } from '../domain/error/error.types'
5+
import { isExperimentalFeatureEnabled, ExperimentalFeature } from '../tools/experimentalFeatures'
56
import { newRetryState, sendWithRetryStrategy } from './sendWithRetryStrategy'
67

78
/**
@@ -38,8 +39,13 @@ export function createHttpRequest(
3839
reportError: (error: RawError) => void
3940
) {
4041
const retryState = newRetryState()
41-
const sendStrategyForRetry = (payload: Payload, onResponse: (r: HttpResponse) => void) =>
42-
fetchKeepAliveStrategy(endpointBuilder, bytesLimit, payload, onResponse)
42+
const sendStrategyForRetry = (payload: Payload, onResponse: (r: HttpResponse) => void) => {
43+
if (isExperimentalFeatureEnabled(ExperimentalFeature.AVOID_FETCH_KEEPALIVE)) {
44+
fetchStrategy(endpointBuilder, payload, onResponse)
45+
} else {
46+
fetchKeepAliveStrategy(endpointBuilder, bytesLimit, payload, onResponse)
47+
}
48+
}
4349

4450
return {
4551
send: (payload: Payload) => {

0 commit comments

Comments
 (0)