-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbatch-buffer.ts
More file actions
99 lines (88 loc) · 2.71 KB
/
batch-buffer.ts
File metadata and controls
99 lines (88 loc) · 2.71 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
import { BATCH_INTERVAL_MS, BATCH_MAX_SIZE } from "./config";
import { BatchBufferOptions, Logger } from "./types";
import { isObject, ok } from "./utils";
/**
* A buffer that accumulates items and flushes them in batches.
* @typeparam T - The type of items to buffer.
*/
export default class BatchBuffer<T> {
private buffer: T[] = [];
private flushHandler: (items: T[]) => Promise<void>;
private logger?: Logger;
private maxSize: number;
private intervalMs: number;
private timer: NodeJS.Timeout | null = null;
/**
* Creates a new `BatchBuffer` instance.
* @param options - The options to configure the buffer.
* @throws If the options are invalid.
*/
constructor(options: BatchBufferOptions<T>) {
ok(isObject(options), "options must be an object");
ok(
typeof options.flushHandler === "function",
"flushHandler must be a function",
);
ok(isObject(options.logger) || !options.logger, "logger must be an object");
ok(
(typeof options.maxSize === "number" && options.maxSize > 0) ||
typeof options.maxSize !== "number",
"maxSize must be greater than 0",
);
ok(
(typeof options.intervalMs === "number" && options.intervalMs >= 0) ||
typeof options.intervalMs !== "number",
"intervalMs must be greater than or equal to 0",
);
this.flushHandler = options.flushHandler;
this.logger = options.logger;
this.maxSize = options.maxSize ?? BATCH_MAX_SIZE;
this.intervalMs = options.intervalMs ?? BATCH_INTERVAL_MS;
}
/**
* Adds an item to the buffer.
*
* @param item - The item to add.
*/
public async add(item: T) {
this.buffer.push(item);
if (this.buffer.length >= this.maxSize) {
await this.flush();
} else if (!this.timer && this.intervalMs > 0) {
this.timer = setTimeout(() => this.flush(), this.intervalMs).unref();
}
}
public async flush(): Promise<void> {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
if (this.buffer.length === 0) {
this.logger?.debug("buffer is empty. nothing to flush");
return;
}
const flushingBuffer = this.buffer;
this.buffer = [];
try {
await this.flushHandler(flushingBuffer);
this.logger?.info("flushed buffered items", {
count: flushingBuffer.length,
});
} catch (error) {
this.logger?.warn("flush of buffered items failed; discarding items", {
error,
count: flushingBuffer.length,
});
}
}
/**
* Destroys the buffer, clearing any pending timer and discarding buffered items.
*/
public destroy(): void {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
this.buffer = [];
}
}