@milaboratories/pl-client 2.16.11 → 2.16.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/driver.cjs +1 -1
- package/dist/core/driver.cjs.map +1 -1
- package/dist/core/driver.js +1 -1
- package/dist/core/driver.js.map +1 -1
- package/dist/core/errors.cjs +2 -0
- package/dist/core/errors.cjs.map +1 -1
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +2 -0
- package/dist/core/errors.js.map +1 -1
- package/dist/core/ll_client.cjs +32 -9
- package/dist/core/ll_client.cjs.map +1 -1
- package/dist/core/ll_client.d.ts.map +1 -1
- package/dist/core/ll_client.js +32 -9
- package/dist/core/ll_client.js.map +1 -1
- package/dist/core/ll_transaction.cjs +10 -0
- package/dist/core/ll_transaction.cjs.map +1 -1
- package/dist/core/ll_transaction.d.ts +1 -0
- package/dist/core/ll_transaction.d.ts.map +1 -1
- package/dist/core/ll_transaction.js +10 -0
- package/dist/core/ll_transaction.js.map +1 -1
- package/dist/core/websocket_stream.cjs +330 -0
- package/dist/core/websocket_stream.cjs.map +1 -0
- package/dist/core/websocket_stream.d.ts +67 -0
- package/dist/core/websocket_stream.d.ts.map +1 -0
- package/dist/core/websocket_stream.js +328 -0
- package/dist/core/websocket_stream.js.map +1 -0
- package/dist/helpers/retry_strategy.cjs +92 -0
- package/dist/helpers/retry_strategy.cjs.map +1 -0
- package/dist/helpers/retry_strategy.d.ts +24 -0
- package/dist/helpers/retry_strategy.d.ts.map +1 -0
- package/dist/helpers/retry_strategy.js +89 -0
- package/dist/helpers/retry_strategy.js.map +1 -0
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.cjs +136 -0
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.cjs.map +1 -1
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts +75 -1
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts.map +1 -1
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.js +135 -1
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.js.map +1 -1
- package/dist/proto-rest/index.cjs +16 -2
- package/dist/proto-rest/index.cjs.map +1 -1
- package/dist/proto-rest/index.d.ts.map +1 -1
- package/dist/proto-rest/index.js +16 -2
- package/dist/proto-rest/index.js.map +1 -1
- package/package.json +6 -6
- package/src/core/driver.ts +1 -1
- package/src/core/errors.ts +1 -0
- package/src/core/ll_client.ts +42 -9
- package/src/core/ll_transaction.test.ts +18 -0
- package/src/core/ll_transaction.ts +12 -0
- package/src/core/websocket_stream.test.ts +423 -0
- package/src/core/websocket_stream.ts +400 -0
- package/src/helpers/retry_strategy.ts +123 -0
- package/src/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.ts +179 -1
- package/src/proto-rest/index.ts +17 -2
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import { WebSocket, type WebSocketInit, type Dispatcher, ErrorEvent } from 'undici';
|
|
2
|
+
import type { BiDiStream } from './abstract_stream';
|
|
3
|
+
import Denque from 'denque';
|
|
4
|
+
import type { RetryConfig } from '../helpers/retry_strategy';
|
|
5
|
+
import { RetryStrategy } from '../helpers/retry_strategy';
|
|
6
|
+
|
|
7
|
+
interface QueuedMessage<InType extends object> {
|
|
8
|
+
message: InType;
|
|
9
|
+
resolve: () => void;
|
|
10
|
+
reject: (error: Error) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ResponseResolver<OutType extends object> {
|
|
14
|
+
resolve: (value: IteratorResult<OutType>) => void;
|
|
15
|
+
reject: (error: Error) => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
enum ConnectionState {
|
|
19
|
+
NEW = 0,
|
|
20
|
+
CONNECTING = 1,
|
|
21
|
+
CONNECTED = 2,
|
|
22
|
+
CLOSING = 3,
|
|
23
|
+
CLOSED = 4,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type WSStreamOptions<ClientMsg extends object, ServerMsg extends object> = {
|
|
27
|
+
abortSignal?: AbortSignal;
|
|
28
|
+
|
|
29
|
+
dispatcher?: Dispatcher;
|
|
30
|
+
jwtToken?: string;
|
|
31
|
+
retryConfig?: Partial<RetryConfig>;
|
|
32
|
+
|
|
33
|
+
onComplete?: (stream: WebSocketBiDiStream<ClientMsg, ServerMsg>) => void | Promise<void>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* WebSocket-based bidirectional stream implementation for LLTransaction.
|
|
38
|
+
* Implements BiDiStream interface which is compatible with DuplexStreamingCall.
|
|
39
|
+
*/
|
|
40
|
+
export class WebSocketBiDiStream<ClientMsg extends object, ServerMsg extends object> implements BiDiStream<ClientMsg, ServerMsg> {
|
|
41
|
+
// Connection
|
|
42
|
+
private ws: WebSocket | null = null;
|
|
43
|
+
private connectionState: ConnectionState = ConnectionState.NEW;
|
|
44
|
+
private readonly reconnection: RetryStrategy;
|
|
45
|
+
|
|
46
|
+
// Send management
|
|
47
|
+
private readonly sendQueue = new Denque<QueuedMessage<ClientMsg>>();
|
|
48
|
+
private sendCompleted = false;
|
|
49
|
+
private readonly onComplete: (stream: WebSocketBiDiStream<ClientMsg, ServerMsg>) => void | Promise<void>;
|
|
50
|
+
|
|
51
|
+
// Response management
|
|
52
|
+
private readonly responseQueue = new Denque<ServerMsg>();
|
|
53
|
+
private responseResolvers: ResponseResolver<ServerMsg>[] = [];
|
|
54
|
+
|
|
55
|
+
// Error tracking
|
|
56
|
+
private lastError?: Error;
|
|
57
|
+
|
|
58
|
+
// === Public API ===
|
|
59
|
+
|
|
60
|
+
public readonly requests = {
|
|
61
|
+
send: async (message: ClientMsg): Promise<void> => {
|
|
62
|
+
return await this.enqueueSend(message);
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
complete: async (): Promise<void> => {
|
|
66
|
+
if (this.sendCompleted) return;
|
|
67
|
+
|
|
68
|
+
await this.drainSendQueue(); // ensure we sent all already queued messages before closing the stream
|
|
69
|
+
try {
|
|
70
|
+
await this.onComplete(this); // custom onComplete may send additional messages
|
|
71
|
+
} catch (_: unknown) {
|
|
72
|
+
// When 'complete' gets called concurrently with connection break or over a broken
|
|
73
|
+
// transaction stream (server decided it should drop transaction), server would close
|
|
74
|
+
// connection anyway on its end. We can safely ignore error here and just continue working.
|
|
75
|
+
}
|
|
76
|
+
this.sendCompleted = true;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
public readonly responses: AsyncIterable<ServerMsg> = {
|
|
81
|
+
[Symbol.asyncIterator]: () => this.createResponseIterator(),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
public close(): void {
|
|
85
|
+
this.reconnection.cancel();
|
|
86
|
+
|
|
87
|
+
if (this.connectionState < ConnectionState.CONNECTED) {
|
|
88
|
+
// Never reached CONNECTED state. ws.close() will never trigger 'close' event.
|
|
89
|
+
this.ws?.close();
|
|
90
|
+
this.onClose();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!this.progressConnectionState(ConnectionState.CLOSING)) return;
|
|
95
|
+
this.ws!.close();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
constructor(
|
|
99
|
+
private readonly url: string,
|
|
100
|
+
private readonly serializeClientMessage: (message: ClientMsg) => Uint8Array,
|
|
101
|
+
private readonly parseServerMessage: (data: Uint8Array) => ServerMsg,
|
|
102
|
+
private readonly options: WSStreamOptions<ClientMsg, ServerMsg> = {},
|
|
103
|
+
) {
|
|
104
|
+
this.onComplete = this.options.onComplete ?? ((stream) => stream.close());
|
|
105
|
+
|
|
106
|
+
const retryConfig = this.options.retryConfig ?? {};
|
|
107
|
+
this.reconnection = new RetryStrategy(retryConfig, {
|
|
108
|
+
onRetry: () => { void this.connect(); },
|
|
109
|
+
onMaxAttemptsReached: (error) => this.handleError(error),
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
if (this.options.abortSignal?.aborted) {
|
|
113
|
+
this.progressConnectionState(ConnectionState.CLOSED);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
this.options.abortSignal?.addEventListener('abort', () => this.close());
|
|
118
|
+
this.connect();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// === Connection Lifecycle ===
|
|
122
|
+
|
|
123
|
+
private connect(): void {
|
|
124
|
+
if (this.options.abortSignal?.aborted) return;
|
|
125
|
+
|
|
126
|
+
// Prevent reconnecting after first successful connection.
|
|
127
|
+
if (!this.progressConnectionState(ConnectionState.CONNECTING)) return;
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
this.ws = this.createWebSocket();
|
|
131
|
+
|
|
132
|
+
this.ws.addEventListener('open', () => this.onOpen());
|
|
133
|
+
this.ws.addEventListener('message', (event) => this.onMessage(event.data));
|
|
134
|
+
this.ws.addEventListener('error', (error) => this.onError(error));
|
|
135
|
+
this.ws.addEventListener('close', () => this.onClose());
|
|
136
|
+
} catch (error) {
|
|
137
|
+
this.lastError = this.toError(error);
|
|
138
|
+
this.reconnection.schedule();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private createWebSocket(): WebSocket {
|
|
143
|
+
const options: WebSocketInit = {};
|
|
144
|
+
|
|
145
|
+
if (this.options.jwtToken) options.headers = { authorization: `Bearer ${this.options.jwtToken}` };
|
|
146
|
+
if (this.options.dispatcher) options.dispatcher = this.options.dispatcher;
|
|
147
|
+
|
|
148
|
+
const ws = new WebSocket(this.url, options);
|
|
149
|
+
ws.binaryType = 'arraybuffer';
|
|
150
|
+
return ws;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private onOpen(): void {
|
|
154
|
+
this.progressConnectionState(ConnectionState.CONNECTED);
|
|
155
|
+
this.processSendQueue();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private onMessage(data: unknown): void {
|
|
159
|
+
if (!(data instanceof ArrayBuffer)) {
|
|
160
|
+
this.handleError(new Error(`Unexpected WS message format: ${typeof data}`));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
const message = this.parseServerMessage(new Uint8Array(data));
|
|
166
|
+
this.deliverResponse(message);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
this.handleError(this.toError(error));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private onError(error: unknown): void {
|
|
173
|
+
if (this.connectionState < ConnectionState.CONNECTED) {
|
|
174
|
+
// Try to connect several times until we succeed or run out of attempts.
|
|
175
|
+
this.lastError = this.toError(error);
|
|
176
|
+
this.reconnection.schedule();
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.handleError(this.toError(error));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private onClose(): void {
|
|
184
|
+
this.progressConnectionState(ConnectionState.CLOSED);
|
|
185
|
+
|
|
186
|
+
if (!this.lastError) {
|
|
187
|
+
this.rejectAllSendOperations(this.createStreamClosedError());
|
|
188
|
+
this.resolveAllPendingResponses(); // unblock active async iterator
|
|
189
|
+
} else {
|
|
190
|
+
this.rejectAllPendingOperations(this.lastError);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// === Send Queue Management ===
|
|
195
|
+
|
|
196
|
+
private enqueueSend(message: ClientMsg): Promise<void> {
|
|
197
|
+
if (this.sendCompleted) {
|
|
198
|
+
throw new Error('Cannot send: stream already completed');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (this.options.abortSignal?.aborted) {
|
|
202
|
+
throw new Error('Cannot send: stream aborted');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return new Promise<void>((resolve, reject) => {
|
|
206
|
+
this.sendQueue.push({ message, resolve, reject });
|
|
207
|
+
this.processSendQueue();
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private processSendQueue(): void {
|
|
212
|
+
if (!this.canSendMessages()) return;
|
|
213
|
+
|
|
214
|
+
while (this.sendQueue.length > 0) {
|
|
215
|
+
const queued = this.sendQueue.shift()!;
|
|
216
|
+
this.sendQueuedMessage(queued);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private canSendMessages(): boolean {
|
|
221
|
+
return this.connectionState === ConnectionState.CONNECTED;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private sendQueuedMessage(queued: QueuedMessage<ClientMsg>): void {
|
|
225
|
+
try {
|
|
226
|
+
const ws = this.ws;
|
|
227
|
+
if (!ws) {
|
|
228
|
+
throw new Error('WebSocket is not connected');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Check if WebSocket is in a valid state for sending
|
|
232
|
+
if (ws.readyState !== WebSocket.OPEN) {
|
|
233
|
+
throw new Error(`WebSocket is not open (readyState: ${ws.readyState})`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const binary = this.serializeClientMessage(queued.message);
|
|
237
|
+
ws.send(binary);
|
|
238
|
+
queued.resolve();
|
|
239
|
+
} catch (error) {
|
|
240
|
+
queued.reject(this.toError(error));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private async drainSendQueue(): Promise<void> {
|
|
245
|
+
const POLL_INTERVAL_MS = 5;
|
|
246
|
+
|
|
247
|
+
while (this.sendQueue.length > 0) {
|
|
248
|
+
await this.waitForCondition(
|
|
249
|
+
() => this.sendQueue.length === 0,
|
|
250
|
+
POLL_INTERVAL_MS,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private waitForCondition(
|
|
256
|
+
condition: () => boolean,
|
|
257
|
+
intervalMs: number,
|
|
258
|
+
): Promise<void> {
|
|
259
|
+
return new Promise<void>((resolve, reject) => {
|
|
260
|
+
if (this.options.abortSignal?.aborted) {
|
|
261
|
+
return reject(this.toError(this.options.abortSignal.reason) ?? new Error('Stream aborted'));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let timeoutId: ReturnType<typeof setTimeout>;
|
|
265
|
+
const onAbort = () => {
|
|
266
|
+
clearTimeout(timeoutId);
|
|
267
|
+
reject(this.toError(this.options.abortSignal?.reason) ?? new Error('Stream aborted'));
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
this.options.abortSignal?.addEventListener('abort', onAbort, { once: true });
|
|
271
|
+
|
|
272
|
+
const check = () => {
|
|
273
|
+
if (condition() || this.isStreamEnded()) {
|
|
274
|
+
this.options.abortSignal?.removeEventListener('abort', onAbort);
|
|
275
|
+
resolve();
|
|
276
|
+
} else {
|
|
277
|
+
timeoutId = setTimeout(check, intervalMs);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
check();
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// === Response Delivery ===
|
|
286
|
+
|
|
287
|
+
private deliverResponse(message: ServerMsg): void {
|
|
288
|
+
if (this.responseResolvers.length > 0) {
|
|
289
|
+
const resolver = this.responseResolvers.shift()!;
|
|
290
|
+
resolver.resolve({ value: message, done: false });
|
|
291
|
+
} else {
|
|
292
|
+
this.responseQueue.push(message);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private async *createResponseIterator(): AsyncIterator<ServerMsg> {
|
|
297
|
+
while (true) {
|
|
298
|
+
const result = await this.nextResponse();
|
|
299
|
+
|
|
300
|
+
if (result.done) break;
|
|
301
|
+
|
|
302
|
+
yield result.value;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private nextResponse(): Promise<IteratorResult<ServerMsg>> {
|
|
307
|
+
return new Promise<IteratorResult<ServerMsg>>((resolve, reject) => {
|
|
308
|
+
// Fast path: message already available
|
|
309
|
+
if (this.responseQueue.length > 0) {
|
|
310
|
+
const message = this.responseQueue.shift()!;
|
|
311
|
+
resolve({ value: message, done: false });
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Stream ended
|
|
316
|
+
if (this.isStreamEnded()) {
|
|
317
|
+
if (this.lastError) {
|
|
318
|
+
reject(this.lastError);
|
|
319
|
+
} else {
|
|
320
|
+
resolve({ value: undefined as any, done: true });
|
|
321
|
+
}
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Wait for next message
|
|
326
|
+
this.responseResolvers.push({ resolve, reject });
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private resolveAllPendingResponses(): void {
|
|
331
|
+
while (this.responseResolvers.length > 0) {
|
|
332
|
+
const resolver = this.responseResolvers.shift()!;
|
|
333
|
+
resolver.resolve({ value: undefined as any, done: true });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// === Error Handling ===
|
|
338
|
+
|
|
339
|
+
private handleError(error: Error): void {
|
|
340
|
+
this.lastError = error;
|
|
341
|
+
this.close();
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private rejectAllPendingOperations(error: Error): void {
|
|
345
|
+
this.rejectAllSendOperations(error);
|
|
346
|
+
this.rejectAllResponseResolvers(error);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private rejectAllSendOperations(error: Error): void {
|
|
350
|
+
while (this.sendQueue.length > 0) {
|
|
351
|
+
const queued = this.sendQueue.shift()!;
|
|
352
|
+
queued.reject(error);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private rejectAllResponseResolvers(error: Error): void {
|
|
357
|
+
while (this.responseResolvers.length > 0) {
|
|
358
|
+
const resolver = this.responseResolvers.shift()!;
|
|
359
|
+
resolver.reject(error);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private createStreamClosedError(): Error {
|
|
364
|
+
if (this.options.abortSignal?.aborted) {
|
|
365
|
+
const reason = this.options.abortSignal.reason;
|
|
366
|
+
if (reason instanceof Error) {
|
|
367
|
+
return reason;
|
|
368
|
+
}
|
|
369
|
+
return new Error('Stream aborted', { cause: reason });
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return new Error('Stream closed');
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// === Helpers ===
|
|
376
|
+
|
|
377
|
+
private isStreamEnded(): boolean {
|
|
378
|
+
return this.connectionState === ConnectionState.CLOSED || this.options.abortSignal?.aborted || false;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private toError(error: unknown): Error {
|
|
382
|
+
if (error instanceof Error) return error;
|
|
383
|
+
if (error instanceof ErrorEvent) return error.error;
|
|
384
|
+
return new Error(String(error));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Connection state progresses linearly from NEW to CLOSED and never goes back.
|
|
389
|
+
* This internal contract dramatically simplifies the internal stream state management.
|
|
390
|
+
*
|
|
391
|
+
* If you ever feel the need to make this contract less strict, think twice.
|
|
392
|
+
*/
|
|
393
|
+
private progressConnectionState(newState: ConnectionState): boolean {
|
|
394
|
+
if (newState < this.connectionState) {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
this.connectionState = newState;
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
export interface RetryConfig {
|
|
2
|
+
maxAttempts: number;
|
|
3
|
+
initialDelay: number;
|
|
4
|
+
maxDelay: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_RETRY_CONFIG: RetryConfig = {
|
|
8
|
+
maxAttempts: 10,
|
|
9
|
+
initialDelay: 100,
|
|
10
|
+
maxDelay: 30000,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export interface RetryCallbacks {
|
|
14
|
+
onRetry: () => void;
|
|
15
|
+
onMaxAttemptsReached: (error: Error) => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class RetryStrategy {
|
|
19
|
+
private attempts = 0;
|
|
20
|
+
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
21
|
+
private readonly config: RetryConfig;
|
|
22
|
+
private readonly callbacks: RetryCallbacks;
|
|
23
|
+
private readonly backoff: ExponentialBackoff;
|
|
24
|
+
|
|
25
|
+
constructor(config: Partial<RetryConfig>, callbacks: RetryCallbacks) {
|
|
26
|
+
this.config = { ...DEFAULT_RETRY_CONFIG, ...config };
|
|
27
|
+
this.callbacks = callbacks;
|
|
28
|
+
this.backoff = new ExponentialBackoff({
|
|
29
|
+
initialDelay: this.config.initialDelay,
|
|
30
|
+
maxDelay: this.config.maxDelay,
|
|
31
|
+
factor: 2,
|
|
32
|
+
jitter: 0.1,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
schedule(): void {
|
|
37
|
+
if (this.timer) return;
|
|
38
|
+
if (this.hasExceededLimit()) {
|
|
39
|
+
this.notifyMaxAttemptsReached();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.timer = setTimeout(() => {
|
|
44
|
+
this.timer = null;
|
|
45
|
+
this.attempts++;
|
|
46
|
+
this.callbacks.onRetry();
|
|
47
|
+
}, this.backoff.delay());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
cancel(): void {
|
|
51
|
+
if (this.timer) {
|
|
52
|
+
clearTimeout(this.timer);
|
|
53
|
+
this.timer = null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
reset(): void {
|
|
58
|
+
this.attempts = 0;
|
|
59
|
+
this.backoff.reset();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private hasExceededLimit(): boolean {
|
|
63
|
+
return this.attempts >= this.config.maxAttempts;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private notifyMaxAttemptsReached(): void {
|
|
67
|
+
const error = new Error(
|
|
68
|
+
`Max retry attempts (${this.config.maxAttempts}) reached`,
|
|
69
|
+
);
|
|
70
|
+
this.callbacks.onMaxAttemptsReached(error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface ExponentialBackoffConfig {
|
|
75
|
+
initialDelay: number;
|
|
76
|
+
maxDelay: number;
|
|
77
|
+
factor: number;
|
|
78
|
+
jitter: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class ExponentialBackoff {
|
|
82
|
+
private readonly initialDelay: number;
|
|
83
|
+
private readonly maxDelay: number;
|
|
84
|
+
|
|
85
|
+
private currentDelay: number;
|
|
86
|
+
|
|
87
|
+
private readonly factor: number;
|
|
88
|
+
private readonly jitter: number;
|
|
89
|
+
|
|
90
|
+
constructor(config: ExponentialBackoffConfig) {
|
|
91
|
+
this.initialDelay = config.initialDelay;
|
|
92
|
+
this.maxDelay = config.maxDelay;
|
|
93
|
+
this.factor = config.factor;
|
|
94
|
+
this.jitter = config.jitter;
|
|
95
|
+
this.currentDelay = config.initialDelay;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
delay(): number {
|
|
99
|
+
if (this.currentDelay >= this.maxDelay) {
|
|
100
|
+
return this.applyJitter(this.maxDelay);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
this.currentDelay = this.currentDelay * this.factor;
|
|
104
|
+
|
|
105
|
+
if (this.currentDelay > this.maxDelay) {
|
|
106
|
+
this.currentDelay = this.maxDelay;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return this.applyJitter(this.currentDelay);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
reset(): void {
|
|
113
|
+
this.currentDelay = this.initialDelay;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private applyJitter(delay: number): number {
|
|
117
|
+
if (delay === 0 || this.jitter === 0) {
|
|
118
|
+
return delay;
|
|
119
|
+
}
|
|
120
|
+
const delayFactor = 1 - (this.jitter / 2) + Math.random() * this.jitter;
|
|
121
|
+
return delay * delayFactor;
|
|
122
|
+
}
|
|
123
|
+
}
|