-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent multiple appends within the same event loop creating many bat…
…ches. - immediately cache the promise and await the promise on subsequent appends - rework backpressured write to work across multiple calls - add test
- Loading branch information
1 parent
2fe617a
commit 088bc45
Showing
4 changed files
with
125 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/__test__/streams/appendToStream-batch-append-flood.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** @jest-environment ./src/__test__/utils/enableVersionCheck.ts */ | ||
import type { Duplex } from "stream"; | ||
|
||
import { | ||
createTestNode, | ||
matchServerVersion, | ||
optionalDescribe, | ||
} from "@test-utils"; | ||
import { EventStoreDBClient, jsonEvent } from "@eventstore/db-client"; | ||
|
||
describe("appendToStream - batch append - flood", () => { | ||
const supported = matchServerVersion`>=21.10`; | ||
|
||
const node = createTestNode(); | ||
let client!: EventStoreDBClient; | ||
|
||
beforeAll(async () => { | ||
await node.up(); | ||
client = new EventStoreDBClient( | ||
{ endpoint: node.uri }, | ||
{ rootCertificate: node.rootCertificate } | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
await node.down(); | ||
}); | ||
|
||
optionalDescribe(supported)("Supported (>=21.10)", () => { | ||
test("Can handle multiple appends within the same event loop", async () => { | ||
const streamName = "batchFlood"; | ||
const numberOfEvents = 10_000; | ||
const value = "A".repeat(904); | ||
|
||
const oneKiloByteEvent = () => | ||
jsonEvent({ | ||
type: "AnyEventType", | ||
data: { value }, | ||
}); | ||
|
||
const requests = []; | ||
for (let i = 0; i < numberOfEvents; i++) { | ||
requests.push(client.appendToStream(streamName, oneKiloByteEvent())); | ||
} | ||
await Promise.all(requests); | ||
|
||
await client.dispose(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,72 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { ClientWritableStream } from "@grpc/grpc-js"; | ||
|
||
export const backpressuredWrite = <T>( | ||
const cache = new WeakMap<ClientWritableStream<any>, BackpressureQueue<any>>(); | ||
|
||
interface QueueItem<T> { | ||
data: T; | ||
resolve: () => void; | ||
reject: (err: Error) => void; | ||
} | ||
|
||
class BackpressureQueue<T> { | ||
private stream: ClientWritableStream<T>; | ||
private queue: QueueItem<T>[] = []; | ||
private writing = false; | ||
|
||
constructor(stream: ClientWritableStream<T>) { | ||
this.stream = stream; | ||
this.stream.once("error", this.errorOut); | ||
} | ||
|
||
write = async (data: T) => | ||
new Promise<void>((resolve, reject) => { | ||
this.queue.push({ data, resolve, reject }); | ||
this.triggerWrite(); | ||
}); | ||
|
||
private triggerWrite = async () => { | ||
if (this.writing) return; | ||
|
||
this.writing = true; | ||
|
||
while (this.queue.length) { | ||
const { data, resolve } = this.queue.shift()!; | ||
|
||
const written = this.stream.write(data); | ||
|
||
if (!written) { | ||
await new Promise<void>((r) => this.stream.once("drain", r)); | ||
} | ||
|
||
resolve(); | ||
} | ||
|
||
this.cleanUp(); | ||
}; | ||
|
||
private errorOut = (err: Error) => { | ||
this.cleanUp(); | ||
|
||
while (this.queue.length) { | ||
const { reject } = this.queue.shift()!; | ||
reject(err); | ||
} | ||
}; | ||
|
||
private cleanUp = () => { | ||
this.stream.removeListener("error", this.errorOut); | ||
cache.delete(this.stream); | ||
}; | ||
} | ||
|
||
export const backpressuredWrite = async <T>( | ||
stream: ClientWritableStream<T>, | ||
data: T | ||
) => | ||
new Promise<void>((resolve) => { | ||
const written = stream.write(data); | ||
if (written) return resolve(); | ||
stream.once("drain", resolve); | ||
}); | ||
) => { | ||
if (!cache.has(stream)) { | ||
cache.set(stream, new BackpressureQueue<T>(stream)); | ||
} | ||
|
||
await cache.get(stream)!.write(data); | ||
}; |