Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/inmemory-slow-subscriber.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@codeforbreakfast/eventsourcing-store-inmemory': patch
---

Fix the subscription buffer size. The store meant to buffer 256 events per subscription but wrote `2 ^ 8`, which is 10 in JavaScript. A subscriber only ten events behind blocked `append`, and a consumer that appended to a stream it was subscribed to could deadlock.

The buffer is now 256. A subscriber that falls 256 events behind still blocks `append`. This is deliberate: the in-memory store is meant for tests and similar uses, where failing fast is better than growing memory without limit.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ interface EventStream<V> {
readonly pubsub: PubSub.PubSub<V>;
}

const subscriberBufferCapacity = 256;

const emptyStream = <V>(): Effect.Effect<EventStream<V>, never, never> =>
pipe(
2 ^ 8,
subscriberBufferCapacity,
PubSub.bounded<V>,
Effect.map((pubsub) => ({
events: Chunk.empty<V>(),
Expand Down
41 changes: 41 additions & 0 deletions packages/eventsourcing-store-inmemory/src/lib/inMemory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,44 @@
Effect.runPromise
));
});

const lagBehindEvents = 100;

const appendOneEventAt = (store: EventStore<string>) => (eventNumber: number) =>
pipe(
['event'],
Stream.fromIterable,
Stream.run(store.append({ streamId: decodeStreamId('lagging-stream'), eventNumber }))
);

const stallOnEveryEvent = () => Effect.never;

const startSubscriberThatNeverDrains = (store: EventStore<string>) =>
pipe(store.subscribeAll(), Effect.flatMap(Stream.runForEach(stallOnEveryEvent)), Effect.fork);

const appendManyEvents = (store: EventStore<string>) =>
Effect.forEach(
Array.from({ length: lagBehindEvents }, (_, i) => i),
appendOneEventAt(store),
{ discard: true }
);

const runLaggingSubscriberTest = (store: EventStore<string>) =>
pipe(
store,
startSubscriberThatNeverDrains,
Effect.andThen(Effect.yieldNow()),
Effect.andThen(appendManyEvents(store)),
Effect.timeout('2 seconds')
);

describe('A slow subscriber', () => {
it('should not stall appends while it lags behind by fewer events than the buffer holds', () =>

Check failure on line 133 in packages/eventsourcing-store-inmemory/src/lib/inMemory.spec.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=CodeForBreakfast_eventsourcing&issues=AaDyTcgaGV71yE5LIYCW&open=AaDyTcgaGV71yE5LIYCW&pullRequest=421
pipe(
StringEventStore,
Effect.flatMap(runLaggingSubscriberTest),
Effect.scoped,
Effect.provide(StringEventStoreLayer),
Effect.runPromise
));
});
Loading