@cratis/chronicle 6.61.0 → 6.63.0
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/ChronicleClient.ts +86 -3
- package/connection/ChronicleConnection.ts +7 -1
- package/connection/ChronicleServices.ts +2 -0
- package/connection/ConnectionLifecycle.ts +42 -1
- package/dist/ChronicleClient.d.ts +28 -1
- package/dist/ChronicleClient.d.ts.map +1 -1
- package/dist/ChronicleClient.js +71 -3
- package/dist/ChronicleClient.js.map +1 -1
- package/dist/connection/ChronicleConnection.d.ts +1 -0
- package/dist/connection/ChronicleConnection.d.ts.map +1 -1
- package/dist/connection/ChronicleConnection.js +6 -2
- package/dist/connection/ChronicleConnection.js.map +1 -1
- package/dist/connection/ChronicleServices.d.ts +2 -1
- package/dist/connection/ChronicleServices.d.ts.map +1 -1
- package/dist/connection/ConnectionLifecycle.d.ts +24 -0
- package/dist/connection/ConnectionLifecycle.d.ts.map +1 -1
- package/dist/connection/ConnectionLifecycle.js +40 -1
- package/dist/connection/ConnectionLifecycle.js.map +1 -1
- package/dist/eventSequences/EventForEventSourceId.d.ts +21 -1
- package/dist/eventSequences/EventForEventSourceId.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.d.ts +9 -0
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +18 -0
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/IEventSequence.d.ts +30 -0
- package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
- package/dist/imports/EventImport.d.ts +29 -0
- package/dist/imports/EventImport.d.ts.map +1 -0
- package/dist/imports/EventImport.js +248 -0
- package/dist/imports/EventImport.js.map +1 -0
- package/dist/imports/EventImportSettings.d.ts +32 -0
- package/dist/imports/EventImportSettings.d.ts.map +1 -0
- package/dist/imports/EventImportSettings.js +4 -0
- package/dist/imports/EventImportSettings.js.map +1 -0
- package/dist/imports/IEventImport.d.ts +50 -0
- package/dist/imports/IEventImport.d.ts.map +1 -0
- package/dist/imports/IEventImport.js +4 -0
- package/dist/imports/IEventImport.js.map +1 -0
- package/dist/imports/ImportChunkRefused.d.ts +12 -0
- package/dist/imports/ImportChunkRefused.d.ts.map +1 -0
- package/dist/imports/ImportChunkRefused.js +19 -0
- package/dist/imports/ImportChunkRefused.js.map +1 -0
- package/dist/imports/ImportCouldNotBegin.d.ts +10 -0
- package/dist/imports/ImportCouldNotBegin.d.ts.map +1 -0
- package/dist/imports/ImportCouldNotBegin.js +16 -0
- package/dist/imports/ImportCouldNotBegin.js.map +1 -0
- package/dist/imports/ImportOptions.d.ts +26 -0
- package/dist/imports/ImportOptions.d.ts.map +1 -0
- package/dist/imports/ImportOptions.js +4 -0
- package/dist/imports/ImportOptions.js.map +1 -0
- package/dist/imports/ImportProgress.d.ts +20 -0
- package/dist/imports/ImportProgress.d.ts.map +1 -0
- package/dist/imports/ImportProgress.js +4 -0
- package/dist/imports/ImportProgress.js.map +1 -0
- package/dist/imports/ImportRejection.d.ts +10 -0
- package/dist/imports/ImportRejection.d.ts.map +1 -0
- package/dist/imports/ImportRejection.js +4 -0
- package/dist/imports/ImportRejection.js.map +1 -0
- package/dist/imports/ImportResult.d.ts +30 -0
- package/dist/imports/ImportResult.d.ts.map +1 -0
- package/dist/imports/ImportResult.js +4 -0
- package/dist/imports/ImportResult.js.map +1 -0
- package/dist/imports/ImportState.d.ts +18 -0
- package/dist/imports/ImportState.d.ts.map +1 -0
- package/dist/imports/ImportState.js +21 -0
- package/dist/imports/ImportState.js.map +1 -0
- package/dist/imports/ImportTarget.d.ts +16 -0
- package/dist/imports/ImportTarget.d.ts.map +1 -0
- package/dist/imports/ImportTarget.js +4 -0
- package/dist/imports/ImportTarget.js.map +1 -0
- package/dist/imports/ImportWasAbandoned.d.ts +11 -0
- package/dist/imports/ImportWasAbandoned.d.ts.map +1 -0
- package/dist/imports/ImportWasAbandoned.js +16 -0
- package/dist/imports/ImportWasAbandoned.js.map +1 -0
- package/dist/imports/ImportsNotSupported.d.ts +5 -0
- package/dist/imports/ImportsNotSupported.d.ts.map +1 -0
- package/dist/imports/ImportsNotSupported.js +10 -0
- package/dist/imports/ImportsNotSupported.js.map +1 -0
- package/dist/imports/OnInvalidEvent.d.ts +8 -0
- package/dist/imports/OnInvalidEvent.d.ts.map +1 -0
- package/dist/imports/OnInvalidEvent.js +11 -0
- package/dist/imports/OnInvalidEvent.js.map +1 -0
- package/dist/imports/abortable.d.ts +23 -0
- package/dist/imports/abortable.d.ts.map +1 -0
- package/dist/imports/abortable.js +60 -0
- package/dist/imports/abortable.js.map +1 -0
- package/dist/imports/beginEventImport.d.ts +16 -0
- package/dist/imports/beginEventImport.d.ts.map +1 -0
- package/dist/imports/beginEventImport.js +74 -0
- package/dist/imports/beginEventImport.js.map +1 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.d.ts +144 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.d.ts.map +1 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.js +132 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.js.map +1 -0
- package/dist/imports/importEvents.d.ts +13 -0
- package/dist/imports/importEvents.d.ts.map +1 -0
- package/dist/imports/importEvents.js +35 -0
- package/dist/imports/importEvents.js.map +1 -0
- package/dist/imports/index.d.ts +12 -0
- package/dist/imports/index.d.ts.map +1 -0
- package/dist/imports/index.js +9 -0
- package/dist/imports/index.js.map +1 -0
- package/dist/imports/isTransientImportFailure.d.ts +7 -0
- package/dist/imports/isTransientImportFailure.d.ts.map +1 -0
- package/dist/imports/isTransientImportFailure.js +13 -0
- package/dist/imports/isTransientImportFailure.js.map +1 -0
- package/dist/imports/sizeOfImportEvent.d.ts +9 -0
- package/dist/imports/sizeOfImportEvent.d.ts.map +1 -0
- package/dist/imports/sizeOfImportEvent.js +25 -0
- package/dist/imports/sizeOfImportEvent.js.map +1 -0
- package/dist/imports/toImportEvent.d.ts +14 -0
- package/dist/imports/toImportEvent.d.ts.map +1 -0
- package/dist/imports/toImportEvent.js +54 -0
- package/dist/imports/toImportEvent.js.map +1 -0
- package/dist/imports/toImportProgress.d.ts +9 -0
- package/dist/imports/toImportProgress.d.ts.map +1 -0
- package/dist/imports/toImportProgress.js +20 -0
- package/dist/imports/toImportProgress.js.map +1 -0
- package/dist/imports/toImportResult.d.ts +9 -0
- package/dist/imports/toImportResult.d.ts.map +1 -0
- package/dist/imports/toImportResult.js +33 -0
- package/dist/imports/toImportResult.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/observation/ObservationReconnectBackoff.d.ts +64 -0
- package/dist/observation/ObservationReconnectBackoff.d.ts.map +1 -0
- package/dist/observation/ObservationReconnectBackoff.js +75 -0
- package/dist/observation/ObservationReconnectBackoff.js.map +1 -0
- package/dist/observation/ObservationStreamOutcome.d.ts +10 -0
- package/dist/observation/ObservationStreamOutcome.d.ts.map +1 -0
- package/dist/observation/ObservationStreamOutcome.js +4 -0
- package/dist/observation/ObservationStreamOutcome.js.map +1 -0
- package/dist/observation/classifyStreamError.d.ts +25 -0
- package/dist/observation/classifyStreamError.d.ts.map +1 -0
- package/dist/observation/classifyStreamError.js +55 -0
- package/dist/observation/classifyStreamError.js.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.d.ts +13 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.d.ts.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.js +25 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.js.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.d.ts +27 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.d.ts.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.js +36 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.js.map +1 -0
- package/dist/observation/index.d.ts +2 -0
- package/dist/observation/index.d.ts.map +1 -1
- package/dist/observation/index.js +1 -0
- package/dist/observation/index.js.map +1 -1
- package/dist/reactors/Reactors.d.ts +15 -5
- package/dist/reactors/Reactors.d.ts.map +1 -1
- package/dist/reactors/Reactors.js +95 -18
- package/dist/reactors/Reactors.js.map +1 -1
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.d.ts +26 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.d.ts.map +1 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.js +64 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.js.map +1 -0
- package/dist/reducers/Reducers.d.ts +11 -5
- package/dist/reducers/Reducers.d.ts.map +1 -1
- package/dist/reducers/Reducers.js +93 -19
- package/dist/reducers/Reducers.js.map +1 -1
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.d.ts +25 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.d.ts.map +1 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.js +70 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.js.map +1 -0
- package/dist/testing/BatchPartitionOutcome.d.ts +7 -0
- package/dist/testing/BatchPartitionOutcome.d.ts.map +1 -0
- package/dist/testing/BatchPartitionOutcome.js +4 -0
- package/dist/testing/BatchPartitionOutcome.js.map +1 -0
- package/dist/testing/InProcessEventSequence.d.ts +4 -0
- package/dist/testing/InProcessEventSequence.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.js +2 -0
- package/dist/testing/InProcessEventSequence.js.map +1 -1
- package/dist/testing/ReactorDeliveryResult.d.ts +2 -0
- package/dist/testing/ReactorDeliveryResult.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.d.ts +46 -10
- package/dist/testing/ReactorScenario.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.js +120 -29
- package/dist/testing/ReactorScenario.js.map +1 -1
- package/dist/testing/deliverToBatchMethods.d.ts +35 -0
- package/dist/testing/deliverToBatchMethods.d.ts.map +1 -0
- package/dist/testing/deliverToBatchMethods.js +55 -0
- package/dist/testing/deliverToBatchMethods.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eventSequences/EventForEventSourceId.ts +25 -1
- package/eventSequences/EventSequence.ts +28 -0
- package/eventSequences/IEventSequence.ts +33 -0
- package/for_ChronicleClient/when_reconnecting/and_an_observation_stream_lost_its_session.spec.ts +212 -0
- package/imports/EventImport.ts +263 -0
- package/imports/EventImportSettings.ts +46 -0
- package/imports/IEventImport.ts +58 -0
- package/imports/ImportChunkRefused.ts +15 -0
- package/imports/ImportCouldNotBegin.ts +14 -0
- package/imports/ImportOptions.ts +36 -0
- package/imports/ImportProgress.ts +29 -0
- package/imports/ImportRejection.ts +14 -0
- package/imports/ImportResult.ts +43 -0
- package/imports/ImportState.ts +26 -0
- package/imports/ImportTarget.ts +23 -0
- package/imports/ImportWasAbandoned.ts +16 -0
- package/imports/ImportsNotSupported.ts +10 -0
- package/imports/OnInvalidEvent.ts +11 -0
- package/imports/abortable.ts +57 -0
- package/imports/beginEventImport.ts +85 -0
- package/imports/for_EventImport/given/a_kernel_import_session.ts +160 -0
- package/imports/for_EventImport/when_a_chunk_fails.spec.ts +65 -0
- package/imports/for_EventImport/when_abandoning.spec.ts +52 -0
- package/imports/for_EventImport/when_cancelling_with_an_abort_signal.spec.ts +59 -0
- package/imports/for_EventImport/when_completing.spec.ts +71 -0
- package/imports/for_EventImport/when_cutting_events_into_chunks.spec.ts +70 -0
- package/imports/for_EventImport/when_keeping_chunks_in_flight.spec.ts +49 -0
- package/imports/for_beginEventImport/when_reattaching.spec.ts +82 -0
- package/imports/for_beginEventImport/when_the_kernel_does_not_serve_imports.spec.ts +55 -0
- package/imports/for_importEvents/when_importing_in_one_call.spec.ts +99 -0
- package/imports/for_toImportEvent/when_converting_an_event.spec.ts +74 -0
- package/imports/importEvents.ts +44 -0
- package/imports/index.ts +14 -0
- package/imports/isTransientImportFailure.ts +15 -0
- package/imports/sizeOfImportEvent.ts +25 -0
- package/imports/toImportEvent.ts +60 -0
- package/imports/toImportProgress.ts +24 -0
- package/imports/toImportResult.ts +36 -0
- package/index.ts +3 -0
- package/observation/ObservationReconnectBackoff.ts +109 -0
- package/observation/ObservationStreamOutcome.ts +12 -0
- package/observation/classifyStreamError.ts +67 -0
- package/observation/for_ObservationReconnectBackoff/given/a_backoff.ts +29 -0
- package/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.ts +52 -0
- package/observation/for_ObservationReconnectBackoff/when_a_stream_delivered_before_it_ended.spec.ts +23 -0
- package/observation/for_ObservationReconnectBackoff/when_a_stream_stayed_open_before_it_ended.spec.ts +25 -0
- package/observation/for_ObservationReconnectBackoff/when_jittering_the_delay.spec.ts +24 -0
- package/observation/for_ObservationReconnectBackoff/when_streams_keep_ending_without_working.spec.ts +24 -0
- package/observation/for_ObservationReconnectBackoff/when_the_base_delay_is_not_positive.spec.ts +18 -0
- package/observation/for_ObservationReconnectBackoff/when_the_settings_are_not_finite.spec.ts +32 -0
- package/observation/for_ObservationReconnectBackoff/when_using_the_defaults.spec.ts +29 -0
- package/observation/for_classifyStreamError/when_classifying_a_stream_error.spec.ts +36 -0
- package/observation/index.ts +2 -0
- package/package.json +7 -2
- package/projections/for_childrenOrderingSupport/when_detecting_support_in_the_pinned_contracts.spec.ts +1 -1
- package/reactors/Reactors.ts +101 -18
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_handler_failed_with_a_lost_session.spec.ts +32 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_replaced_stream_fails_late.spec.ts +28 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_stream_delivered_before_it_ended.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_stream_stayed_open_before_it_ended.spec.ts +23 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_it_is_disposed_while_waiting_to_reopen.spec.ts +27 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_streams_keep_ending_without_working.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_client_reconnected_after_streams_kept_ending.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_client_reconnected_while_waiting_to_reopen.spec.ts +25 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_kernel_lost_the_session.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.ts +69 -0
- package/reducers/Reducers.ts +99 -19
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_reducer_failed_with_a_lost_session.spec.ts +33 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_replaced_stream_fails_late.spec.ts +28 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_stream_delivered_before_it_ended.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_stream_stayed_open_before_it_ended.spec.ts +23 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_it_is_disposed_while_waiting_to_reopen.spec.ts +27 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_streams_keep_ending_without_working.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_client_reconnected_after_streams_kept_ending.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_client_reconnected_while_waiting_to_reopen.spec.ts +25 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_kernel_lost_the_session.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.ts +69 -0
- package/testing/BatchPartitionOutcome.ts +10 -0
- package/testing/InProcessEventSequence.ts +4 -0
- package/testing/ReactorDeliveryResult.ts +2 -0
- package/testing/ReactorScenario.ts +147 -39
- package/testing/deliverToBatchMethods.ts +90 -0
- package/testing/for_ReactorScenario/when_the_reactor_has_a_batch_method.spec.ts +143 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cratis/chronicle",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.63.0",
|
|
4
4
|
"description": "Event sourcing for TypeScript and Node.js — the idiomatic client for Cratis Chronicle, the open-source event-sourcing database",
|
|
5
5
|
"author": "Cratis",
|
|
6
6
|
"license": "MIT",
|
|
@@ -165,6 +165,11 @@
|
|
|
165
165
|
"types": "./dist/transactions/index.d.ts",
|
|
166
166
|
"import": "./dist/transactions/index.js",
|
|
167
167
|
"default": "./dist/transactions/index.js"
|
|
168
|
+
},
|
|
169
|
+
"./imports": {
|
|
170
|
+
"types": "./dist/imports/index.d.ts",
|
|
171
|
+
"import": "./dist/imports/index.js",
|
|
172
|
+
"default": "./dist/imports/index.js"
|
|
168
173
|
}
|
|
169
174
|
},
|
|
170
175
|
"scripts": {
|
|
@@ -178,7 +183,7 @@
|
|
|
178
183
|
},
|
|
179
184
|
"dependencies": {
|
|
180
185
|
"@bufbuild/protobuf": "^2.16.0",
|
|
181
|
-
"@cratis/chronicle.contracts": "19.
|
|
186
|
+
"@cratis/chronicle.contracts": "19.153.0",
|
|
182
187
|
"@grpc/grpc-js": "^1.14.5",
|
|
183
188
|
"@opentelemetry/api": "^1.9.1",
|
|
184
189
|
"glob": "^13.0.6",
|
|
@@ -9,7 +9,7 @@ chai.should();
|
|
|
9
9
|
|
|
10
10
|
// The contracts version this expectation was verified against. Bumping the contracts fails the first spec on purpose:
|
|
11
11
|
// re-check children ordering support for the new version, then update both the version and the expectation together.
|
|
12
|
-
const verifiedContractsVersion = '19.
|
|
12
|
+
const verifiedContractsVersion = '19.153.0';
|
|
13
13
|
const supportedInVerifiedVersion = true;
|
|
14
14
|
|
|
15
15
|
const pinnedContractsVersion = (JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')) as {
|
package/reactors/Reactors.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { createLogger } from '../logging/createLogger.js';
|
|
|
6
6
|
import { runInBackgroundContext } from '../telemetry/runInBackgroundContext.js';
|
|
7
7
|
import { Constructor } from '@cratis/fundamentals';
|
|
8
8
|
import { EventGenerationDelivery, ReactorMessage } from '@cratis/chronicle.contracts';
|
|
9
|
+
import type { EventsToObserve } from '@cratis/chronicle.contracts';
|
|
9
10
|
import { IClientArtifactsProvider } from '../artifacts/index.js';
|
|
10
11
|
import { ChronicleConnection } from '../connection/index.js';
|
|
11
12
|
import { ConnectionLifecycle } from '../connection/ConnectionLifecycle.js';
|
|
@@ -29,6 +30,9 @@ import type { ReactorServices } from './ReactorServices.js';
|
|
|
29
30
|
import type { IEventStore } from '../IEventStore.js';
|
|
30
31
|
import type { ClientArtifactsActivator } from '../artifacts/ClientArtifactsActivator.js';
|
|
31
32
|
import { refuseClientHostedObservers } from '../artifacts/refuseClientHostedObservers.js';
|
|
33
|
+
import { ObservationReconnectBackoff, type ObservationReconnectBackoffOptions } from '../observation/ObservationReconnectBackoff.js';
|
|
34
|
+
import { classifyStreamError, StreamErrorKind } from '../observation/classifyStreamError.js';
|
|
35
|
+
import type { ObservationStreamOutcome } from '../observation/ObservationStreamOutcome.js';
|
|
32
36
|
|
|
33
37
|
/** Expression used to partition reactor observations by event source ID. */
|
|
34
38
|
const EVENT_SOURCE_ID_KEY = '$eventSourceId';
|
|
@@ -96,15 +100,14 @@ class AsyncQueue<T> {
|
|
|
96
100
|
* with the Chronicle Kernel via bidirectional gRPC streaming.
|
|
97
101
|
*/
|
|
98
102
|
export class Reactors implements IReactors {
|
|
99
|
-
/** How long to wait before re-establishing an observation whose stream ended. */
|
|
100
|
-
private static readonly _reobserveDelayMs = 2000;
|
|
101
|
-
|
|
102
103
|
private readonly _logger: ReturnType<typeof createLogger>;
|
|
103
104
|
private readonly _lifecycle: ConnectionLifecycle;
|
|
104
105
|
private readonly _unsubscribeDisconnected: () => void;
|
|
105
106
|
private readonly _reactors = new Map<string, Constructor>();
|
|
106
107
|
private readonly _queues = new Map<string, AsyncQueue<ReactorMessage>>();
|
|
107
108
|
private readonly _observations = new Map<string, AbortController>();
|
|
109
|
+
private readonly _reconnectBackoffs = new Map<string, ObservationReconnectBackoff>();
|
|
110
|
+
private readonly _reobserveTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
108
111
|
private _registered = false;
|
|
109
112
|
private _disposed = false;
|
|
110
113
|
|
|
@@ -116,6 +119,11 @@ export class Reactors implements IReactors {
|
|
|
116
119
|
* @param _namespace - The namespace within the event store.
|
|
117
120
|
* @param lifecycle - The connection lifecycle used to react to disconnect events.
|
|
118
121
|
* @param _eventLog - The event log used to append side-effect events a reactor handler returns.
|
|
122
|
+
* @param _resultHandler - Optional handler for values a reactor handler returns.
|
|
123
|
+
* @param _eventStore - Optional event store handed to reactor handlers.
|
|
124
|
+
* @param _artifactActivator - Optional activator creating reactor instances.
|
|
125
|
+
* @param _skipKeepAlive - Whether the client runs without a keep-alive, which refuses client-hosted reactors.
|
|
126
|
+
* @param _reconnectBackoff - How a reactor waits before opening its observation stream again after it ended.
|
|
119
127
|
*/
|
|
120
128
|
constructor(
|
|
121
129
|
private readonly _clientArtifacts: IClientArtifactsProvider,
|
|
@@ -127,7 +135,8 @@ export class Reactors implements IReactors {
|
|
|
127
135
|
private readonly _resultHandler?: ReactorResultHandler,
|
|
128
136
|
private readonly _eventStore?: IEventStore,
|
|
129
137
|
private readonly _artifactActivator?: ClientArtifactsActivator,
|
|
130
|
-
private readonly _skipKeepAlive: boolean = false
|
|
138
|
+
private readonly _skipKeepAlive: boolean = false,
|
|
139
|
+
private readonly _reconnectBackoff: ObservationReconnectBackoffOptions = {}
|
|
131
140
|
) {
|
|
132
141
|
this._logger = createLogger('@cratis/chronicle/reactors', _connection.logger);
|
|
133
142
|
this._lifecycle = lifecycle;
|
|
@@ -144,6 +153,7 @@ export class Reactors implements IReactors {
|
|
|
144
153
|
this._disposed = true;
|
|
145
154
|
this._registered = false;
|
|
146
155
|
this.disconnectAll();
|
|
156
|
+
this._reconnectBackoffs.clear();
|
|
147
157
|
}
|
|
148
158
|
|
|
149
159
|
/** @inheritdoc */
|
|
@@ -210,13 +220,23 @@ export class Reactors implements IReactors {
|
|
|
210
220
|
eventTypes: ReactorEventTypeEntry[],
|
|
211
221
|
validateInstance: (instance: object) => void
|
|
212
222
|
): Promise<void> {
|
|
223
|
+
let outcome: ObservationStreamOutcome = 'ended';
|
|
213
224
|
try {
|
|
214
|
-
await this.observeReactor(id, reactorType, eventSequenceId, eventTypes, validateInstance);
|
|
225
|
+
outcome = await this.observeReactor(id, reactorType, eventSequenceId, eventTypes, validateInstance);
|
|
215
226
|
} catch (error) {
|
|
216
227
|
this._logger.error('Reactor observation loop exited with error', { reactorId: id, error });
|
|
217
228
|
}
|
|
218
229
|
|
|
219
|
-
this.scheduleReobserve(id, reactorType, eventTypes, validateInstance);
|
|
230
|
+
if (outcome === 'ended') this.scheduleReobserve(id, reactorType, eventTypes, validateInstance);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private reconnectBackoffFor(id: string): ObservationReconnectBackoff {
|
|
234
|
+
let backoff = this._reconnectBackoffs.get(id);
|
|
235
|
+
if (!backoff) {
|
|
236
|
+
backoff = new ObservationReconnectBackoff(this._reconnectBackoff);
|
|
237
|
+
this._reconnectBackoffs.set(id, backoff);
|
|
238
|
+
}
|
|
239
|
+
return backoff;
|
|
220
240
|
}
|
|
221
241
|
|
|
222
242
|
/**
|
|
@@ -225,8 +245,9 @@ export class Reactors implements IReactors {
|
|
|
225
245
|
* The stream ending without an error is not proof that all is well — the kernel
|
|
226
246
|
* closes a cross-store (inbox) reactor's stream rather than tailing it forever,
|
|
227
247
|
* so a reactor that does not re-subscribe there silently stops observing until
|
|
228
|
-
* the whole client reconnects. Both endings are therefore retried
|
|
229
|
-
*
|
|
248
|
+
* the whole client reconnects. Both endings are therefore retried. The delay keeps
|
|
249
|
+
* a stream that keeps ending from becoming a hot loop, and grows with jitter while
|
|
250
|
+
* it does (see {@link ObservationReconnectBackoff}).
|
|
230
251
|
*/
|
|
231
252
|
private scheduleReobserve(id: string, reactorType: Constructor, eventTypes: ReactorEventTypeEntry[], validateInstance: (instance: object) => void): void {
|
|
232
253
|
// A disconnect clears the registration; the reconnect re-registers every
|
|
@@ -235,16 +256,22 @@ export class Reactors implements IReactors {
|
|
|
235
256
|
return;
|
|
236
257
|
}
|
|
237
258
|
|
|
259
|
+
clearTimeout(this._reobserveTimers.get(id));
|
|
260
|
+
const delayMs = this.reconnectBackoffFor(id).nextDelayMs();
|
|
261
|
+
this._logger.debug('Waiting before re-establishing reactor observation', { reactorId: id, delayMs });
|
|
238
262
|
const handle = runInBackgroundContext(() => setTimeout(() => {
|
|
239
|
-
|
|
263
|
+
this._reobserveTimers.delete(id);
|
|
264
|
+
// A reconnect in the meantime has already opened a new stream; opening another would leave one untracked.
|
|
265
|
+
if (!this._registered || this._disposed || this._observations.has(id)) {
|
|
240
266
|
return;
|
|
241
267
|
}
|
|
242
268
|
|
|
243
269
|
this._logger.info('Re-establishing reactor observation', { reactorId: id });
|
|
244
270
|
this.startObservation(id, reactorType, eventTypes, validateInstance);
|
|
245
|
-
},
|
|
271
|
+
}, delayMs));
|
|
246
272
|
|
|
247
273
|
handle.unref?.();
|
|
274
|
+
this._reobserveTimers.set(id, handle);
|
|
248
275
|
}
|
|
249
276
|
|
|
250
277
|
private async observeReactor(
|
|
@@ -253,17 +280,20 @@ export class Reactors implements IReactors {
|
|
|
253
280
|
eventSequenceId: string,
|
|
254
281
|
eventTypes: ReactorEventTypeEntry[],
|
|
255
282
|
validateInstance: (instance: object) => void
|
|
256
|
-
): Promise<
|
|
283
|
+
): Promise<ObservationStreamOutcome> {
|
|
257
284
|
const batchMethods = getReactorBatchMethods(reactorType, this._clientArtifacts.eventTypes);
|
|
258
285
|
const queue = new AsyncQueue<ReactorMessage>();
|
|
259
286
|
const controller = new AbortController();
|
|
260
287
|
this._queues.set(id, queue);
|
|
261
288
|
this._observations.set(id, controller);
|
|
289
|
+
const connectionId = this._lifecycle.connectionId;
|
|
290
|
+
const backoff = this.reconnectBackoffFor(id);
|
|
291
|
+
backoff.streamOpened();
|
|
262
292
|
|
|
263
293
|
queue.send({
|
|
264
294
|
Content: {
|
|
265
295
|
Value0: {
|
|
266
|
-
ConnectionId:
|
|
296
|
+
ConnectionId: connectionId,
|
|
267
297
|
EventStore: this._eventStoreName,
|
|
268
298
|
Namespace: this._namespace,
|
|
269
299
|
Reactor: {
|
|
@@ -290,6 +320,8 @@ export class Reactors implements IReactors {
|
|
|
290
320
|
}
|
|
291
321
|
});
|
|
292
322
|
|
|
323
|
+
let iterator: AsyncIterator<EventsToObserve> | undefined;
|
|
324
|
+
let streamFinished = false;
|
|
293
325
|
try {
|
|
294
326
|
const reactorInstance = this._artifactActivator ? undefined : new (reactorType as new () => Record<string, Function>)();
|
|
295
327
|
if (reactorInstance) validateInstance(reactorInstance);
|
|
@@ -304,17 +336,44 @@ export class Reactors implements IReactors {
|
|
|
304
336
|
logger: this._logger, connectionLogger: this._connection.logger
|
|
305
337
|
});
|
|
306
338
|
|
|
307
|
-
|
|
308
|
-
|
|
339
|
+
// Pull each message by hand so only a failure of the stream itself is classified: an error from a
|
|
340
|
+
// handler is the handler's failure, and must never make the whole client reconnect.
|
|
341
|
+
iterator = this._connection.reactors.observe(queue, { signal: controller.signal })[Symbol.asyncIterator]();
|
|
342
|
+
for (;;) {
|
|
343
|
+
let next: IteratorResult<EventsToObserve>;
|
|
344
|
+
try {
|
|
345
|
+
next = await iterator.next();
|
|
346
|
+
} catch (streamError) {
|
|
347
|
+
streamFinished = true;
|
|
348
|
+
return this.streamFailed(id, queue, controller.signal, connectionId, streamError);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (next.done) {
|
|
352
|
+
streamFinished = true;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
backoff.streamDelivered();
|
|
357
|
+
const result = await dispatcher.dispatch(next.value);
|
|
309
358
|
queue.send({ Content: { Value0: undefined, Value1: result } });
|
|
310
359
|
}
|
|
311
|
-
|
|
312
|
-
if (
|
|
360
|
+
|
|
361
|
+
if (this._queues.get(id) !== queue) {
|
|
313
362
|
this._logger.debug('Reactor observation stream closed cleanly', { reactorId: id });
|
|
314
|
-
|
|
315
|
-
this._logger.error('Reactor observation stream ended unexpectedly', { reactorId: id, error: err });
|
|
363
|
+
return 'superseded';
|
|
316
364
|
}
|
|
365
|
+
|
|
366
|
+
return 'ended';
|
|
367
|
+
} catch (handlerError) {
|
|
368
|
+
if (this._queues.get(id) !== queue) return 'superseded';
|
|
369
|
+
this._logger.error('Reactor observation failed while handling a delivery', { reactorId: id, error: handlerError });
|
|
370
|
+
return 'ended';
|
|
317
371
|
} finally {
|
|
372
|
+
if (!streamFinished) {
|
|
373
|
+
controller.abort();
|
|
374
|
+
void iterator?.return?.()?.catch(() => undefined);
|
|
375
|
+
}
|
|
376
|
+
|
|
318
377
|
// Only retire our own queue: a reconnect can already have replaced it,
|
|
319
378
|
// and deleting the new one would leave that observation untracked and
|
|
320
379
|
// leak a duplicate stream on every reconnect.
|
|
@@ -326,7 +385,31 @@ export class Reactors implements IReactors {
|
|
|
326
385
|
}
|
|
327
386
|
}
|
|
328
387
|
|
|
388
|
+
private streamFailed(id: string, queue: AsyncQueue<ReactorMessage>, signal: AbortSignal, connectionId: string, error: unknown): ObservationStreamOutcome {
|
|
389
|
+
// A late error from a stream that a disconnect, a dispose or a newer stream replaced says nothing about now.
|
|
390
|
+
if (this._queues.get(id) !== queue) {
|
|
391
|
+
this._logger.debug('Reactor observation stream closed cleanly', { reactorId: id });
|
|
392
|
+
return 'superseded';
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const kind = classifyStreamError(error);
|
|
396
|
+
if (kind === StreamErrorKind.Cancelled && signal.aborted) return 'stopped';
|
|
397
|
+
if (kind === StreamErrorKind.SessionLost) {
|
|
398
|
+
// The kernel lost this client's registration. Opening the same stream with the same connection id
|
|
399
|
+
// can never work, so reconnect the whole client, which re-registers every reactor.
|
|
400
|
+
this._logger.warn('Reactor observation stream lost its session; reconnecting the client', { reactorId: id, error });
|
|
401
|
+
this._lifecycle.requestReconnect(connectionId, 'reactor-session-lost', error,
|
|
402
|
+
handlerError => this._logger.error('Reconnect request handler failed', { reactorId: id, error: handlerError }));
|
|
403
|
+
return 'sessionLost';
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
this._logger.error('Reactor observation stream ended unexpectedly', { reactorId: id, error });
|
|
407
|
+
return 'ended';
|
|
408
|
+
}
|
|
409
|
+
|
|
329
410
|
private disconnectAll(): void {
|
|
411
|
+
for (const timer of this._reobserveTimers.values()) clearTimeout(timer);
|
|
412
|
+
this._reobserveTimers.clear();
|
|
330
413
|
for (const controller of this._observations.values()) controller.abort();
|
|
331
414
|
for (const queue of this._queues.values()) queue.complete();
|
|
332
415
|
this._observations.clear();
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { ClientError, Status } from 'nice-grpc';
|
|
6
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
7
|
+
import { page } from '../given/observe_reactor_messages.js';
|
|
8
|
+
import { ReactorPageDispatcher } from '../../ReactorPageDispatcher.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
|
|
12
|
+
describe('when reconnecting a reactor observation stream and a handler failed with a lost session', () => {
|
|
13
|
+
let context: a_reactor_whose_stream_ends;
|
|
14
|
+
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
context = new a_reactor_whose_stream_ends();
|
|
17
|
+
vi.spyOn(ReactorPageDispatcher.prototype, 'dispatch')
|
|
18
|
+
.mockRejectedValueOnce(new ClientError('/EventSequences/Append', Status.UNKNOWN, 'ClientIsNotConnected: connection is not connected'));
|
|
19
|
+
context.scripts.push({ deliver: [page(1n, {})], untilAborted: true }, { untilAborted: true });
|
|
20
|
+
await context.reactors.register();
|
|
21
|
+
await vi.advanceTimersByTimeAsync(120_000);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
context.dispose();
|
|
26
|
+
vi.restoreAllMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should not ask the whole client to reconnect', () => context.reconnectRequests.should.be.empty);
|
|
30
|
+
it('should open the stream again after the base delay', () => context.waits.should.deep.equal([1_000]));
|
|
31
|
+
it('should close the stream the handler failed on', () => context.stream.open.should.equal(1));
|
|
32
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { ClientError, Status } from 'nice-grpc';
|
|
6
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
describe('when reconnecting a reactor observation stream and a replaced stream fails late', () => {
|
|
11
|
+
let context: a_reactor_whose_stream_ends;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
context = new a_reactor_whose_stream_ends();
|
|
15
|
+
context.scripts.push(
|
|
16
|
+
{ untilAborted: true, endsLateByMs: 10, error: new ClientError('/Reactors/Observe', Status.INTERNAL, 'boom') },
|
|
17
|
+
{ untilAborted: true });
|
|
18
|
+
await context.reactors.register();
|
|
19
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
20
|
+
await context.reconnect();
|
|
21
|
+
await vi.advanceTimersByTimeAsync(120_000);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => context.dispose());
|
|
25
|
+
|
|
26
|
+
it('should not open the stream again', () => context.openedAt.should.have.lengthOf(2));
|
|
27
|
+
it('should leave exactly one stream open', () => context.stream.open.should.equal(1));
|
|
28
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
6
|
+
import { page } from '../given/observe_reactor_messages.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
describe('when reconnecting a reactor observation stream and a stream delivered before it ended', () => {
|
|
11
|
+
let context: a_reactor_whose_stream_ends;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
context = new a_reactor_whose_stream_ends();
|
|
15
|
+
context.scripts.push({}, {}, { deliver: [page(1n, {})] }, {});
|
|
16
|
+
await context.reactors.register();
|
|
17
|
+
await vi.advanceTimersByTimeAsync(6_500);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => context.dispose());
|
|
21
|
+
|
|
22
|
+
it('should start the wait over at the base delay after the stream that delivered', () =>
|
|
23
|
+
context.waits.slice(0, 4).should.deep.equal([1_000, 2_000, 1_000, 2_000]));
|
|
24
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
6
|
+
|
|
7
|
+
chai.should();
|
|
8
|
+
|
|
9
|
+
describe('when reconnecting a reactor observation stream and a stream stayed open before it ended', () => {
|
|
10
|
+
let context: a_reactor_whose_stream_ends;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
context = new a_reactor_whose_stream_ends();
|
|
14
|
+
context.scripts.push({}, {}, { openForMs: 180_000 }, { untilAborted: true });
|
|
15
|
+
await context.reactors.register();
|
|
16
|
+
await vi.advanceTimersByTimeAsync(185_000);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => context.dispose());
|
|
20
|
+
|
|
21
|
+
it('should start the wait over at the base delay after the stream that stayed open', () =>
|
|
22
|
+
context.waits.should.deep.equal([1_000, 2_000, 181_000]));
|
|
23
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
6
|
+
|
|
7
|
+
chai.should();
|
|
8
|
+
|
|
9
|
+
describe('when reconnecting a reactor observation stream and it is disposed while it waits to reopen', () => {
|
|
10
|
+
let context: a_reactor_whose_stream_ends;
|
|
11
|
+
let timersAfterDispose: number;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
context = new a_reactor_whose_stream_ends();
|
|
15
|
+
context.scripts.push({});
|
|
16
|
+
await context.reactors.register();
|
|
17
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
18
|
+
context.reactors.dispose();
|
|
19
|
+
timersAfterDispose = vi.getTimerCount();
|
|
20
|
+
await vi.advanceTimersByTimeAsync(120_000);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(() => context.dispose());
|
|
24
|
+
|
|
25
|
+
it('should not open the stream again', () => context.openedAt.should.have.lengthOf(1));
|
|
26
|
+
it('should leave no timer behind', () => timersAfterDispose.should.equal(0));
|
|
27
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { ClientError, Status } from 'nice-grpc';
|
|
6
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
describe('when reconnecting a reactor observation stream and streams keep ending without working', () => {
|
|
11
|
+
let context: a_reactor_whose_stream_ends;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
context = new a_reactor_whose_stream_ends();
|
|
15
|
+
context.scripts.push({}, { error: new ClientError('/Reactors/Observe', Status.INTERNAL, 'boom') }, {}, {}, {});
|
|
16
|
+
await context.reactors.register();
|
|
17
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => context.dispose());
|
|
21
|
+
|
|
22
|
+
it('should double the wait each time a stream ends', () => context.waits.slice(0, 5).should.deep.equal([1_000, 2_000, 4_000, 8_000, 16_000]));
|
|
23
|
+
it('should not reconnect the client', () => context.reconnectRequests.should.be.empty);
|
|
24
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
6
|
+
|
|
7
|
+
chai.should();
|
|
8
|
+
|
|
9
|
+
describe('when reconnecting a reactor observation stream and the client reconnected after streams kept ending', () => {
|
|
10
|
+
let context: a_reactor_whose_stream_ends;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
context = new a_reactor_whose_stream_ends();
|
|
14
|
+
context.scripts.push({}, {}, {}, { untilAborted: true });
|
|
15
|
+
await context.reactors.register();
|
|
16
|
+
await vi.advanceTimersByTimeAsync(1_000);
|
|
17
|
+
await context.reconnect();
|
|
18
|
+
await vi.advanceTimersByTimeAsync(4_000);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(() => context.dispose());
|
|
22
|
+
|
|
23
|
+
it('should keep the wait it had grown to', () => context.waits.should.deep.equal([1_000, 0, 4_000]));
|
|
24
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
6
|
+
|
|
7
|
+
chai.should();
|
|
8
|
+
|
|
9
|
+
describe('when reconnecting a reactor observation stream and the client reconnected while it waited to reopen', () => {
|
|
10
|
+
let context: a_reactor_whose_stream_ends;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
context = new a_reactor_whose_stream_ends();
|
|
14
|
+
context.scripts.push({}, { untilAborted: true });
|
|
15
|
+
await context.reactors.register();
|
|
16
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
17
|
+
await context.reconnect();
|
|
18
|
+
await vi.advanceTimersByTimeAsync(120_000);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(() => context.dispose());
|
|
22
|
+
|
|
23
|
+
it('should only open the stream the reconnect registered', () => context.openedAt.should.have.lengthOf(2));
|
|
24
|
+
it('should leave exactly one stream open', () => context.stream.open.should.equal(1));
|
|
25
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { ClientError, Status } from 'nice-grpc';
|
|
6
|
+
import { a_reactor_whose_stream_ends } from './given/a_reactor_whose_stream_ends.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
describe('when reconnecting a reactor observation stream and the kernel lost the session', () => {
|
|
11
|
+
let context: a_reactor_whose_stream_ends;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
context = new a_reactor_whose_stream_ends();
|
|
15
|
+
context.scripts.push({ error: new ClientError('/Reactors/Observe', Status.UNKNOWN, 'ClientIsNotConnected: connection is not connected') });
|
|
16
|
+
await context.reactors.register();
|
|
17
|
+
await vi.advanceTimersByTimeAsync(120_000);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => context.dispose());
|
|
21
|
+
|
|
22
|
+
it('should ask the whole client to reconnect', () => context.reconnectRequests.should.deep.equal(['reactor-session-lost']));
|
|
23
|
+
it('should not open the stream again with the same connection', () => context.openedAt.should.have.lengthOf(1));
|
|
24
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { vi } from 'vitest';
|
|
5
|
+
import type { EventsToObserve } from '@cratis/chronicle.contracts';
|
|
6
|
+
import { Reactors } from '../../../Reactors.js';
|
|
7
|
+
import { reactor } from '../../../reactor.js';
|
|
8
|
+
import { eventType } from '../../../../events/eventTypeDecorator.js';
|
|
9
|
+
import { ConnectionLifecycle } from '../../../../connection/ConnectionLifecycle.js';
|
|
10
|
+
import type { ChronicleConnection } from '../../../../connection/ChronicleConnection.js';
|
|
11
|
+
import type { IClientArtifactsProvider } from '../../../../artifacts/IClientArtifactsProvider.js';
|
|
12
|
+
import type { IEventLog } from '../../../../eventSequences/IEventLog.js';
|
|
13
|
+
import type { IEventStore } from '../../../../IEventStore.js';
|
|
14
|
+
import { a_scripted_stream, type StreamScript } from '../../../../observation/for_ObservationReconnectBackoff/given/a_scripted_stream.js';
|
|
15
|
+
|
|
16
|
+
@eventType('reconnecting-reactor-item')
|
|
17
|
+
class Item {}
|
|
18
|
+
|
|
19
|
+
@reactor('reconnecting-reactor')
|
|
20
|
+
class Observer { item() {} }
|
|
21
|
+
|
|
22
|
+
export type { StreamScript };
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A reactor whose observation streams follow a script, on fake timers and a jitter of zero, so every wait is
|
|
26
|
+
* exactly half of its ceiling.
|
|
27
|
+
*/
|
|
28
|
+
export class a_reactor_whose_stream_ends {
|
|
29
|
+
readonly lifecycle = new ConnectionLifecycle();
|
|
30
|
+
readonly reconnectRequests: string[] = [];
|
|
31
|
+
readonly stream = new a_scripted_stream<EventsToObserve>();
|
|
32
|
+
readonly reactors: Reactors;
|
|
33
|
+
|
|
34
|
+
constructor() {
|
|
35
|
+
vi.useFakeTimers();
|
|
36
|
+
this.lifecycle.onReconnectRequested(reason => this.reconnectRequests.push(reason));
|
|
37
|
+
const artifacts = { eventTypes: [Item], reactors: [Observer], reducers: [], readModels: [], seeders: [], constraints: [],
|
|
38
|
+
projections: [], webhooks: [], eventTypeMigrations: [], globalForHandlers: [] } as IClientArtifactsProvider;
|
|
39
|
+
this.reactors = new Reactors(artifacts, { reactors: { observe: this.stream.observe } } as unknown as ChronicleConnection,
|
|
40
|
+
'store', 'Default', this.lifecycle, {} as IEventLog, undefined, { readModels: {} } as IEventStore, undefined, false,
|
|
41
|
+
{ now: () => Date.now(), nextJitter: () => 0 });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Gets the scripts the next streams follow. */
|
|
45
|
+
get scripts(): StreamScript<EventsToObserve>[] {
|
|
46
|
+
return this.stream.scripts;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Gets when each stream was opened. */
|
|
50
|
+
get openedAt(): number[] {
|
|
51
|
+
return this.stream.openedAt;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Gets the wait before each stream after the first was opened. */
|
|
55
|
+
get waits(): number[] {
|
|
56
|
+
return this.stream.waits;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Drops the session and registers again, as a client reconnect does. */
|
|
60
|
+
async reconnect(): Promise<void> {
|
|
61
|
+
await this.lifecycle.disconnected(() => undefined);
|
|
62
|
+
await this.reactors.register();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
dispose(): void {
|
|
66
|
+
this.reactors.dispose();
|
|
67
|
+
vi.useRealTimers();
|
|
68
|
+
}
|
|
69
|
+
}
|