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.mts";
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 名称 */
@@ -920,11 +920,11 @@ function _ts_generator(thisArg, body) {
920
920
  }
921
921
  }
922
922
  var GRACEFUL_SHUTDOWN_TIMEOUT = 5000;
923
- var _binary = /*#__PURE__*/ new WeakMap(), _database = /*#__PURE__*/ new WeakMap(), _proc = /*#__PURE__*/ new WeakMap(), _initMode = /*#__PURE__*/ new WeakMap(), _draining = /*#__PURE__*/ new WeakMap();
923
+ 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();
924
924
  var process_ProcessManager = /*#__PURE__*/ function() {
925
925
  "use strict";
926
926
  function ProcessManager() {
927
- 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;
927
+ 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;
928
928
  process_class_call_check(this, ProcessManager);
929
929
  process_class_private_field_init(this, _binary, {
930
930
  writable: true,
@@ -946,12 +946,29 @@ var process_ProcessManager = /*#__PURE__*/ function() {
946
946
  writable: true,
947
947
  value: false
948
948
  });
949
+ process_class_private_field_init(this, _onDrain, {
950
+ writable: true,
951
+ value: void 0
952
+ });
949
953
  var _which;
950
954
  process_class_private_field_set(this, _binary, null != (_which = which(binary)) ? _which : binary);
951
955
  process_class_private_field_set(this, _database, database);
952
956
  process_class_private_field_set(this, _initMode, initMode);
957
+ process_class_private_field_set(this, _onDrain, null != onDrain ? onDrain : function() {});
953
958
  }
954
959
  process_create_class(ProcessManager, [
960
+ {
961
+ key: "draining",
962
+ get: function() {
963
+ return process_class_private_field_get(this, _draining);
964
+ }
965
+ },
966
+ {
967
+ key: "setOnDrainCallback",
968
+ value: function(fn) {
969
+ process_class_private_field_set(this, _onDrain, fn);
970
+ }
971
+ },
955
972
  {
956
973
  key: "binary",
957
974
  get: function() {
@@ -997,10 +1014,12 @@ var process_ProcessManager = /*#__PURE__*/ function() {
997
1014
  var _$_class_private_field_get;
998
1015
  var stream = null == (_$_class_private_field_get = process_class_private_field_get(this, _proc)) ? void 0 : _$_class_private_field_get.stdin;
999
1016
  if (!stream) return;
1000
- if (!stream.write(data) && !process_class_private_field_get(this, _draining)) {
1017
+ if (process_class_private_field_get(this, _draining)) return;
1018
+ if (!stream.write(data)) {
1001
1019
  process_class_private_field_set(this, _draining, true);
1002
1020
  stream.once("drain", function() {
1003
1021
  process_class_private_field_set(_this, _draining, false);
1022
+ process_class_private_field_get(_this, _onDrain).call(_this);
1004
1023
  });
1005
1024
  }
1006
1025
  }
@@ -1019,7 +1038,7 @@ var process_ProcessManager = /*#__PURE__*/ function() {
1019
1038
  2
1020
1039
  ];
1021
1040
  timer = setTimeout(function() {
1022
- _this.kill();
1041
+ proc.kill();
1023
1042
  }, GRACEFUL_SHUTDOWN_TIMEOUT);
1024
1043
  _state.label = 1;
1025
1044
  case 1:
@@ -1774,6 +1793,8 @@ function processStreamRows(task, parsed) {
1774
1793
  }
1775
1794
  function settleTask(task, error, value, metrics) {
1776
1795
  var _ref = arguments.length > 4 && void 0 !== arguments[4] ? arguments[4] : {}, _ref_resetRowParser = _ref.resetRowParser, resetRowParser = void 0 === _ref_resetRowParser ? false : _ref_resetRowParser;
1796
+ if (task.settled) return;
1797
+ task.settled = true;
1777
1798
  clearTimeout(task.timer);
1778
1799
  if (resetRowParser) {
1779
1800
  var _task_rowParser_reset, _task_rowParser;
@@ -1941,7 +1962,10 @@ var taskWorker_TaskWorker = /*#__PURE__*/ function() {
1941
1962
  taskWorker_class_private_field_set(this, _processManager, new process_ProcessManager({
1942
1963
  binary: binary,
1943
1964
  database: database,
1944
- initMode: initMode
1965
+ initMode: initMode,
1966
+ onDrain: function() {
1967
+ return taskWorker_class_private_method_get(_this, _pumpQueue, pumpQueue).call(_this);
1968
+ }
1945
1969
  }));
1946
1970
  taskWorker_class_private_field_set(this, _valueParser, createJsonValueParser(function(raw) {
1947
1971
  return taskWorker_class_private_method_get(_this, _handleParsedValue, handleParsedValue).call(_this, raw);
@@ -1983,6 +2007,7 @@ var taskWorker_TaskWorker = /*#__PURE__*/ function() {
1983
2007
  onRow: null != (_config_onRow = config.onRow) ? _config_onRow : null,
1984
2008
  consumerError: null,
1985
2009
  stderrText: "",
2010
+ settled: false,
1986
2011
  errorScheduled: false,
1987
2012
  timer: null,
1988
2013
  startTime: 0
@@ -2026,6 +2051,7 @@ function startProcess() {
2026
2051
  function pumpQueue() {
2027
2052
  var _this = this;
2028
2053
  var _$_class_private_field_get;
2054
+ if (taskWorker_class_private_field_get(this, _processManager).draining) return;
2029
2055
  if (taskWorker_class_private_field_get(this, _inflightTasks).length >= taskWorker_class_private_field_get(this, _maxInflight)) return;
2030
2056
  var batch = [];
2031
2057
  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)){
@@ -2074,6 +2100,7 @@ function handleParsedValue(raw) {
2074
2100
  if (isSentinelRow(parsed, task.token)) {
2075
2101
  clearTimeout(task.timer);
2076
2102
  taskWorker_class_private_field_get(this, _inflightTasks).shift();
2103
+ if (task.timedout) return void taskWorker_class_private_method_get(this, _pumpQueue, pumpQueue).call(this);
2077
2104
  if (task.stderrText) {
2078
2105
  taskWorker_class_private_method_get(this, _settleTask, settleTask1).call(this, task, new Error(task.stderrText.trim()), void 0);
2079
2106
  taskWorker_class_private_method_get(this, _pumpQueue, pumpQueue).call(this);
@@ -2142,9 +2169,12 @@ function taskWorker_handleStderrChunk(chunk) {
2142
2169
  }
2143
2170
  function handleTaskTimeout(task) {
2144
2171
  var _$_class_private_field_get;
2145
- if (taskWorker_class_private_field_get(this, _inflightTasks)[0] !== task) return;
2172
+ if (task.settled) return;
2173
+ task.timedout = true;
2174
+ clearTimeout(task.timer);
2175
+ task.timer = null;
2146
2176
  null == (_$_class_private_field_get = taskWorker_class_private_field_get(this, _metrics)) || _$_class_private_field_get.incrementTasksTimeout();
2147
- taskWorker_class_private_method_get(this, _rejectAll, taskWorker_rejectAll).call(this, createTimeoutError(task.timeout, task.sql));
2177
+ taskWorker_class_private_method_get(this, _settleTask, settleTask1).call(this, task, createTimeoutError(task.timeout, task.sql), void 0);
2148
2178
  }
2149
2179
  function settleTask1(task, error, value) {
2150
2180
  settleTask(task, error, value, taskWorker_class_private_field_get(this, _metrics));
@@ -2909,7 +2939,7 @@ function pipelineEngine_unsupported_iterable_to_array(o, minLen) {
2909
2939
  if ("Map" === n || "Set" === n) return Array.from(n);
2910
2940
  if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return pipelineEngine_array_like_to_array(o, minLen);
2911
2941
  }
2912
- 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();
2942
+ 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();
2913
2943
  var pipelineEngine_PipelineEngine = /*#__PURE__*/ function() {
2914
2944
  "use strict";
2915
2945
  function PipelineEngine(processManager, param) {
@@ -2919,6 +2949,7 @@ var pipelineEngine_PipelineEngine = /*#__PURE__*/ function() {
2919
2949
  pipelineEngine_class_private_method_init(this, core_pipelineEngine_pumpQueue);
2920
2950
  pipelineEngine_class_private_method_init(this, pipelineEngine_handleParsedValue);
2921
2951
  pipelineEngine_class_private_method_init(this, core_pipelineEngine_scheduleFinalizeCheck);
2952
+ pipelineEngine_class_private_method_init(this, core_pipelineEngine_handleTaskTimeout);
2922
2953
  pipelineEngine_class_private_method_init(this, pipelineEngine_settleTask);
2923
2954
  pipelineEngine_class_private_field_init(this, _queue, {
2924
2955
  writable: true,
@@ -2982,6 +3013,9 @@ var pipelineEngine_PipelineEngine = /*#__PURE__*/ function() {
2982
3013
  pipelineEngine_class_private_field_set(this, _sharedValueParser, createJsonValueParser(function(raw) {
2983
3014
  return pipelineEngine_class_private_method_get(_this, pipelineEngine_handleParsedValue, core_pipelineEngine_handleParsedValue).call(_this, raw);
2984
3015
  }));
3016
+ pipelineEngine_class_private_field_get(this, pipelineEngine_processManager).setOnDrainCallback(function() {
3017
+ return pipelineEngine_class_private_method_get(_this, core_pipelineEngine_pumpQueue, pipelineEngine_pumpQueue).call(_this);
3018
+ });
2985
3019
  }
2986
3020
  pipelineEngine_create_class(PipelineEngine, [
2987
3021
  {
@@ -3114,6 +3148,7 @@ function pipelineEngine_pumpQueue() {
3114
3148
  var _this = this;
3115
3149
  var _$_class_private_field_get;
3116
3150
  if (!pipelineEngine_class_private_field_get(this, _active)) return;
3151
+ if (pipelineEngine_class_private_field_get(this, pipelineEngine_processManager).draining) return;
3117
3152
  if (pipelineEngine_class_private_field_get(this, pipelineEngine_inflightTasks).length >= pipelineEngine_class_private_field_get(this, pipelineEngine_maxInflight)) return;
3118
3153
  var batch = [];
3119
3154
  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)){
@@ -3131,8 +3166,7 @@ function pipelineEngine_pumpQueue() {
3131
3166
  var task = _step.value;
3132
3167
  task.startTime = now;
3133
3168
  task.timer = setTimeout(function() {
3134
- if (pipelineEngine_class_private_field_get(_this, pipelineEngine_inflightTasks)[0] !== task) return;
3135
- pipelineEngine_class_private_field_get(_this, _onTaskTimeout).call(_this, task);
3169
+ return pipelineEngine_class_private_method_get(_this, core_pipelineEngine_handleTaskTimeout, pipelineEngine_handleTaskTimeout).call(_this, task);
3136
3170
  }, task.timeout);
3137
3171
  };
3138
3172
  for(var _iterator = batch[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
@@ -3162,6 +3196,7 @@ function core_pipelineEngine_handleParsedValue(raw) {
3162
3196
  if (isSentinelRow(parsed, task.token)) {
3163
3197
  clearTimeout(task.timer);
3164
3198
  pipelineEngine_class_private_field_get(this, pipelineEngine_inflightTasks).shift();
3199
+ if (task.timedout) return void pipelineEngine_class_private_method_get(this, core_pipelineEngine_pumpQueue, pipelineEngine_pumpQueue).call(this);
3165
3200
  if (task.consumerError) {
3166
3201
  pipelineEngine_class_private_method_get(this, pipelineEngine_settleTask, pipelineEngine_settleTask1).call(this, task, task.consumerError, void 0);
3167
3202
  pipelineEngine_class_private_method_get(this, core_pipelineEngine_pumpQueue, pipelineEngine_pumpQueue).call(this);
@@ -3172,6 +3207,7 @@ function core_pipelineEngine_handleParsedValue(raw) {
3172
3207
  pipelineEngine_class_private_method_get(this, core_pipelineEngine_pumpQueue, pipelineEngine_pumpQueue).call(this);
3173
3208
  return;
3174
3209
  }
3210
+ if (task.timedout) return;
3175
3211
  if ("query" === task.kind) return void collectQueryRows(task, parsed);
3176
3212
  if ("stream" === task.kind) processStreamRows(task, parsed);
3177
3213
  }
@@ -3212,6 +3248,14 @@ function pipelineEngine_scheduleFinalizeCheck() {
3212
3248
  pipelineEngine_class_private_field_get(_this, pipelineEngine_pendingFinalizeTasks).clear();
3213
3249
  });
3214
3250
  }
3251
+ function pipelineEngine_handleTaskTimeout(task) {
3252
+ var _this, _this1;
3253
+ if (task.settled) return;
3254
+ task.timedout = true;
3255
+ clearTimeout(task.timer);
3256
+ pipelineEngine_class_private_method_get(this, pipelineEngine_settleTask, pipelineEngine_settleTask1).call(this, task, createTimeoutError(task.timeout, task.sql), void 0);
3257
+ null == (_this = pipelineEngine_class_private_field_get(_this1 = this, _onTaskTimeout)) || _this.call(_this1, task);
3258
+ }
3215
3259
  function pipelineEngine_settleTask1(task, error, value) {
3216
3260
  settleTask(task, error, value, pipelineEngine_class_private_field_get(this, pipelineEngine_metrics), {
3217
3261
  resetRowParser: true
@@ -3521,7 +3565,6 @@ var executor_SQLiteExecutor = /*#__PURE__*/ function() {
3521
3565
  logger: executor_class_private_field_get(this, executor_logger),
3522
3566
  onTaskTimeout: function(task) {
3523
3567
  executor_class_private_field_get(_this, executor_metrics).incrementTasksTimeout();
3524
- executor_class_private_method_get(_this, _handleProcessFailure, handleProcessFailure).call(_this, createTimeoutError(task.timeout, task.sql));
3525
3568
  }
3526
3569
  }));
3527
3570
  executor_class_private_method_get(this, executor_startProcess, core_executor_startProcess).call(this);
@@ -3838,6 +3881,7 @@ function enqueueWriter(kind, sql, timeout, token, onRow, scopeId) {
3838
3881
  reject: reject,
3839
3882
  consumerError: null,
3840
3883
  stderrText: "",
3884
+ settled: false,
3841
3885
  timer: null,
3842
3886
  startTime: 0,
3843
3887
  rowParser: null