fix(api): the API failed to start after moving the proposal limit to Redis
ProposalLimiter, in the presenters, asked for the RateLimitStore port, which only the application layer sees, so Nest could not build it and the container never became healthy. The count now lives in an application service, ProposalQuota; the presenter only answers 429. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,21 +1,13 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { RateLimitStore } from '@task-stream/api/tracker/application';
|
||||
import { ProposalQuota } from '@task-stream/api/tracker/application';
|
||||
|
||||
/** Proposals from one address within the window. */
|
||||
const LIMIT = 5;
|
||||
const WINDOW_MS = 15 * 60_000;
|
||||
|
||||
/**
|
||||
* Keeps one address from flooding a project with proposals. Counted in
|
||||
* Redis, so the limit holds across every instance of the API.
|
||||
*/
|
||||
/** The proposal quota, as the HTTP answer: 429 once it is used up. */
|
||||
@Injectable()
|
||||
export class ProposalLimiter {
|
||||
constructor(private readonly store: RateLimitStore) {}
|
||||
constructor(private readonly quota: ProposalQuota) {}
|
||||
|
||||
async take(address: string): Promise<void> {
|
||||
const count = await this.store.hit(`proposals:${address}`, WINDOW_MS);
|
||||
if (count > LIMIT)
|
||||
if (!(await this.quota.take(address)))
|
||||
throw new HttpException(
|
||||
'Too many proposals from here; try again in a few minutes',
|
||||
HttpStatus.TOO_MANY_REQUESTS,
|
||||
|
||||
Reference in New Issue
Block a user