sqlite-executor 4.0.2 → 4.0.3

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.
@@ -12,6 +12,8 @@ export interface PipelineEngineOptions {
12
12
  logger?: { error?: (...args: any[]) => void };
13
13
  /** 管线化批量大小,控制一次 stdin.write 合并几条语句,默认 10 */
14
14
  batchSize?: number;
15
+ /** 最大 inflight 任务数,默认 50 */
16
+ maxInflight?: number;
15
17
  /** 任务超时时的回调 */
16
18
  onTaskTimeout?: (task: any) => void;
17
19
  }
@@ -11,8 +11,14 @@ export declare class ProcessManager {
11
11
  * @param options.initMode 初始化模式
12
12
  * - `"wal"` (默认):启动时通过 `-cmd` 设置 WAL 模式 + busy_timeout
13
13
  * - `"none"`:不添加任何初始化参数(适用于 reader,直接继承数据库已有 WAL 模式)
14
+ * @param options.onDrain drain 回调,当 stdin pipe 重新可写时调用
14
15
  */
15
- constructor(options?: { binary?: string; database?: string; initMode?: "wal" | "none" });
16
+ constructor(options?: {
17
+ binary?: string;
18
+ database?: string;
19
+ initMode?: "wal" | "none";
20
+ onDrain?: () => void;
21
+ });
16
22
 
17
23
  /** 当前使用的 sqlite3 可执行文件路径 */
18
24
  get binary(): string;
@@ -20,6 +26,12 @@ export declare class ProcessManager {
20
26
  /** 底层子进程实例,未启动时为 null */
21
27
  get process(): ChildProcess | null;
22
28
 
29
+ /**
30
+ * drain 状态,true 表示 OS pipe 已满,应暂停写入 stdin。
31
+ * 当 pipe 重新可写时 drain 事件触发,draining 恢复为 false。
32
+ */
33
+ get draining(): boolean;
34
+
23
35
  /**
24
36
  * 启动子进程(json 模式),返回 ChildProcess 实例。
25
37
  * @throws {Error} 如果 binary 路径为空或文件不存在
@@ -29,6 +41,9 @@ export declare class ProcessManager {
29
41
  /** 向子进程的 stdin 写入数据 */
30
42
  write(data: string): void;
31
43
 
44
+ /** 注册 drain 回调,当 pipe 重新可写时被调用。 */
45
+ setOnDrainCallback(fn: () => void): void;
46
+
32
47
  /**
33
48
  * 终止子进程。
34
49
  * 先尝试 SIGTERM,3 秒后未退出则 SIGKILL。
@@ -1,3 +1,5 @@
1
+ import { Metrics } from "./metrics.cts";
2
+
1
3
  /**
2
4
  * 单个 sqlite3 进程 Worker,支持管线化(pipelining)。
3
5
  *
@@ -18,6 +20,8 @@ export class TaskWorker {
18
20
  * @param options.name Worker 名称,用于日志和调试
19
21
  * @param options.initMode 子进程初始化模式(参考 ProcessManager)
20
22
  * @param options.batchSize 管线化批量大小,默认 10
23
+ * @param options.maxInflight 最大 inflight 任务数,默认 50
24
+ * @param options.metrics 指标收集器
21
25
  */
22
26
  constructor(options: {
23
27
  binary: string;
@@ -27,6 +31,8 @@ export class TaskWorker {
27
31
  name?: string;
28
32
  initMode?: "wal" | "none";
29
33
  batchSize?: number;
34
+ maxInflight?: number;
35
+ metrics?: Metrics;
30
36
  });
31
37
 
32
38
  /** Worker 名称 */
@@ -980,11 +980,11 @@ function _ts_generator(thisArg, body) {
980
980
  }
981
981
  }
982
982
  var GRACEFUL_SHUTDOWN_TIMEOUT = 5000;
983
- var _binary = /*#__PURE__*/ new WeakMap(), _database = /*#__PURE__*/ new WeakMap(), _proc = /*#__PURE__*/ new WeakMap(), _initMode = /*#__PURE__*/ new WeakMap(), _draining = /*#__PURE__*/ new WeakMap();
983
+ var _binary = /*#__PURE__*/ new WeakMap(), _database = /*#__PURE__*/ new WeakMap(), _proc = /*#__PURE__*/ new WeakMap(), _initMode = /*#__PURE__*/ new WeakMap(), _draining = /*#__PURE__*/ new WeakMap(), _onDrain = /*#__PURE__*/ new WeakMap();
984
984
  var process_ProcessManager = /*#__PURE__*/ function() {
985
985
  "use strict";
986
986
  function ProcessManager() {
987
- var _ref = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, binary = _ref.binary, database = _ref.database, _ref_initMode = _ref.initMode, initMode = void 0 === _ref_initMode ? "wal" : _ref_initMode;
987
+ var _ref = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, binary = _ref.binary, database = _ref.database, _ref_initMode = _ref.initMode, initMode = void 0 === _ref_initMode ? "wal" : _ref_initMode, onDrain = _ref.onDrain;
988
988
  process_class_call_check(this, ProcessManager);
989
989
  process_class_private_field_init(this, _binary, {
990
990
  writable: true,
@@ -1006,12 +1006,29 @@ var process_ProcessManager = /*#__PURE__*/ function() {
1006
1006
  writable: true,
1007
1007
  value: false
1008
1008
  });
1009
+ process_class_private_field_init(this, _onDrain, {
1010
+ writable: true,
1011
+ value: void 0
1012
+ });
1009
1013
  var _which;
1010
1014
  process_class_private_field_set(this, _binary, null != (_which = which(binary)) ? _which : binary);
1011
1015
  process_class_private_field_set(this, _database, database);
1012
1016
  process_class_private_field_set(this, _initMode, initMode);
1017
+ process_class_private_field_set(this, _onDrain, null != onDrain ? onDrain : function() {});
1013
1018
  }
1014
1019
  process_create_class(ProcessManager, [
1020
+ {
1021
+ key: "draining",
1022
+ get: function() {
1023
+ return process_class_private_field_get(this, _draining);
1024
+ }
1025
+ },
1026
+ {
1027
+ key: "setOnDrainCallback",
1028
+ value: function(fn) {
1029
+ process_class_private_field_set(this, _onDrain, fn);
1030
+ }
1031
+ },
1015
1032
  {
1016
1033
  key: "binary",
1017
1034
  get: function() {
@@ -1057,10 +1074,12 @@ var process_ProcessManager = /*#__PURE__*/ function() {
1057
1074
  var _$_class_private_field_get;
1058
1075
  var stream = null == (_$_class_private_field_get = process_class_private_field_get(this, _proc)) ? void 0 : _$_class_private_field_get.stdin;
1059
1076
  if (!stream) return;
1060
- if (!stream.write(data) && !process_class_private_field_get(this, _draining)) {
1077
+ if (process_class_private_field_get(this, _draining)) return;
1078
+ if (!stream.write(data)) {
1061
1079
  process_class_private_field_set(this, _draining, true);
1062
1080
  stream.once("drain", function() {
1063
1081
  process_class_private_field_set(_this, _draining, false);
1082
+ process_class_private_field_get(_this, _onDrain).call(_this);
1064
1083
  });
1065
1084
  }
1066
1085
  }
@@ -1079,7 +1098,7 @@ var process_ProcessManager = /*#__PURE__*/ function() {
1079
1098
  2
1080
1099
  ];
1081
1100
  timer = setTimeout(function() {
1082
- _this.kill();
1101
+ proc.kill();
1083
1102
  }, GRACEFUL_SHUTDOWN_TIMEOUT);
1084
1103
  _state.label = 1;
1085
1104
  case 1:
@@ -1834,6 +1853,8 @@ function processStreamRows(task, parsed) {
1834
1853
  }
1835
1854
  function settleTask(task, error, value, metrics) {
1836
1855
  var _ref = arguments.length > 4 && void 0 !== arguments[4] ? arguments[4] : {}, _ref_resetRowParser = _ref.resetRowParser, resetRowParser = void 0 === _ref_resetRowParser ? false : _ref_resetRowParser;
1856
+ if (task.settled) return;
1857
+ task.settled = true;
1837
1858
  clearTimeout(task.timer);
1838
1859
  if (resetRowParser) {
1839
1860
  var _task_rowParser_reset, _task_rowParser;
@@ -2001,7 +2022,10 @@ var taskWorker_TaskWorker = /*#__PURE__*/ function() {
2001
2022
  taskWorker_class_private_field_set(this, _processManager, new process_ProcessManager({
2002
2023
  binary: binary,
2003
2024
  database: database,
2004
- initMode: initMode
2025
+ initMode: initMode,
2026
+ onDrain: function() {
2027
+ return taskWorker_class_private_method_get(_this, _pumpQueue, pumpQueue).call(_this);
2028
+ }
2005
2029
  }));
2006
2030
  taskWorker_class_private_field_set(this, _valueParser, createJsonValueParser(function(raw) {
2007
2031
  return taskWorker_class_private_method_get(_this, _handleParsedValue, handleParsedValue).call(_this, raw);
@@ -2043,6 +2067,7 @@ var taskWorker_TaskWorker = /*#__PURE__*/ function() {
2043
2067
  onRow: null != (_config_onRow = config.onRow) ? _config_onRow : null,
2044
2068
  consumerError: null,
2045
2069
  stderrText: "",
2070
+ settled: false,
2046
2071
  errorScheduled: false,
2047
2072
  timer: null,
2048
2073
  startTime: 0
@@ -2086,6 +2111,7 @@ function startProcess() {
2086
2111
  function pumpQueue() {
2087
2112
  var _this = this;
2088
2113
  var _$_class_private_field_get;
2114
+ if (taskWorker_class_private_field_get(this, _processManager).draining) return;
2089
2115
  if (taskWorker_class_private_field_get(this, _inflightTasks).length >= taskWorker_class_private_field_get(this, _maxInflight)) return;
2090
2116
  var batch = [];
2091
2117
  while(batch.length < taskWorker_class_private_field_get(this, _batchSize) && !taskWorker_class_private_field_get(this, _pendingQueue).isEmpty() && taskWorker_class_private_field_get(this, _inflightTasks).length + batch.length < taskWorker_class_private_field_get(this, _maxInflight)){
@@ -2134,6 +2160,7 @@ function handleParsedValue(raw) {
2134
2160
  if (isSentinelRow(parsed, task.token)) {
2135
2161
  clearTimeout(task.timer);
2136
2162
  taskWorker_class_private_field_get(this, _inflightTasks).shift();
2163
+ if (task.timedout) return void taskWorker_class_private_method_get(this, _pumpQueue, pumpQueue).call(this);
2137
2164
  if (task.stderrText) {
2138
2165
  taskWorker_class_private_method_get(this, _settleTask, settleTask1).call(this, task, new Error(task.stderrText.trim()), void 0);
2139
2166
  taskWorker_class_private_method_get(this, _pumpQueue, pumpQueue).call(this);
@@ -2202,9 +2229,12 @@ function taskWorker_handleStderrChunk(chunk) {
2202
2229
  }
2203
2230
  function handleTaskTimeout(task) {
2204
2231
  var _$_class_private_field_get;
2205
- if (taskWorker_class_private_field_get(this, _inflightTasks)[0] !== task) return;
2232
+ if (task.settled) return;
2233
+ task.timedout = true;
2234
+ clearTimeout(task.timer);
2235
+ task.timer = null;
2206
2236
  null == (_$_class_private_field_get = taskWorker_class_private_field_get(this, _metrics)) || _$_class_private_field_get.incrementTasksTimeout();
2207
- taskWorker_class_private_method_get(this, _rejectAll, taskWorker_rejectAll).call(this, createTimeoutError(task.timeout, task.sql));
2237
+ taskWorker_class_private_method_get(this, _settleTask, settleTask1).call(this, task, createTimeoutError(task.timeout, task.sql), void 0);
2208
2238
  }
2209
2239
  function settleTask1(task, error, value) {
2210
2240
  settleTask(task, error, value, taskWorker_class_private_field_get(this, _metrics));
@@ -2969,7 +2999,7 @@ function pipelineEngine_unsupported_iterable_to_array(o, minLen) {
2969
2999
  if ("Map" === n || "Set" === n) return Array.from(n);
2970
3000
  if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return pipelineEngine_array_like_to_array(o, minLen);
2971
3001
  }
2972
- var _queue = /*#__PURE__*/ new WeakMap(), pipelineEngine_inflightTasks = /*#__PURE__*/ new WeakMap(), pipelineEngine_pendingFinalizeTasks = /*#__PURE__*/ new WeakMap(), pipelineEngine_scheduledFinalize = /*#__PURE__*/ new WeakMap(), _sharedValueParser = /*#__PURE__*/ new WeakMap(), pipelineEngine_processManager = /*#__PURE__*/ new WeakMap(), pipelineEngine_metrics = /*#__PURE__*/ new WeakMap(), pipelineEngine_statementTimeout = /*#__PURE__*/ new WeakMap(), pipelineEngine_logger = /*#__PURE__*/ new WeakMap(), pipelineEngine_batchSize = /*#__PURE__*/ new WeakMap(), pipelineEngine_maxInflight = /*#__PURE__*/ new WeakMap(), _onTaskTimeout = /*#__PURE__*/ new WeakMap(), _active = /*#__PURE__*/ new WeakMap(), core_pipelineEngine_pumpQueue = /*#__PURE__*/ new WeakSet(), pipelineEngine_handleParsedValue = /*#__PURE__*/ new WeakSet(), core_pipelineEngine_scheduleFinalizeCheck = /*#__PURE__*/ new WeakSet(), pipelineEngine_settleTask = /*#__PURE__*/ new WeakSet();
3002
+ var _queue = /*#__PURE__*/ new WeakMap(), pipelineEngine_inflightTasks = /*#__PURE__*/ new WeakMap(), pipelineEngine_pendingFinalizeTasks = /*#__PURE__*/ new WeakMap(), pipelineEngine_scheduledFinalize = /*#__PURE__*/ new WeakMap(), _sharedValueParser = /*#__PURE__*/ new WeakMap(), pipelineEngine_processManager = /*#__PURE__*/ new WeakMap(), pipelineEngine_metrics = /*#__PURE__*/ new WeakMap(), pipelineEngine_statementTimeout = /*#__PURE__*/ new WeakMap(), pipelineEngine_logger = /*#__PURE__*/ new WeakMap(), pipelineEngine_batchSize = /*#__PURE__*/ new WeakMap(), pipelineEngine_maxInflight = /*#__PURE__*/ new WeakMap(), _onTaskTimeout = /*#__PURE__*/ new WeakMap(), _active = /*#__PURE__*/ new WeakMap(), core_pipelineEngine_pumpQueue = /*#__PURE__*/ new WeakSet(), pipelineEngine_handleParsedValue = /*#__PURE__*/ new WeakSet(), core_pipelineEngine_scheduleFinalizeCheck = /*#__PURE__*/ new WeakSet(), core_pipelineEngine_handleTaskTimeout = /*#__PURE__*/ new WeakSet(), pipelineEngine_settleTask = /*#__PURE__*/ new WeakSet();
2973
3003
  var pipelineEngine_PipelineEngine = /*#__PURE__*/ function() {
2974
3004
  "use strict";
2975
3005
  function PipelineEngine(processManager, param) {
@@ -2979,6 +3009,7 @@ var pipelineEngine_PipelineEngine = /*#__PURE__*/ function() {
2979
3009
  pipelineEngine_class_private_method_init(this, core_pipelineEngine_pumpQueue);
2980
3010
  pipelineEngine_class_private_method_init(this, pipelineEngine_handleParsedValue);
2981
3011
  pipelineEngine_class_private_method_init(this, core_pipelineEngine_scheduleFinalizeCheck);
3012
+ pipelineEngine_class_private_method_init(this, core_pipelineEngine_handleTaskTimeout);
2982
3013
  pipelineEngine_class_private_method_init(this, pipelineEngine_settleTask);
2983
3014
  pipelineEngine_class_private_field_init(this, _queue, {
2984
3015
  writable: true,
@@ -3042,6 +3073,9 @@ var pipelineEngine_PipelineEngine = /*#__PURE__*/ function() {
3042
3073
  pipelineEngine_class_private_field_set(this, _sharedValueParser, createJsonValueParser(function(raw) {
3043
3074
  return pipelineEngine_class_private_method_get(_this, pipelineEngine_handleParsedValue, core_pipelineEngine_handleParsedValue).call(_this, raw);
3044
3075
  }));
3076
+ pipelineEngine_class_private_field_get(this, pipelineEngine_processManager).setOnDrainCallback(function() {
3077
+ return pipelineEngine_class_private_method_get(_this, core_pipelineEngine_pumpQueue, pipelineEngine_pumpQueue).call(_this);
3078
+ });
3045
3079
  }
3046
3080
  pipelineEngine_create_class(PipelineEngine, [
3047
3081
  {
@@ -3174,6 +3208,7 @@ function pipelineEngine_pumpQueue() {
3174
3208
  var _this = this;
3175
3209
  var _$_class_private_field_get;
3176
3210
  if (!pipelineEngine_class_private_field_get(this, _active)) return;
3211
+ if (pipelineEngine_class_private_field_get(this, pipelineEngine_processManager).draining) return;
3177
3212
  if (pipelineEngine_class_private_field_get(this, pipelineEngine_inflightTasks).length >= pipelineEngine_class_private_field_get(this, pipelineEngine_maxInflight)) return;
3178
3213
  var batch = [];
3179
3214
  while(batch.length < pipelineEngine_class_private_field_get(this, pipelineEngine_batchSize) && !pipelineEngine_class_private_field_get(this, _queue).isEmpty() && pipelineEngine_class_private_field_get(this, pipelineEngine_inflightTasks).length + batch.length < pipelineEngine_class_private_field_get(this, pipelineEngine_maxInflight)){
@@ -3191,8 +3226,7 @@ function pipelineEngine_pumpQueue() {
3191
3226
  var task = _step.value;
3192
3227
  task.startTime = now;
3193
3228
  task.timer = setTimeout(function() {
3194
- if (pipelineEngine_class_private_field_get(_this, pipelineEngine_inflightTasks)[0] !== task) return;
3195
- pipelineEngine_class_private_field_get(_this, _onTaskTimeout).call(_this, task);
3229
+ return pipelineEngine_class_private_method_get(_this, core_pipelineEngine_handleTaskTimeout, pipelineEngine_handleTaskTimeout).call(_this, task);
3196
3230
  }, task.timeout);
3197
3231
  };
3198
3232
  for(var _iterator = batch[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
@@ -3222,6 +3256,7 @@ function core_pipelineEngine_handleParsedValue(raw) {
3222
3256
  if (isSentinelRow(parsed, task.token)) {
3223
3257
  clearTimeout(task.timer);
3224
3258
  pipelineEngine_class_private_field_get(this, pipelineEngine_inflightTasks).shift();
3259
+ if (task.timedout) return void pipelineEngine_class_private_method_get(this, core_pipelineEngine_pumpQueue, pipelineEngine_pumpQueue).call(this);
3225
3260
  if (task.consumerError) {
3226
3261
  pipelineEngine_class_private_method_get(this, pipelineEngine_settleTask, pipelineEngine_settleTask1).call(this, task, task.consumerError, void 0);
3227
3262
  pipelineEngine_class_private_method_get(this, core_pipelineEngine_pumpQueue, pipelineEngine_pumpQueue).call(this);
@@ -3232,6 +3267,7 @@ function core_pipelineEngine_handleParsedValue(raw) {
3232
3267
  pipelineEngine_class_private_method_get(this, core_pipelineEngine_pumpQueue, pipelineEngine_pumpQueue).call(this);
3233
3268
  return;
3234
3269
  }
3270
+ if (task.timedout) return;
3235
3271
  if ("query" === task.kind) return void collectQueryRows(task, parsed);
3236
3272
  if ("stream" === task.kind) processStreamRows(task, parsed);
3237
3273
  }
@@ -3272,6 +3308,14 @@ function pipelineEngine_scheduleFinalizeCheck() {
3272
3308
  pipelineEngine_class_private_field_get(_this, pipelineEngine_pendingFinalizeTasks).clear();
3273
3309
  });
3274
3310
  }
3311
+ function pipelineEngine_handleTaskTimeout(task) {
3312
+ var _this, _this1;
3313
+ if (task.settled) return;
3314
+ task.timedout = true;
3315
+ clearTimeout(task.timer);
3316
+ pipelineEngine_class_private_method_get(this, pipelineEngine_settleTask, pipelineEngine_settleTask1).call(this, task, createTimeoutError(task.timeout, task.sql), void 0);
3317
+ null == (_this = pipelineEngine_class_private_field_get(_this1 = this, _onTaskTimeout)) || _this.call(_this1, task);
3318
+ }
3275
3319
  function pipelineEngine_settleTask1(task, error, value) {
3276
3320
  settleTask(task, error, value, pipelineEngine_class_private_field_get(this, pipelineEngine_metrics), {
3277
3321
  resetRowParser: true
@@ -3581,7 +3625,6 @@ var executor_SQLiteExecutor = /*#__PURE__*/ function() {
3581
3625
  logger: executor_class_private_field_get(this, executor_logger),
3582
3626
  onTaskTimeout: function(task) {
3583
3627
  executor_class_private_field_get(_this, executor_metrics).incrementTasksTimeout();
3584
- executor_class_private_method_get(_this, _handleProcessFailure, handleProcessFailure).call(_this, createTimeoutError(task.timeout, task.sql));
3585
3628
  }
3586
3629
  }));
3587
3630
  executor_class_private_method_get(this, executor_startProcess, core_executor_startProcess).call(this);
@@ -3898,6 +3941,7 @@ function enqueueWriter(kind, sql, timeout, token, onRow, scopeId) {
3898
3941
  reject: reject,
3899
3942
  consumerError: null,
3900
3943
  stderrText: "",
3944
+ settled: false,
3901
3945
  timer: null,
3902
3946
  startTime: 0,
3903
3947
  rowParser: null