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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ AVIATIONSTACK_API_KEY= # optional — leave blank to use mock data
# Port the API server listens on
PORT=3001

# Maximum listeners per policy status event emitter. Default: 1000.
# STATUS_EVENTS_MAX_LISTENERS=1000

# Environment: "development", "staging", or "production"
NODE_ENV="development"

Expand Down
29 changes: 29 additions & 0 deletions src/common/events/status-events.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { StatusEventsService } from './status-events.service';
import { ConfigService } from '@nestjs/config';
import { EventEmitter } from 'events';

describe('StatusEventsService', () => {
function build(configuredMaxListeners?: string) {
const config = {
get: jest.fn((key: string) => key === 'STATUS_EVENTS_MAX_LISTENERS' ? configuredMaxListeners : undefined),
};
const service = new StatusEventsService(undefined, undefined, config as unknown as ConfigService);

return { service, config };
}

it('uses the configured maximum listener count', () => {
const { service } = build('250');

expect((service as unknown as { emitter: EventEmitter }).emitter.getMaxListeners()).toBe(250);
});

it.each([undefined, '', '0', '-1', '1.5', 'not-a-number'])(
'uses the default maximum listener count for invalid configuration (%s)',
(configuredMaxListeners) => {
const { service } = build(configuredMaxListeners);

expect((service as unknown as { emitter: EventEmitter }).emitter.getMaxListeners()).toBe(1000);
},
);
});
12 changes: 9 additions & 3 deletions src/common/events/status-events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@nestjs/common';
import { EventEmitter } from 'events';
import * as crypto from 'crypto';
import { ConfigService } from '@nestjs/config';
import type Redis from 'ioredis';
import { PrismaService } from '../../prisma/prisma.service';

Expand All @@ -19,6 +20,7 @@ export interface PolicyStatusEvent {
}

const STATUS_EVENTS_CHANNEL = 'policy:status:events';
const DEFAULT_MAX_LISTENERS = 1000;

/**
* StatusEventsService — pub/sub for policy status changes (#349).
Expand All @@ -42,10 +44,14 @@ export class StatusEventsService implements OnModuleInit, OnModuleDestroy {
private readonly redis?: Redis,
@Optional()
private readonly prisma?: PrismaService,
@Optional()
private readonly config?: ConfigService,
) {
// Default is 10 — a popular policy with many open SSE connections
// shouldn't trigger Node's "possible memory leak" warning.
this.emitter.setMaxListeners(1000);
const configuredMaxListeners = Number(this.config?.get<string>('STATUS_EVENTS_MAX_LISTENERS'));
const maxListeners = Number.isSafeInteger(configuredMaxListeners) && configuredMaxListeners > 0
? configuredMaxListeners
: DEFAULT_MAX_LISTENERS;
this.emitter.setMaxListeners(maxListeners);
}

setPolicyOwner(policyId: string, owner: string): void {
Expand Down