@yarkivaev/scada 2.12.1 → 2.12.2
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/package.json
CHANGED
|
@@ -8,6 +8,7 @@ export default function queuedDelivery(env, secrets, supplied) {
|
|
|
8
8
|
let burst = 0;
|
|
9
9
|
const queue = supplied ?? (env.COLLECTOR_SPOOL_DIR ? deliveryQueue(env.COLLECTOR_SPOOL_DIR, {
|
|
10
10
|
maxBytes: Number(env.COLLECTOR_SPOOL_MAX_BYTES), maxRecords: Number(env.COLLECTOR_SPOOL_MAX_RECORDS ?? 100000), factory(spec) { return sinks().get(spec.type)(spec, { secrets }); },
|
|
11
|
+
walCheckpoint: env.COLLECTOR_SPOOL_WAL_CHECKPOINT === 'managed' ? {} : undefined,
|
|
11
12
|
onError(error) { process.stderr.write(`Collector delivery delayed: ${error.message}\n`); }
|
|
12
13
|
}) : undefined);
|
|
13
14
|
function nextDelay(result) {
|
|
@@ -4,13 +4,16 @@ import { pendingFair } from './spoolReplay.js';
|
|
|
4
4
|
import spoolMutations from './spoolMutations.js';
|
|
5
5
|
import spoolMigration from './spoolMigration.js';
|
|
6
6
|
import spoolLimits from './spoolLimits.js';
|
|
7
|
+
import spoolCheckpoints from './spoolCheckpoints.js';
|
|
7
8
|
|
|
8
9
|
/* eslint max-lines-per-function: ["error", 100], max-statements: ["error", 25] */
|
|
9
10
|
/** Single owner, bounded cache and indexed reads. Caps count logical JSON bytes, not allocated pages. */
|
|
10
11
|
export default function deliverySpool(directory, options) {
|
|
11
|
-
const { maxBytes, maxRecords, legacyMigrationMaxBytes } = spoolLimits(options);
|
|
12
|
+
const { maxBytes, maxRecords, legacyMigrationMaxBytes, walCheckpoint } = spoolLimits(options);
|
|
12
13
|
const state = { db: null, owner: null, ready: false, busy: false, bytes: 0, records: 0 };
|
|
14
|
+
let maintenance;
|
|
13
15
|
function release() {
|
|
16
|
+
maintenance?.cancel(); maintenance = undefined;
|
|
14
17
|
state.ready = false;
|
|
15
18
|
try { state.db?.close(); } finally { state.owner?.close(); state.db = null; state.owner = null; }
|
|
16
19
|
}
|
|
@@ -24,7 +27,12 @@ export default function deliverySpool(directory, options) {
|
|
|
24
27
|
try { return await action(); } finally { Object.assign(state, { busy:false }); }
|
|
25
28
|
}
|
|
26
29
|
function commit(action) {
|
|
27
|
-
try {
|
|
30
|
+
try {
|
|
31
|
+
maintenance?.beforeWrite();
|
|
32
|
+
const result = state.db.transaction(action)();
|
|
33
|
+
refresh(); maintenance?.schedule();
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
28
36
|
catch (error) { if (error.code?.startsWith('SQLITE_')) { release(); } throw error; }
|
|
29
37
|
}
|
|
30
38
|
function mutateMany(operations) {
|
|
@@ -43,6 +51,7 @@ export default function deliverySpool(directory, options) {
|
|
|
43
51
|
Object.assign(state, await database(directory));
|
|
44
52
|
capacity(state.db, maxBytes, maxRecords);
|
|
45
53
|
await spoolMigration(directory, state.db, { maxBytes, maxRecords, legacyMigrationMaxBytes });
|
|
54
|
+
if (walCheckpoint) { maintenance = spoolCheckpoints(state.db, directory, walCheckpoint); maintenance.schedule(); }
|
|
46
55
|
refresh(); Object.assign(state, { ready:true });
|
|
47
56
|
} catch (error) { release(); throw error; }
|
|
48
57
|
}, false);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { statSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
const idleMs = 150;
|
|
5
|
+
const maxDelayMs = 2000;
|
|
6
|
+
|
|
7
|
+
export function spoolCheckpointLimits(options) {
|
|
8
|
+
if (options === undefined) { return undefined; }
|
|
9
|
+
if (!options || typeof options !== 'object' || Array.isArray(options)) { throw new TypeError('Invalid spool WAL checkpoint options'); }
|
|
10
|
+
const { targetBytes = 2 * 1024 * 1024, maxBytes = 8 * 1024 * 1024 } = options;
|
|
11
|
+
if (!Number.isSafeInteger(targetBytes) || targetBytes <= 0 || !Number.isSafeInteger(maxBytes) || maxBytes <= targetBytes) {
|
|
12
|
+
throw new TypeError('Spool WAL limit must be greater than its positive checkpoint target');
|
|
13
|
+
}
|
|
14
|
+
return { targetBytes, maxBytes };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default function spoolCheckpoints(db, directory, { targetBytes, maxBytes }) {
|
|
18
|
+
const file = path.join(directory, 'delivery.sqlite-wal');
|
|
19
|
+
db.pragma('wal_autocheckpoint = 0');
|
|
20
|
+
let timer;
|
|
21
|
+
let deadline;
|
|
22
|
+
let failure;
|
|
23
|
+
|
|
24
|
+
function bytes() { return statSync(file, { throwIfNoEntry: false })?.size ?? 0; }
|
|
25
|
+
function cancel() {
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
timer = undefined;
|
|
28
|
+
deadline = undefined;
|
|
29
|
+
}
|
|
30
|
+
function checkpoint(mode) { return db.pragma(`wal_checkpoint(${mode})`)[0]; }
|
|
31
|
+
function maintenance() {
|
|
32
|
+
cancel();
|
|
33
|
+
try {
|
|
34
|
+
const size = bytes();
|
|
35
|
+
if (size >= maxBytes) { checkpoint('TRUNCATE'); }
|
|
36
|
+
else if (size >= targetBytes) { checkpoint('PASSIVE'); }
|
|
37
|
+
} catch (error) { failure = error; }
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
beforeWrite() {
|
|
41
|
+
if (failure) {
|
|
42
|
+
const error = failure;
|
|
43
|
+
failure = undefined;
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
// A durable group may cross the limit. A later writer cannot extend that WAL.
|
|
47
|
+
if (bytes() < maxBytes) { return; }
|
|
48
|
+
cancel();
|
|
49
|
+
if (checkpoint('TRUNCATE').busy !== 0) { throw new Error('Spool WAL growth limit reached while checkpoint is busy'); }
|
|
50
|
+
},
|
|
51
|
+
schedule() {
|
|
52
|
+
try {
|
|
53
|
+
if (bytes() < targetBytes) { cancel(); return; }
|
|
54
|
+
const now = performance.now();
|
|
55
|
+
deadline ??= now + maxDelayMs;
|
|
56
|
+
// Keep the timer runnable under sustained writes once its maximum deferral expires.
|
|
57
|
+
if (timer && now >= deadline) { return; }
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
timer = setTimeout(maintenance, Math.min(idleMs, deadline - now));
|
|
60
|
+
timer.unref();
|
|
61
|
+
} catch (error) { failure = error; }
|
|
62
|
+
},
|
|
63
|
+
cancel
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { spoolCheckpointLimits } from './spoolCheckpoints.js';
|
|
2
|
+
|
|
3
|
+
export default function spoolLimits({ maxBytes, maxRecords = 100000, legacyMigrationMaxBytes = 16 * 1024 * 1024, walCheckpoint } = {}) {
|
|
2
4
|
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) { throw new TypeError('Explicit positive spool maxBytes required'); }
|
|
3
5
|
if (!Number.isSafeInteger(maxRecords) || maxRecords <= 0) { throw new TypeError('Positive safe spool maxRecords required'); }
|
|
4
6
|
if (!Number.isSafeInteger(legacyMigrationMaxBytes) || legacyMigrationMaxBytes <= 0) { throw new TypeError('Positive safe legacyMigrationMaxBytes required'); }
|
|
5
|
-
return { maxBytes, maxRecords, legacyMigrationMaxBytes };
|
|
7
|
+
return { maxBytes, maxRecords, legacyMigrationMaxBytes, walCheckpoint: spoolCheckpointLimits(walCheckpoint) };
|
|
6
8
|
}
|