Skip to content
Merged
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
14 changes: 12 additions & 2 deletions backend/src/services/sse.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { randomUUID } from 'crypto';
import type { Response } from 'express';
import logger from '../logger.js';
import logger, { requestContext } from '../logger.js';
import { isRedisAvailable, getPublisher, getSubscriber } from '../lib/redis.js';

const HEARTBEAT_INTERVAL_MS = 30_000;
Expand Down Expand Up @@ -34,6 +35,13 @@ export class SSEService {
private perIpPeakConnections = 0;
private perUserPeakConnections = 0;

/**
* Stable id attached to every log line emitted by the heartbeat
* setInterval callback, since it fires outside of any HTTP request and
* would otherwise have no requestContext (and thus no correlation id).
*/
private readonly heartbeatWorkerId = `sse-heartbeat:${randomUUID()}`;

private readonly maxConnections: number = (() => {
const parsed = Number.parseInt(process.env.MAX_SSE_CONNECTIONS ?? '10000', 10);
if (!Number.isFinite(parsed) || parsed <= 0) return 10000;
Expand Down Expand Up @@ -251,7 +259,9 @@ export class SSEService {
}

this.heartbeatTimer = setInterval(() => {
this.sendHeartbeat();
requestContext.run({ requestId: this.heartbeatWorkerId }, () => {
this.sendHeartbeat();
});
}, HEARTBEAT_INTERVAL_MS);
}

Expand Down
10 changes: 9 additions & 1 deletion backend/src/workers/soroban-event-worker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { randomUUID } from "crypto";
import { rpc, xdr, StrKey } from "@stellar/stellar-sdk";
import { prisma } from "../lib/prisma.js";
import { INDEXER_STATE_ID, ensureIndexerState } from "../lib/indexer-state.js";
import { sseService } from "../services/sse.service.js";
import logger from "../logger.js";
import logger, { requestContext } from "../logger.js";
import { Prisma } from "../generated/prisma/index.js";
import "../lib/stream-id.js";

Expand Down Expand Up @@ -112,6 +113,13 @@ export class SorobanEventWorker {
/** Recent attempt outcomes for sliding-window spike detection. */
private recentOutcomes: { ok: boolean; at: number }[] = [];

/**
* Stable id attached to every log line emitted by the background poll
* loop, since these callbacks fire outside of any HTTP request and would
* otherwise have no requestContext (and thus no correlation id) at all.
*/
private readonly workerId = `soroban-worker:${randomUUID()}`;

constructor() {
const rpcUrl =
process.env.SOROBAN_RPC_URL ?? "https://soroban-testnet.stellar.org";
Expand Down
Loading