implement background send, requestID

This commit is contained in:
Sasha 2025-09-26 01:34:37 +02:00
parent 4fe8bfdd88
commit 37ee490114
No known key found for this signature in database
2 changed files with 34 additions and 7 deletions

View File

@ -80,10 +80,7 @@ export class MessageStore {
}
}
public async queue(
encoder: IEncoder,
message: IMessage
): Promise<RequestId | undefined> {
public async queue(encoder: IEncoder, message: IMessage): Promise<RequestId> {
const requestId = crypto.randomUUID();
this.pendingRequests.set(requestId, {

View File

@ -11,16 +11,46 @@ export class Sender {
private readonly messageStore: MessageStore;
private readonly lightPush: ILightPush;
private sendInterval: ReturnType<typeof setInterval> | null = null;
public constructor(params: SenderConstructorParams) {
this.messageStore = params.messageStore;
this.lightPush = params.lightPush;
}
public async send(encoder: IEncoder, message: IMessage): Promise<void> {
public start(): void {
this.sendInterval = setInterval(() => void this.backgroundSend(), 1000);
}
public stop(): void {
if (this.sendInterval) {
clearInterval(this.sendInterval);
this.sendInterval = null;
}
}
public async send(encoder: IEncoder, message: IMessage): Promise<string> {
const requestId = await this.messageStore.queue(encoder, message);
await this.lightPush.send(encoder, message);
if (requestId) {
const response = await this.lightPush.send(encoder, message);
if (response.successes.length > 0) {
await this.messageStore.markSent(requestId);
}
return requestId;
}
private async backgroundSend(): Promise<void> {
const pendingRequests = this.messageStore.getMessagesToSend();
// todo: implement chunking, error handling, retry, etc.
// todo: implement backoff and batching potentially
for (const { requestId, encoder, message } of pendingRequests) {
const response = await this.lightPush.send(encoder, message);
if (response.successes.length > 0) {
await this.messageStore.markSent(requestId);
}
}
}
}