forked from oracle/javavscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetryRetry.ts
More file actions
138 lines (115 loc) · 5.06 KB
/
telemetryRetry.ts
File metadata and controls
138 lines (115 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Copyright (c) 2024, Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { LOGGER } from "../../logger";
import { TelemetryConfiguration } from "../config";
import { BaseEvent } from "../events/baseEvent";
import { TelemetryPostResponse } from "./postTelemetry";
export class TelemetryRetry {
private TELEMETRY_RETRY_CONFIG = TelemetryConfiguration.getInstance().getRetryConfig();
private timePeriod: number = this.TELEMETRY_RETRY_CONFIG?.baseTimer;
private timeout?: NodeJS.Timeout | null;
private numOfAttemptsWhenTimerHits: number = 1;
private queueCapacity: number = this.TELEMETRY_RETRY_CONFIG?.baseCapacity;
private numOfAttemptsWhenQueueIsFull: number = 1;
private triggeredDueToQueueOverflow: boolean = false;
private callbackHandler?: () => {};
public registerCallbackHandler = (callbackHandler: () => {}): void => {
this.callbackHandler = callbackHandler;
}
public startTimer = (): void => {
if (!this.callbackHandler) {
LOGGER.debug("Callback handler is not set for telemetry retry mechanism");
return;
}
if (this.timeout) {
LOGGER.debug("Overriding current timeout");
}
this.timeout = setInterval(this.callbackHandler, this.timePeriod);
}
private resetTimerParameters = () => {
this.numOfAttemptsWhenTimerHits = 1;
this.timePeriod = this.TELEMETRY_RETRY_CONFIG.baseTimer;
this.clearTimer();
}
private increaseTimePeriod = (): void => {
if (this.numOfAttemptsWhenTimerHits <= this.TELEMETRY_RETRY_CONFIG.maxRetries) {
this.timePeriod = this.calculateDelay();
this.numOfAttemptsWhenTimerHits++;
return;
}
throw new Error("Number of retries exceeded");
}
public clearTimer = (): void => {
if (this.timeout) {
clearInterval(this.timeout);
this.timeout = null;
}
}
private calculateDelay = (): number => {
const baseDelay = this.TELEMETRY_RETRY_CONFIG.baseTimer *
Math.pow(this.TELEMETRY_RETRY_CONFIG.backoffFactor, this.numOfAttemptsWhenTimerHits);
const cappedDelay = Math.min(baseDelay, this.TELEMETRY_RETRY_CONFIG.maxDelayMs);
const jitterMultiplier = 1 + (Math.random() * 2 - 1) * this.TELEMETRY_RETRY_CONFIG.jitterFactor;
return Math.floor(cappedDelay * jitterMultiplier);
};
private increaseQueueCapacity = (): void => {
if (this.numOfAttemptsWhenQueueIsFull < this.TELEMETRY_RETRY_CONFIG.maxRetries) {
this.queueCapacity = this.TELEMETRY_RETRY_CONFIG.baseCapacity *
Math.pow(this.TELEMETRY_RETRY_CONFIG.backoffFactor, this.numOfAttemptsWhenQueueIsFull);
}
throw new Error("Number of retries exceeded");
}
private resetQueueCapacity = (): void => {
this.queueCapacity = this.TELEMETRY_RETRY_CONFIG.baseCapacity;
this.numOfAttemptsWhenQueueIsFull = 1;
this.triggeredDueToQueueOverflow = false;
}
public isQueueOverflow = (queueSize: number): boolean => {
if (queueSize >= this.queueCapacity) {
this.triggeredDueToQueueOverflow = true;
return true;
}
return false;
}
private isEventRetryable = (statusCode: number): boolean => {
return statusCode <= 0 || statusCode > 500 || statusCode == 429;
}
public eventsToBeEnqueuedAgain = (eventResponses: TelemetryPostResponse): BaseEvent<any>[] => {
eventResponses.success.forEach(res => {
res.event.onSuccessPostEventCallback();
});
if (eventResponses.failures.length === 0) {
this.resetQueueCapacity();
this.resetTimerParameters();
} else {
const eventsToBeEnqueuedAgain: BaseEvent<any>[] = [];
eventResponses.failures.forEach((eventRes) => {
if (this.isEventRetryable(eventRes.statusCode))
eventsToBeEnqueuedAgain.push(eventRes.event);
});
if (eventsToBeEnqueuedAgain.length) {
this.triggeredDueToQueueOverflow ?
this.increaseQueueCapacity() :
this.increaseTimePeriod();
LOGGER.debug(`Queue max capacity size: ${this.queueCapacity}`);
LOGGER.debug(`Timer period: ${this.timePeriod}`);
} else {
eventResponses.failures.forEach(res => {
res.event.onFailPostEventCallback();
});
}
return eventsToBeEnqueuedAgain;
}
return [];
}
}