fixed up ratelimiting
This commit is contained in:
parent
a616c92656
commit
a7146a3f57
15 changed files with 912 additions and 186 deletions
|
|
@ -62,6 +62,7 @@ export function registerApplicationServices(
|
|||
container.register({
|
||||
queueManager: asFunction(({ logger, handlerRegistry }) => {
|
||||
const { QueueManager } = require('@stock-bot/queue');
|
||||
|
||||
const queueConfig = {
|
||||
serviceName: config.service?.serviceName || config.service?.name || 'unknown',
|
||||
redis: {
|
||||
|
|
@ -78,6 +79,7 @@ export function registerApplicationServices(
|
|||
enableScheduledJobs: config.queue!.enableScheduledJobs ?? true,
|
||||
autoDiscoverHandlers: true,
|
||||
};
|
||||
|
||||
return new QueueManager(queueConfig, handlerRegistry, logger);
|
||||
}).singleton(),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -362,6 +362,45 @@ export class ServiceApplication {
|
|||
});
|
||||
await handlerInitializer(containerWithDI);
|
||||
this.logger.info('Handlers initialized');
|
||||
|
||||
// After handlers are initialized, add rate limit rules to queue manager
|
||||
const queueManager = this.container.resolve('queueManager');
|
||||
const handlerRegistry = this.container.resolve<HandlerRegistry>('handlerRegistry');
|
||||
|
||||
if (queueManager && handlerRegistry) {
|
||||
const rateLimitRules = [];
|
||||
const allMetadata = handlerRegistry.getAllMetadata();
|
||||
|
||||
for (const [handlerName, metadata] of allMetadata) {
|
||||
// Handler-level rate limit
|
||||
if (metadata.rateLimit) {
|
||||
rateLimitRules.push({
|
||||
level: 'handler' as const,
|
||||
handler: handlerName,
|
||||
config: metadata.rateLimit as any, // Type mismatch between packages
|
||||
});
|
||||
}
|
||||
|
||||
// Operation-level rate limits
|
||||
for (const operation of metadata.operations) {
|
||||
if (operation.rateLimit) {
|
||||
rateLimitRules.push({
|
||||
level: 'operation' as const,
|
||||
handler: handlerName,
|
||||
operation: operation.name,
|
||||
config: operation.rateLimit as any, // Type mismatch between packages
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rateLimitRules.length > 0) {
|
||||
await queueManager.addRateLimitRules(rateLimitRules);
|
||||
this.logger.info('Rate limit rules added to QueueManager', {
|
||||
ruleCount: rateLimitRules.length,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create and mount routes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue