@reventlessdev/reventless-core 3.0.0-alpha.315 → 3.0.0-alpha.316

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.
@@ -45,6 +45,64 @@ module Make = (
45
45
  producedEventTypes->Array.filter(t => !(queryEventTypes->Array.includes(t))),
46
46
  )
47
47
 
48
+ /**
49
+ The produced events with their personal fields sealed by the installed provider:
50
+ the one form both the stored event and the published message carry. Refused,
51
+ never degraded, when a field cannot be sealed or comes out unsealed; the detail
52
+ names the event type and the field, never the value.
53
+ */
54
+ let sealRawEvents = async (raws: array<ReventlessInfra.DcbEventLog.rawEvent>): result<
55
+ array<ReventlessInfra.DcbEventLog.rawEvent>,
56
+ string,
57
+ > => {
58
+ let out = []
59
+ let failure = ref(None)
60
+ for i in 0 to raws->Array.length - 1 {
61
+ let raw = raws->Array.getUnsafe(i)
62
+ switch (failure.contents, raw.data) {
63
+ | (None, Object(data)) =>
64
+ switch await Reventless.Pii.sealData(Spec.eventSchema, ~eventType=raw.eventType, data) {
65
+ | Error(e) =>
66
+ failure := Some(`cannot seal ${raw.eventType}: ${e->Reventless.Pii.errorMessage}`)
67
+ | Ok(sealed) =>
68
+ switch Reventless.Pii.unsealedField(Spec.eventSchema, ~eventType=raw.eventType, sealed) {
69
+ | Some(field) =>
70
+ failure := Some(`refused ${raw.eventType}: its personal field ${field} is not sealed`)
71
+ | None => out->Array.push({...raw, data: JSON.Object(sealed)})
72
+ }
73
+ }
74
+ | (None, _) => out->Array.push(raw)
75
+ | (Some(_), _) => ()
76
+ }
77
+ }
78
+ switch failure.contents {
79
+ | Some(msg) => Error(msg)
80
+ | None => Ok(out)
81
+ }
82
+ }
83
+
84
+ /** A read event with its sealed personal fields opened, before the decoder
85
+ reads it. Most events hold nothing sealed and pass without a promise. */
86
+ let openRaw = (raw: ReventlessInfra.DcbEventLog.rawSequencedEvent) =>
87
+ switch raw.data {
88
+ | Object(data)
89
+ if Reventless.Pii.needsOpen(Spec.consumedEventSchema, ~eventType=raw.eventType, data) =>
90
+ Effect.promise(async () =>
91
+ switch await Reventless.Pii.openData(
92
+ Spec.consumedEventSchema,
93
+ ~eventType=raw.eventType,
94
+ data,
95
+ ) {
96
+ | Ok(opened) => {...raw, data: JSON.Object(opened)}
97
+ | Error(e) =>
98
+ JsError.throwWithMessage(
99
+ `cannot open ${raw.eventType}: ${e->Reventless.Pii.errorMessage}`,
100
+ )
101
+ }
102
+ )
103
+ | _ => Effect.succeed(raw)
104
+ }
105
+
48
106
  let encodeEvent = (
49
107
  ~parentMeta: Message.meta,
50
108
  ~tagKeysByEventType: Dict.t<array<string>>,
@@ -179,7 +237,7 @@ module Make = (
179
237
  let projectionCache: Lru.t<
180
238
  string,
181
239
  (Decision.state, option<Reventless.DcbTag.sequencePosition>, array<string>),
182
- > = Lru.make(~capacity=projectionCacheCapacity)
240
+ > = Lru.make(~capacity=projectionCacheCapacity, ~ttlMs=Lru.decodedStateTtlMs())
183
241
 
184
242
  let resetCache = () => projectionCache->Lru.clear
185
243
 
@@ -345,6 +403,7 @@ module Make = (
345
403
  }
346
404
 
347
405
  dcbEventLog.readStream(~query, ~after=?afterPos, ~strongConsistency)
406
+ ->Stream.mapEffect(openRaw)
348
407
  // The head accumulator starts at `afterPos` (the seeded head), not `None`,
349
408
  // so an empty delta read keeps the cached head — the append must condition
350
409
  // on the head that actually exists, not assert "no events exist yet".
@@ -457,107 +516,128 @@ module Make = (
457
516
  )
458
517
  EffectLogger.logInfo(~comp, "no events produced")->Effect.map(_ => Ok("ok"))
459
518
  | Ok(newEvents) =>
460
- let rawEvents =
519
+ let encoded =
461
520
  newEvents->Array.map(
462
521
  e => encodeEvent(~parentMeta=command'.meta, ~tagKeysByEventType, e),
463
522
  )
464
- let eventCount = rawEvents->Array.length->Int.toString
465
- let eventDetails =
466
- rawEvents
467
- ->Array.map(
468
- e => {
469
- let fields = switch e.data {
470
- | Object(dict) =>
471
- let f =
472
- dict
473
- ->Dict.toArray
474
- ->Array.map(((k, v)) => `${k}:${v->JSON.stringify}`)
475
- ->Array.join(",")
476
- f == "" ? "" : `({${f}})`
477
- | _ => ""
478
- }
479
- `${LogFormat.bold(e.eventType)}${fields}`
480
- },
481
- )
482
- ->Array.join(", ")
483
- let eventJsons = rawEvents->Array.map(e => e.data)->JSON.Encode.array
484
- let condition: Reventless.DcbTag.appendCondition = {
485
- query,
486
- after: ?headPosition,
487
- }
488
- EffectLogger.logInfo(
489
- ~comp,
490
- ~detail=eventJsons,
491
- `produced ${eventCount} event(s): [${eventDetails}]`,
492
- )
493
- ->Effect.flatMap(_ => Effect.promise(() => dcbEventLog.append(rawEvents, ~condition)))
494
- ->Effect.flatMap(
495
- appendResult =>
496
- switch appendResult {
497
- | Ok(_position) =>
498
- // Cache the decided-on state at the read head (NOT including the
499
- // events we just appended, and NOT the returned position): the
500
- // next command's delta read picks our events up from `headPosition`.
523
+ Effect.promise(() => sealRawEvents(encoded))->Effect.flatMap(
524
+ sealResult =>
525
+ switch sealResult {
526
+ | Error(detail) =>
527
+ // Refused before the append: nothing is stored, and the read
528
+ // snapshot is still valid.
501
529
  cachePut((state, headPosition, owners))
502
- CommandTopic_Helpers.reportAccepted(
530
+ CommandTopic_Helpers.reportRejected(
503
531
  ~component=Spec.name,
532
+ ~cause=InfrastructureFailure,
504
533
  cmdJson.meta.msgId,
505
- switch entityId {
506
- | Some(eid) => {entityId: eid, eventCount: rawEvents->Array.length}
507
- | None => {eventCount: rawEvents->Array.length}
534
+ {
535
+ errorCode: Reventless.CommandOutcome.code(AppendFailed),
536
+ errorDetail: detail,
508
537
  },
509
538
  )
510
- EffectLogger.logInfo(~comp, `append: ${eventCount} event(s)`)->Effect.map(
511
- _ => Ok("ok"),
539
+ EffectLogger.logError(~comp, `append refused: ${detail}`)->Effect.map(
540
+ _ => Error(detail),
512
541
  )
513
- | Error(err) =>
514
- // Typed classification (was substring-matching the error string,
515
- // which disagreed with the backends' casing).
516
- let (errorCode, errDetail) = switch err {
517
- | ReventlessInfra.DcbEventLog.Conflict => (
518
- Reventless.CommandOutcome.code(Conflict),
519
- "conflict: condition check failed",
542
+ | Ok(rawEvents) =>
543
+ let eventCount = rawEvents->Array.length->Int.toString
544
+ // Through the redacting formatter, like every other log line.
545
+ let eventDetails =
546
+ rawEvents
547
+ ->Array.map(
548
+ e =>
549
+ switch e.data {
550
+ | Object(dict) =>
551
+ Message.combineMessage(e.eventType, dict)->LogFormat.cmdLabelOfJson
552
+ | _ => LogFormat.bold(e.eventType)
553
+ },
520
554
  )
521
- | StorageFailure(msg) => (Reventless.CommandOutcome.code(AppendFailed), msg)
555
+ ->Array.join(", ")
556
+ let eventJsons = rawEvents->Array.map(e => e.data)->JSON.Encode.array
557
+ let condition: Reventless.DcbTag.appendCondition = {
558
+ query,
559
+ after: ?headPosition,
522
560
  }
523
- if retries > 0 {
524
- // Re-seed from the just-read snapshot so the retry reads only
525
- // the delta the conflicting writer added, not full history.
526
- seed := Some((state, headPosition, owners))
527
- // Per-slice CloudWatch counter: rising AppendRetry signals a
528
- // slice feeling contention (the metric to watch the eventual
529
- // default with — see dcb-high-contention-handling.md).
530
- Metrics.emitCount(~metric="AppendRetry", ~slice=Spec.name)
531
- EffectLogger.logWarn(
532
- ~comp,
533
- `append failed (retrying ${(maxRetries - retries + 1)
534
- ->Int.toString}/${maxRetries->Int.toString}): ${errDetail}`,
535
- )->Effect.flatMap(_ => attempt(~retries=retries - 1))
536
- } else {
537
- // Retries exhausted — drop any cached snapshot so the next
538
- // command for this entity takes the cold full-read path.
539
- cacheInvalidate()
561
+ EffectLogger.logInfo(
562
+ ~comp,
563
+ ~detail=eventJsons,
564
+ `produced ${eventCount} event(s): [${eventDetails}]`,
565
+ )
566
+ ->Effect.flatMap(
567
+ _ => Effect.promise(() => dcbEventLog.append(rawEvents, ~condition)),
568
+ )
569
+ ->Effect.flatMap(
570
+ appendResult =>
571
+ switch appendResult {
572
+ | Ok(_position) =>
573
+ // Cache the decided-on state at the read head (NOT including the
574
+ // events we just appended, and NOT the returned position): the
575
+ // next command's delta read picks our events up from `headPosition`.
576
+ cachePut((state, headPosition, owners))
577
+ CommandTopic_Helpers.reportAccepted(
578
+ ~component=Spec.name,
579
+ cmdJson.meta.msgId,
580
+ switch entityId {
581
+ | Some(eid) => {entityId: eid, eventCount: rawEvents->Array.length}
582
+ | None => {eventCount: rawEvents->Array.length}
583
+ },
584
+ )
585
+ EffectLogger.logInfo(~comp, `append: ${eventCount} event(s)`)->Effect.map(
586
+ _ => Ok("ok"),
587
+ )
588
+ | Error(err) =>
589
+ // Typed classification (was substring-matching the error string,
590
+ // which disagreed with the backends' casing).
591
+ let (errorCode, errDetail) = switch err {
592
+ | ReventlessInfra.DcbEventLog.Conflict => (
593
+ Reventless.CommandOutcome.code(Conflict),
594
+ "conflict: condition check failed",
595
+ )
596
+ | StorageFailure(msg) => (
597
+ Reventless.CommandOutcome.code(AppendFailed),
598
+ msg,
599
+ )
600
+ }
601
+ if retries > 0 {
602
+ // Re-seed from the just-read snapshot so the retry reads only
603
+ // the delta the conflicting writer added, not full history.
604
+ seed := Some((state, headPosition, owners))
605
+ // Per-slice CloudWatch counter: rising AppendRetry signals a
606
+ // slice feeling contention (the metric to watch the eventual
607
+ // default with — see dcb-high-contention-handling.md).
608
+ Metrics.emitCount(~metric="AppendRetry", ~slice=Spec.name)
609
+ EffectLogger.logWarn(
610
+ ~comp,
611
+ `append failed (retrying ${(maxRetries - retries + 1)
612
+ ->Int.toString}/${maxRetries->Int.toString}): ${errDetail}`,
613
+ )->Effect.flatMap(_ => attempt(~retries=retries - 1))
614
+ } else {
615
+ // Retries exhausted — drop any cached snapshot so the next
616
+ // command for this entity takes the cold full-read path.
617
+ cacheInvalidate()
540
618
 
541
- // A surfaced Conflict means the 3-retry loop could not absorb
542
- // the contention — the operator signal for "this slice is hot"
543
- // (consider sharding / async — Issue 10, §4 of the analysis).
544
- if errorCode == Reventless.CommandOutcome.code(Conflict) {
545
- Metrics.emitCount(~metric="AppendConflict", ~slice=Spec.name)
546
- }
547
- // Both arms here are storage, not the model: a surfaced Conflict is
548
- // contention the retry loop couldn't absorb, and AppendFailed is the
549
- // store failing. `decide` had already said Ok.
550
- CommandTopic_Helpers.reportRejected(
551
- ~component=Spec.name,
552
- ~cause=InfrastructureFailure,
553
- cmdJson.meta.msgId,
554
- {errorCode, errorDetail: errDetail},
555
- )
556
- EffectLogger.logError(
557
- ~comp,
558
- `append failed, retries exhausted: ${errDetail}`,
559
- )->Effect.map(_ => Error(errDetail))
560
- }
619
+ // A surfaced Conflict means the 3-retry loop could not absorb
620
+ // the contention — the operator signal for "this slice is hot"
621
+ // (consider sharding / async — Issue 10, §4 of the analysis).
622
+ if errorCode == Reventless.CommandOutcome.code(Conflict) {
623
+ Metrics.emitCount(~metric="AppendConflict", ~slice=Spec.name)
624
+ }
625
+ // Both arms here are storage, not the model: a surfaced Conflict is
626
+ // contention the retry loop couldn't absorb, and AppendFailed is the
627
+ // store failing. `decide` had already said Ok.
628
+ CommandTopic_Helpers.reportRejected(
629
+ ~component=Spec.name,
630
+ ~cause=InfrastructureFailure,
631
+ cmdJson.meta.msgId,
632
+ {errorCode, errorDetail: errDetail},
633
+ )
634
+ EffectLogger.logError(
635
+ ~comp,
636
+ `append failed, retries exhausted: ${errDetail}`,
637
+ )->Effect.map(_ => Error(errDetail))
638
+ }
639
+ },
640
+ )
561
641
  },
562
642
  )
563
643
  | Error(error) =>
@@ -8,6 +8,8 @@ import * as Id$Reventless from "@reventlessdev/reventless-spec/src/types/Id.res.
8
8
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
9
9
  import * as Effect from "effect/Effect";
10
10
  import * as Stream$1 from "effect/Stream";
11
+ import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
12
+ import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
11
13
  import * as Owner$Reventless from "@reventlessdev/reventless-spec/src/components/Owner.res.mjs";
12
14
  import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
13
15
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
@@ -30,6 +32,72 @@ function Make(Spec) {
30
32
  let queryEventTypes = decoder.eventTypes;
31
33
  let producedEventTypes = DcbTag$Reventless.extractAllVariantNames(Spec.eventSchema);
32
34
  let queryEventTypesWithOwn = queryEventTypes.concat(producedEventTypes.filter(t => !queryEventTypes.includes(t)));
35
+ let sealRawEvents = async raws => {
36
+ let out = [];
37
+ let failure;
38
+ for (let i = 0, i_finish = raws.length; i < i_finish; ++i) {
39
+ let raw = raws[i];
40
+ let match = failure;
41
+ let match$1 = raw.data;
42
+ if (match === undefined) {
43
+ if (typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1)) {
44
+ let e = await Pii$Reventless.sealData(Spec.eventSchema, raw.eventType, match$1);
45
+ if (e.TAG === "Ok") {
46
+ let sealed = e._0;
47
+ let field = Pii$Reventless.unsealedField(Spec.eventSchema, raw.eventType, sealed);
48
+ if (field !== undefined) {
49
+ failure = `refused ` + raw.eventType + `: its personal field ` + field + ` is not sealed`;
50
+ } else {
51
+ out.push({
52
+ eventType: raw.eventType,
53
+ data: sealed,
54
+ tags: raw.tags,
55
+ meta: raw.meta
56
+ });
57
+ }
58
+ } else {
59
+ failure = `cannot seal ` + raw.eventType + `: ` + Pii$Reventless.errorMessage(e._0);
60
+ }
61
+ } else {
62
+ out.push(raw);
63
+ }
64
+ }
65
+ }
66
+ let msg = failure;
67
+ if (msg !== undefined) {
68
+ return {
69
+ TAG: "Error",
70
+ _0: msg
71
+ };
72
+ } else {
73
+ return {
74
+ TAG: "Ok",
75
+ _0: out
76
+ };
77
+ }
78
+ };
79
+ let openRaw = raw => {
80
+ let data = raw.data;
81
+ if (typeof data === "object" && data !== null && !Array.isArray(data) && Pii$Reventless.needsOpen(Spec.consumedEventSchema, raw.eventType, data)) {
82
+ return Effect.promise(async () => {
83
+ let opened = await Pii$Reventless.openData(Spec.consumedEventSchema, raw.eventType, data);
84
+ if (opened.TAG === "Ok") {
85
+ return {
86
+ position: raw.position,
87
+ eventType: raw.eventType,
88
+ data: opened._0,
89
+ tags: raw.tags,
90
+ meta: raw.meta,
91
+ recordedAt: raw.recordedAt
92
+ };
93
+ } else {
94
+ return Stdlib_JsError.throwWithMessage(`cannot open ` + raw.eventType + `: ` + Pii$Reventless.errorMessage(opened._0));
95
+ }
96
+ });
97
+ } else {
98
+ return Effect.succeed(raw);
99
+ }
100
+ };
33
101
  let slicePartitionTag = Stdlib_Lazy.make(() => {
34
102
  try {
35
103
  return DcbTag$Reventless.deriveSlicePartition({
@@ -78,7 +146,7 @@ function Make(Spec) {
78
146
  return v;
79
147
  }
80
148
  };
81
- let projectionCache = Lru$ReventlessCore.make(100);
149
+ let projectionCache = Lru$ReventlessCore.make(100, Lru$ReventlessCore.decodedStateTtlMs(), undefined);
82
150
  let resetCache = () => Lru$ReventlessCore.clear(projectionCache);
83
151
  let handleSingleCommand = (tagKeysByEventType, crossPartitionTagKeys, payloadTagKeys, partitionTag, dcbEventLog, command$p) => {
84
152
  let cmdJson = Message$ReventlessCore.commandJsonOfCommand$p(Id$Reventless.$$String.toString, Spec.commandSchema, command$p);
@@ -179,7 +247,7 @@ function Make(Spec) {
179
247
  strongConsistency = false;
180
248
  break;
181
249
  }
182
- return Effect.flatMap(Effect.tap(Stream$1.runFold(dcbEventLog.readStream(query, afterPos, strongConsistency), [
250
+ return Effect.flatMap(Effect.tap(Stream$1.runFold(Stream$1.mapEffect(dcbEventLog.readStream(query, afterPos, strongConsistency), openRaw), [
183
251
  match$1[0],
184
252
  afterPos,
185
253
  [],
@@ -272,7 +340,7 @@ function Make(Spec) {
272
340
  _0: "ok"
273
341
  }));
274
342
  }
275
- let rawEvents = newEvents$1.map(e => {
343
+ let encoded = newEvents$1.map(e => {
276
344
  let parentMeta = command$p.meta;
277
345
  let json = Util_Sury$Reventless.toJson(e, Spec.eventSchema);
278
346
  let match = Message$ReventlessCore.splitMessage(json);
@@ -288,74 +356,90 @@ function Make(Spec) {
288
356
  meta: meta
289
357
  };
290
358
  });
291
- let eventCount = rawEvents.length.toString();
292
- let eventDetails = rawEvents.map(e => {
293
- let dict = e.data;
294
- let fields;
295
- if (typeof dict === "object" && dict !== null && !Array.isArray(dict)) {
296
- let f = Object.entries(dict).map(param => param[0] + `:` + JSON.stringify(param[1])).join(",");
297
- fields = f === "" ? "" : `({` + f + `})`;
298
- } else {
299
- fields = "";
300
- }
301
- return LogFormat$ReventlessCore.bold(e.eventType) + fields;
302
- }).join(", ");
303
- let eventJsons = rawEvents.map(e => e.data);
304
- let condition = {
305
- query: query,
306
- after: headPosition
307
- };
308
- return Effect.flatMap(Effect.flatMap(EffectLogger$ReventlessCore.logInfo(comp, eventJsons, `produced ` + eventCount + ` event(s): [` + eventDetails + `]`), () => Effect.promise(() => dcbEventLog.append(rawEvents, condition))), appendResult => {
309
- if (appendResult.TAG === "Ok") {
310
- cachePut([
311
- state,
312
- headPosition,
313
- owners
314
- ]);
315
- CommandTopic_Helpers$ReventlessCore.reportAccepted(Spec.name, cmdJson.meta.msgId, entityId !== undefined ? ({
316
- entityId: entityId,
317
- eventCount: rawEvents.length
318
- }) : ({
319
- eventCount: rawEvents.length
320
- }));
321
- return Effect.map(EffectLogger$ReventlessCore.logInfo(comp, undefined, `append: ` + eventCount + ` event(s)`), () => ({
322
- TAG: "Ok",
323
- _0: "ok"
324
- }));
325
- }
326
- let err = appendResult._0;
327
- let match;
328
- match = typeof err !== "object" ? [
329
- CommandOutcome$Reventless.code("Conflict"),
330
- "conflict: condition check failed"
331
- ] : [
332
- CommandOutcome$Reventless.code("AppendFailed"),
333
- err._0
334
- ];
335
- let errDetail = match[1];
336
- let errorCode = match[0];
337
- if (retries > 0) {
338
- seed.contents = [
339
- state,
340
- headPosition,
341
- owners
342
- ];
343
- Metrics$ReventlessCore.emitCount("AppendRetry", Spec.name, undefined);
344
- return Effect.flatMap(EffectLogger$ReventlessCore.logWarn(comp, undefined, `append failed (retrying ` + ((3 - retries | 0) + 1 | 0).toString() + `/` + (3).toString() + `): ` + errDetail), () => attempt(retries - 1 | 0));
345
- } else {
346
- cacheInvalidate();
347
- if (errorCode === CommandOutcome$Reventless.code("Conflict")) {
348
- Metrics$ReventlessCore.emitCount("AppendConflict", Spec.name, undefined);
349
- }
350
- CommandTopic_Helpers$ReventlessCore.reportRejected(Spec.name, "InfrastructureFailure", cmdJson.meta.msgId, {
351
- errorCode: errorCode,
352
- errorDetail: errDetail
359
+ return Effect.flatMap(Effect.promise(() => sealRawEvents(encoded)), sealResult => {
360
+ if (sealResult.TAG === "Ok") {
361
+ let rawEvents = sealResult._0;
362
+ let eventCount = rawEvents.length.toString();
363
+ let eventDetails = rawEvents.map(e => {
364
+ let dict = e.data;
365
+ if (typeof dict === "object" && dict !== null && !Array.isArray(dict)) {
366
+ return LogFormat$ReventlessCore.cmdLabelOfJson(Message$ReventlessCore.combineMessage(e.eventType, dict));
367
+ } else {
368
+ return LogFormat$ReventlessCore.bold(e.eventType);
369
+ }
370
+ }).join(", ");
371
+ let eventJsons = rawEvents.map(e => e.data);
372
+ let condition = {
373
+ query: query,
374
+ after: headPosition
375
+ };
376
+ return Effect.flatMap(Effect.flatMap(EffectLogger$ReventlessCore.logInfo(comp, eventJsons, `produced ` + eventCount + ` event(s): [` + eventDetails + `]`), () => Effect.promise(() => dcbEventLog.append(rawEvents, condition))), appendResult => {
377
+ if (appendResult.TAG === "Ok") {
378
+ cachePut([
379
+ state,
380
+ headPosition,
381
+ owners
382
+ ]);
383
+ CommandTopic_Helpers$ReventlessCore.reportAccepted(Spec.name, cmdJson.meta.msgId, entityId !== undefined ? ({
384
+ entityId: entityId,
385
+ eventCount: rawEvents.length
386
+ }) : ({
387
+ eventCount: rawEvents.length
388
+ }));
389
+ return Effect.map(EffectLogger$ReventlessCore.logInfo(comp, undefined, `append: ` + eventCount + ` event(s)`), () => ({
390
+ TAG: "Ok",
391
+ _0: "ok"
392
+ }));
393
+ }
394
+ let err = appendResult._0;
395
+ let match;
396
+ match = typeof err !== "object" ? [
397
+ CommandOutcome$Reventless.code("Conflict"),
398
+ "conflict: condition check failed"
399
+ ] : [
400
+ CommandOutcome$Reventless.code("AppendFailed"),
401
+ err._0
402
+ ];
403
+ let errDetail = match[1];
404
+ let errorCode = match[0];
405
+ if (retries > 0) {
406
+ seed.contents = [
407
+ state,
408
+ headPosition,
409
+ owners
410
+ ];
411
+ Metrics$ReventlessCore.emitCount("AppendRetry", Spec.name, undefined);
412
+ return Effect.flatMap(EffectLogger$ReventlessCore.logWarn(comp, undefined, `append failed (retrying ` + ((3 - retries | 0) + 1 | 0).toString() + `/` + (3).toString() + `): ` + errDetail), () => attempt(retries - 1 | 0));
413
+ } else {
414
+ cacheInvalidate();
415
+ if (errorCode === CommandOutcome$Reventless.code("Conflict")) {
416
+ Metrics$ReventlessCore.emitCount("AppendConflict", Spec.name, undefined);
417
+ }
418
+ CommandTopic_Helpers$ReventlessCore.reportRejected(Spec.name, "InfrastructureFailure", cmdJson.meta.msgId, {
419
+ errorCode: errorCode,
420
+ errorDetail: errDetail
421
+ });
422
+ return Effect.map(EffectLogger$ReventlessCore.logError(comp, undefined, `append failed, retries exhausted: ` + errDetail), () => ({
423
+ TAG: "Error",
424
+ _0: errDetail
425
+ }));
426
+ }
353
427
  });
354
- return Effect.map(EffectLogger$ReventlessCore.logError(comp, undefined, `append failed, retries exhausted: ` + errDetail), () => ({
355
- TAG: "Error",
356
- _0: errDetail
357
- }));
358
428
  }
429
+ let detail = sealResult._0;
430
+ cachePut([
431
+ state,
432
+ headPosition,
433
+ owners
434
+ ]);
435
+ CommandTopic_Helpers$ReventlessCore.reportRejected(Spec.name, "InfrastructureFailure", cmdJson.meta.msgId, {
436
+ errorCode: CommandOutcome$Reventless.code("AppendFailed"),
437
+ errorDetail: detail
438
+ });
439
+ return Effect.map(EffectLogger$ReventlessCore.logError(comp, undefined, `append refused: ` + detail), () => ({
440
+ TAG: "Error",
441
+ _0: detail
442
+ }));
359
443
  });
360
444
  }
361
445
  cachePut([
package/src/util/Lru.res CHANGED
@@ -5,22 +5,45 @@
5
5
  // least-recently-used key as the first element of `Map.keys`. `put` evicts that
6
6
  // first key once `size` exceeds `capacity`.
7
7
 
8
+ // An entry also carries when it stops being served: decoded state holds personal
9
+ // values in the clear, and an erased person's must leave a warm container within a
10
+ // bound the erasure promise can name, not whenever the entry happens to be evicted.
11
+
8
12
  type t<'k, 'v> = {
9
13
  capacity: int,
10
- store: Map.t<'k, 'v>,
14
+ ttlMs: option<float>,
15
+ now: unit => float,
16
+ store: Map.t<'k, ('v, float)>,
11
17
  }
12
18
 
13
- let make = (~capacity: int): t<'k, 'v> => {
19
+ @val external env: dict<string> = "process.env"
20
+
21
+ /** The time limit a cache of decoded state takes unless it says otherwise: five
22
+ minutes, or `REVENTLESS_DECODED_CACHE_TTL_SECONDS`. */
23
+ let decodedStateTtlMs = (): float =>
24
+ env
25
+ ->Dict.get("REVENTLESS_DECODED_CACHE_TTL_SECONDS")
26
+ ->Option.flatMap(Float.fromString)
27
+ ->Option.filter(s => s > 0.)
28
+ ->Option.getOr(300.) *. 1000.
29
+
30
+ let make = (~capacity: int, ~ttlMs: option<float>=?, ~now: unit => float=Date.now): t<'k, 'v> => {
14
31
  capacity,
32
+ ttlMs,
33
+ now,
15
34
  store: Map.make(),
16
35
  }
17
36
 
18
37
  let get = (cache: t<'k, 'v>, key: 'k): option<'v> =>
19
38
  switch cache.store->Map.get(key) {
20
- | Some(value) =>
21
- // Touch: re-insert so this key becomes most-recently-used.
39
+ | Some((_, expiresAt)) if cache.now() >= expiresAt =>
22
40
  let _ = cache.store->Map.delete(key)
23
- cache.store->Map.set(key, value)
41
+ None
42
+ | Some((value, expiresAt)) =>
43
+ // Touch: re-insert so this key becomes most-recently-used. The time limit runs
44
+ // from the put, not from the last read, so a hot key still expires.
45
+ let _ = cache.store->Map.delete(key)
46
+ cache.store->Map.set(key, (value, expiresAt))
24
47
  Some(value)
25
48
  | None => None
26
49
  }
@@ -32,7 +55,11 @@ let put = (cache: t<'k, 'v>, key: 'k, value: 'v): unit =>
32
55
  } else {
33
56
  // Delete first so an update also moves the key to most-recently-used.
34
57
  let _ = cache.store->Map.delete(key)
35
- cache.store->Map.set(key, value)
58
+ let expiresAt = switch cache.ttlMs {
59
+ | Some(ttl) => cache.now() +. ttl
60
+ | None => Float.Constants.positiveInfinity
61
+ }
62
+ cache.store->Map.set(key, (value, expiresAt))
36
63
  if cache.store->Map.size > cache.capacity {
37
64
  // Evict the least-recently-used entry = oldest insertion = first key.
38
65
  switch cache.store->Map.keys->Array.fromIterator->Array.get(0) {