@rotorsoft/act 0.6.2 → 0.6.4

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.
@@ -1 +1 @@
1
- {"version":3,"file":"InMemoryStore.d.ts","sourceRoot":"","sources":["../../../src/adapters/InMemoryStore.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EACV,SAAS,EACT,SAAS,EACT,KAAK,EACL,OAAO,EACP,KAAK,EACL,OAAO,EACP,KAAK,EACN,MAAM,mBAAmB,CAAC;AAgG3B;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAc,YAAW,KAAK;IAEzC,OAAO,CAAC,OAAO,CAA2C;IAE1D,OAAO,CAAC,QAAQ,CAA0C;IAE1D;;;OAGG;IACG,OAAO;IAKb;;;OAGG;IACG,IAAI;IAIV;;;OAGG;IACG,IAAI;IAMV,OAAO,CAAC,QAAQ;IAUhB;;;;;OAKG;IACG,KAAK,CAAC,CAAC,SAAS,OAAO,EAC3B,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,KAAK,IAAI,EAChD,KAAK,CAAC,EAAE,KAAK;IAiCf;;;;;;;;OAQG;IACG,MAAM,CAAC,CAAC,SAAS,OAAO,EAC5B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,EAC3B,IAAI,EAAE,SAAS,EACf,eAAe,CAAC,EAAE,MAAM;IAiC1B;;;;;OAKG;IACG,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;;;;;IAe3C;;;;;OAKG;IACG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,MAAM;IAa3C;;;OAGG;IACG,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE;;;;;;;IAOzB;;;;OAIG;IACG,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,GAAG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}
@@ -68,11 +68,13 @@ export type ReactionPayload<E extends Schemas> = Reaction<E> & {
68
68
  * @property stream - The target stream name.
69
69
  * @property source - The source stream.
70
70
  * @property at - The lease watermark.
71
+ * @property lagging - Whether the stream is lagging behind.
71
72
  */
72
73
  export type Poll = {
73
74
  readonly stream: string;
74
75
  readonly source?: string;
75
76
  readonly at: number;
77
+ readonly lagging: boolean;
76
78
  };
77
79
  /**
78
80
  * Result of fetching events from the store for processing.
@@ -80,12 +82,14 @@ export type Poll = {
80
82
  * @property stream - The stream name
81
83
  * @property source - The source stream(s) (name or RegExp), or undefined when sourcing from all streams.
82
84
  * @property at - The last event sequence number processed by the stream.
85
+ * @property lagging - Whether the stream is lagging behind.
83
86
  * @property events - The list of next committed events to be processed by the stream.
84
87
  */
85
88
  export type Fetch<E extends Schemas> = Array<{
86
89
  readonly stream: string;
87
90
  readonly source?: string;
88
91
  readonly at: number;
92
+ readonly lagging: boolean;
89
93
  readonly events: Committed<E, keyof E>[];
90
94
  }>;
91
95
  /**
@@ -93,8 +97,9 @@ export type Fetch<E extends Schemas> = Array<{
93
97
  * @property stream - The target stream name.
94
98
  * @property source - The source stream.
95
99
  * @property by - The lease holder.
96
- * @property at - The lease watermark.
100
+ * @property at - The lease watermark (last event in the lease window or acked).
97
101
  * @property retry - Retry count.
102
+ * @property lagging - Whether the stream is lagging behind.
98
103
  */
99
104
  export type Lease = {
100
105
  readonly stream: string;
@@ -102,6 +107,7 @@ export type Lease = {
102
107
  readonly at: number;
103
108
  readonly by: string;
104
109
  readonly retry: number;
110
+ readonly lagging: boolean;
105
111
  };
106
112
  /**
107
113
  * Options for draining events from the store.
@@ -119,14 +125,14 @@ export type DrainOptions = {
119
125
  * @property fetched - The fetched events.
120
126
  * @property leased - The leased events.
121
127
  * @property acked - The acked events.
122
- * @property blocked - The blocked events.
128
+ * @property blocked - The blocked events (with error).
123
129
  */
124
130
  export type Drain<E extends Schemas> = {
125
131
  readonly fetched: Fetch<E>;
126
132
  readonly leased: Lease[];
127
133
  readonly acked: Lease[];
128
134
  readonly blocked: Array<Lease & {
129
- error: string;
135
+ readonly error: string;
130
136
  }>;
131
137
  };
132
138
  //# sourceMappingURL=reaction.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"reaction.d.ts","sourceRoot":"","sources":["../../../src/types/reaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAExE;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,OAAO,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,CAClE,KAAK,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EACtB,MAAM,EAAE,MAAM,KACX,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC;AAEzC;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,OAAO,EAAE,CAAC,SAAS,MAAM,CAAC,IAC7D;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GACnC,CAAC,CACC,KAAK,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,KACnB;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAAC,CAAC;AAE1D;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAC/B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,OAAO,EAAE,CAAC,SAAS,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI;IACrE,QAAQ,CAAC,OAAO,EAAE,eAA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E,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC;CAC1C,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,OAAO,IAAI;IACrC,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,GAAG;QAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC7D,CAAC"}
package/dist/index.cjs CHANGED
@@ -280,53 +280,54 @@ var InMemoryStream = class {
280
280
  }
281
281
  /**
282
282
  * Attempt to lease this stream for processing.
283
- * @param at - The end-of-lease watermark.
284
- * @param by - The lease holder.
283
+ * @param lease - The lease request.
285
284
  * @param millis - Lease duration in milliseconds.
286
285
  * @returns The granted lease or undefined if blocked.
287
286
  */
288
- lease(at, by, millis) {
287
+ lease(lease, millis) {
289
288
  if (this.is_avaliable) {
290
289
  if (millis > 0) {
291
- this._leased_by = by;
290
+ this._leased_by = lease.by;
292
291
  this._leased_until = new Date(Date.now() + millis);
293
292
  this._retry = this._retry + 1;
294
293
  }
295
294
  return {
296
295
  stream: this.stream,
297
296
  source: this.source,
298
- at,
299
- by,
300
- retry: this._retry
297
+ at: lease.at,
298
+ by: lease.by,
299
+ retry: this._retry,
300
+ lagging: lease.lagging
301
301
  };
302
302
  }
303
303
  }
304
304
  /**
305
305
  * Acknowledge completion of processing for this stream.
306
- * @param at - Last processed watermark.
307
- * @param by - Lease holder that processed the watermark.
306
+ * @param lease - The lease request.
308
307
  */
309
- ack(at, by) {
310
- if (this._leased_by === by) {
308
+ ack(lease) {
309
+ if (this._leased_by === lease.by) {
311
310
  this._leased_by = void 0;
312
311
  this._leased_until = void 0;
313
- this._at = at;
312
+ this._at = lease.at;
314
313
  this._retry = -1;
315
314
  return {
316
315
  stream: this.stream,
317
316
  source: this.source,
318
317
  at: this._at,
319
- by,
320
- retry: this._retry
318
+ by: lease.by,
319
+ retry: this._retry,
320
+ lagging: lease.lagging
321
321
  };
322
322
  }
323
323
  }
324
324
  /**
325
325
  * Block a stream for processing after failing to process and reaching max retries with blocking enabled.
326
+ * @param lease - The lease request.
326
327
  * @param error Blocked error message.
327
328
  */
328
- block(by, error) {
329
- if (this._leased_by === by) {
329
+ block(lease, error) {
330
+ if (this._leased_by === lease.by) {
330
331
  this._blocked = true;
331
332
  this._error = error;
332
333
  return {
@@ -335,7 +336,8 @@ var InMemoryStream = class {
335
336
  at: this._at,
336
337
  by: this._leased_by,
337
338
  retry: this._retry,
338
- error: this._error
339
+ error: this._error,
340
+ lagging: lease.lagging
339
341
  };
340
342
  }
341
343
  }
@@ -459,8 +461,18 @@ var InMemoryStore = class {
459
461
  */
460
462
  async poll(lagging, leading) {
461
463
  await sleep();
462
- const a = [...this._streams.values()].filter((s) => s.is_avaliable).sort((a2, b2) => a2.at - b2.at).slice(0, lagging).map(({ stream, source, at }) => ({ stream, source, at }));
463
- const b = [...this._streams.values()].filter((s) => s.is_avaliable).sort((a2, b2) => b2.at - a2.at).slice(0, leading).map(({ stream, source, at }) => ({ stream, source, at }));
464
+ const a = [...this._streams.values()].filter((s) => s.is_avaliable).sort((a2, b2) => a2.at - b2.at).slice(0, lagging).map(({ stream, source, at }) => ({
465
+ stream,
466
+ source,
467
+ at,
468
+ lagging: true
469
+ }));
470
+ const b = [...this._streams.values()].filter((s) => s.is_avaliable).sort((a2, b2) => b2.at - a2.at).slice(0, leading).map(({ stream, source, at }) => ({
471
+ stream,
472
+ source,
473
+ at,
474
+ lagging: false
475
+ }));
464
476
  return [...a, ...b];
465
477
  }
466
478
  /**
@@ -475,7 +487,7 @@ var InMemoryStore = class {
475
487
  if (!this._streams.has(l.stream)) {
476
488
  this._streams.set(l.stream, new InMemoryStream(l.stream, l.source));
477
489
  }
478
- return this._streams.get(l.stream)?.lease(l.at, l.by, millis);
490
+ return this._streams.get(l.stream)?.lease(l, millis);
479
491
  }).filter((l) => !!l);
480
492
  }
481
493
  /**
@@ -484,7 +496,7 @@ var InMemoryStore = class {
484
496
  */
485
497
  async ack(leases) {
486
498
  await sleep();
487
- return leases.map((l) => this._streams.get(l.stream)?.ack(l.at, l.by)).filter((l) => !!l);
499
+ return leases.map((l) => this._streams.get(l.stream)?.ack(l)).filter((l) => !!l);
488
500
  }
489
501
  /**
490
502
  * Block a stream for processing after failing to process and reaching max retries with blocking enabled.
@@ -493,7 +505,7 @@ var InMemoryStore = class {
493
505
  */
494
506
  async block(leases) {
495
507
  await sleep();
496
- return leases.map((l) => this._streams.get(l.stream)?.block(l.by, l.error)).filter((l) => !!l);
508
+ return leases.map((l) => this._streams.get(l.stream)?.block(l, l.error)).filter((l) => !!l);
497
509
  }
498
510
  };
499
511
 
@@ -753,6 +765,7 @@ var Act = class {
753
765
  }
754
766
  _emitter = new import_events.default();
755
767
  _drain_locked = false;
768
+ _drain_lag2lead_ratio = 0.5;
756
769
  _correlation_interval = void 0;
757
770
  emit(event, args) {
758
771
  return this._emitter.emit(event, args);
@@ -855,7 +868,7 @@ var Act = class {
855
868
  * @returns The lease with results
856
869
  */
857
870
  async handle(lease, payloads) {
858
- if (payloads.length === 0) return { lease, at: lease.at };
871
+ if (payloads.length === 0) return { lease, handled: 0, at: lease.at };
859
872
  const stream = lease.stream;
860
873
  let at = payloads.at(0).event.id, handled = 0;
861
874
  lease.retry > 0 && logger.warn(`Retrying ${stream}@${at} (${lease.retry}).`);
@@ -871,6 +884,7 @@ var Act = class {
871
884
  block && logger.error(`Blocking ${stream} after ${lease.retry} retries.`);
872
885
  return {
873
886
  lease,
887
+ handled,
874
888
  at,
875
889
  // only report error when nothing was handled
876
890
  error: handled === 0 ? error.message : void 0,
@@ -878,7 +892,7 @@ var Act = class {
878
892
  };
879
893
  }
880
894
  }
881
- return { lease, at };
895
+ return { lease, handled, at };
882
896
  }
883
897
  /**
884
898
  * Drains and processes events from the store, triggering reactions and updating state.
@@ -898,27 +912,27 @@ var Act = class {
898
912
  if (!this._drain_locked) {
899
913
  try {
900
914
  this._drain_locked = true;
901
- const lagging = Math.ceil(streamLimit * 2 / 3);
915
+ const lagging = Math.ceil(streamLimit * this._drain_lag2lead_ratio);
902
916
  const leading = streamLimit - lagging;
903
917
  const polled = await store().poll(lagging, leading);
904
918
  const fetched = await Promise.all(
905
- polled.map(async ({ stream, source, at }) => {
919
+ polled.map(async ({ stream, source, at, lagging: lagging2 }) => {
906
920
  const events = await this.query_array({
907
921
  stream: source,
908
922
  after: at,
909
923
  limit: eventLimit
910
924
  });
911
- return { stream, source, at, events };
925
+ return { stream, source, at, lagging: lagging2, events };
912
926
  })
913
927
  );
914
928
  if (fetched.length) {
915
929
  tracer.fetched(fetched);
916
930
  const leases = /* @__PURE__ */ new Map();
917
- const last_window_at = fetched.reduce(
931
+ const fetch_window_at = fetched.reduce(
918
932
  (max, { at, events }) => Math.max(max, events.at(-1)?.id || at),
919
933
  0
920
934
  );
921
- fetched.forEach(({ stream, events }) => {
935
+ fetched.forEach(({ stream, lagging: lagging2, events }) => {
922
936
  const payloads = events.flatMap((event) => {
923
937
  const register = this.registry.events[event.name] || [];
924
938
  return [...register.reactions.values()].filter((reaction) => {
@@ -930,16 +944,17 @@ var Act = class {
930
944
  lease: {
931
945
  stream,
932
946
  by: (0, import_crypto2.randomUUID)(),
933
- at: events.at(-1)?.id || last_window_at,
947
+ at: events.at(-1)?.id || fetch_window_at,
934
948
  // ff when no matching events
935
- retry: 0
949
+ retry: 0,
950
+ lagging: lagging2
936
951
  },
937
952
  // @ts-expect-error indexed by key
938
953
  payloads
939
954
  });
940
955
  });
941
956
  const leased = await store().lease(
942
- [...leases.values()].map((l) => l.lease),
957
+ [...leases.values()].map(({ lease }) => lease),
943
958
  leaseMillis
944
959
  );
945
960
  tracer.leased(leased);
@@ -948,6 +963,17 @@ var Act = class {
948
963
  (lease) => this.handle(lease, leases.get(lease.stream).payloads)
949
964
  )
950
965
  );
966
+ const [lagging_handled, leading_handled] = handled.reduce(
967
+ ([lagging_handled2, leading_handled2], { lease, handled: handled2 }) => [
968
+ lagging_handled2 + (lease.lagging ? handled2 : 0),
969
+ leading_handled2 + (lease.lagging ? 0 : handled2)
970
+ ],
971
+ [0, 0]
972
+ );
973
+ const lagging_avg = lagging > 0 ? lagging_handled / lagging : 0;
974
+ const leading_avg = leading > 0 ? leading_handled / leading : 0;
975
+ const total = lagging_avg + leading_avg;
976
+ this._drain_lag2lead_ratio = total > 0 ? Math.max(0.2, Math.min(0.8, lagging_avg / total)) : 0.5;
951
977
  const acked = await store().ack(
952
978
  handled.filter(({ error }) => !error).map(({ at, lease }) => ({ ...lease, at }))
953
979
  );
@@ -998,6 +1024,7 @@ var Act = class {
998
1024
  by: (0, import_crypto2.randomUUID)(),
999
1025
  at: 0,
1000
1026
  retry: 0,
1027
+ lagging: true,
1001
1028
  payloads
1002
1029
  }));
1003
1030
  const leased = await store().lease(leases, 0);