Skip to content

Commit

Permalink
πŸ”‰ [RUMF-1351] retry: add extra context to queue full log (#1714)
Browse files Browse the repository at this point in the history
* πŸ”‰ [RUMF-1351] retry: add extra context to queue full log

* πŸ‘Œ add endpoint type
  • Loading branch information
bcaudan authored Aug 31, 2022
1 parent cc58e11 commit 1754446
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/domain/configuration/endpointBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const INTAKE_TRACKS = {
sessionReplay: 'replay',
}

type EndpointType = keyof typeof ENDPOINTS
export type EndpointType = keyof typeof ENDPOINTS

export type EndpointBuilder = ReturnType<typeof createEndpointBuilder>

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/domain/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export {
DefaultPrivacyLevel,
validateAndBuildConfiguration,
} from './configuration'
export { createEndpointBuilder, EndpointBuilder } from './endpointBuilder'
export { createEndpointBuilder, EndpointBuilder, EndpointType } from './endpointBuilder'
export {
isExperimentalFeatureEnabled,
updateExperimentalFeatures,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/transport/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function createHttpRequest(endpointBuilder: EndpointBuilder, bytesLimit:
if (!isExperimentalFeatureEnabled('retry')) {
fetchKeepAliveStrategy(endpointBuilder, bytesLimit, payload)
} else {
sendWithRetryStrategy(payload, retryState, sendStrategyForRetry)
sendWithRetryStrategy(payload, retryState, sendStrategyForRetry, endpointBuilder.endpointType)
}
},
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/transport/sendWithRetryStrategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('sendWithRetryStrategy', () => {
data: payload?.data ?? 'a',
bytesCount: payload?.bytesCount ?? 1,
}
sendWithRetryStrategy(effectivePayload, state, sendStub.sendStrategy)
sendWithRetryStrategy(effectivePayload, state, sendStub.sendStrategy, 'logs')
}
})

Expand Down
32 changes: 23 additions & 9 deletions packages/core/src/transport/sendWithRetryStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { addTelemetryDebug } from '../domain/telemetry'
import type { EndpointType } from '../domain/configuration'
import { monitor } from '../tools/monitor'
import { ONE_KIBI_BYTE, ONE_MEBI_BYTE, ONE_SECOND } from '../tools/utils'
import type { Payload, HttpResponse } from './httpRequest'
Expand All @@ -25,25 +26,30 @@ export interface RetryState {

type SendStrategy = (payload: Payload, onResponse: (r: HttpResponse) => void) => void

export function sendWithRetryStrategy(payload: Payload, state: RetryState, sendStrategy: SendStrategy) {
export function sendWithRetryStrategy(
payload: Payload,
state: RetryState,
sendStrategy: SendStrategy,
endpointType: EndpointType
) {
if (
state.transportStatus === TransportStatus.UP &&
state.queuedPayloads.size() === 0 &&
state.bandwidthMonitor.canHandle(payload)
) {
send(payload, state, sendStrategy, {
onSuccess: () => retryQueuedPayloads(state, sendStrategy),
onSuccess: () => retryQueuedPayloads(state, sendStrategy, 'request success', endpointType),
onFailure: () => {
state.queuedPayloads.enqueue(payload)
scheduleRetry(state, sendStrategy)
scheduleRetry(state, sendStrategy, endpointType)
},
})
} else {
state.queuedPayloads.enqueue(payload)
}
}

function scheduleRetry(state: RetryState, sendStrategy: SendStrategy) {
function scheduleRetry(state: RetryState, sendStrategy: SendStrategy, endpointType: EndpointType) {
if (state.transportStatus !== TransportStatus.DOWN) {
return
}
Expand All @@ -62,11 +68,11 @@ function scheduleRetry(state: RetryState, sendStrategy: SendStrategy) {
})
}
state.currentBackoffTime = INITIAL_BACKOFF_TIME
retryQueuedPayloads(state, sendStrategy)
retryQueuedPayloads(state, sendStrategy, 'transport down', endpointType)
},
onFailure: () => {
state.currentBackoffTime = Math.min(MAX_BACKOFF_TIME, state.currentBackoffTime * 2)
scheduleRetry(state, sendStrategy)
scheduleRetry(state, sendStrategy, endpointType)
},
})
}),
Expand Down Expand Up @@ -96,14 +102,22 @@ function send(
})
}

function retryQueuedPayloads(state: RetryState, sendStrategy: SendStrategy) {
function retryQueuedPayloads(
state: RetryState,
sendStrategy: SendStrategy,
reason: string,
endpointType: EndpointType
) {
const previousQueue = state.queuedPayloads
if (previousQueue.isFull()) {
addTelemetryDebug('retry queue full')
addTelemetryDebug('retry queue full', {
reason,
endpointType,
})
}
state.queuedPayloads = newPayloadQueue()
while (previousQueue.size() > 0) {
sendWithRetryStrategy(previousQueue.dequeue()!, state, sendStrategy)
sendWithRetryStrategy(previousQueue.dequeue()!, state, sendStrategy, endpointType)
}
}

Expand Down

0 comments on commit 1754446

Please sign in to comment.