mirror of
https://github.com/apotdevin/thunderhub.git
synced 2024-11-19 09:50:03 +01:00
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { getGraphQLRateLimiter } from 'graphql-rate-limit';
|
|
import { logger } from './logger';
|
|
|
|
interface RateConfigProps {
|
|
[key: string]: {
|
|
max: number;
|
|
window: string;
|
|
};
|
|
}
|
|
|
|
export const RateConfig: RateConfigProps = {
|
|
getMessages: { max: 10, window: '5s' },
|
|
nodeInfo: { max: 10, window: '5s' },
|
|
chainBalance: { max: 10, window: '5s' },
|
|
pendingChainBalance: { max: 10, window: '5s' },
|
|
channelBalance: { max: 10, window: '5s' },
|
|
getChannel: { max: 1000, window: '5s' },
|
|
};
|
|
|
|
const rateLimiter = getGraphQLRateLimiter({
|
|
identifyContext: (ctx: string) => ctx,
|
|
formatError: () => 'Rate Limit Reached',
|
|
});
|
|
|
|
export const requestLimiter = async (rate: string, field: string) => {
|
|
const { max, window } = RateConfig[field] || { max: 5, window: '5s' };
|
|
const errorMessage = await rateLimiter(
|
|
{
|
|
parent: rate,
|
|
args: {},
|
|
context: rate,
|
|
info: { fieldName: field } as any,
|
|
},
|
|
{ max, window }
|
|
);
|
|
if (errorMessage) {
|
|
logger.warn(`Rate limit reached for '${field}' from ip ${rate}`);
|
|
throw new Error(errorMessage);
|
|
}
|
|
};
|