lookup and cache rate limit factors directly within SkRateLimiterService
This commit is contained in:
parent
402933004a
commit
09669d72e7
7 changed files with 124 additions and 71 deletions
|
|
@ -18,7 +18,6 @@ import { CacheService } from '@/core/CacheService.js';
|
|||
import { MiLocalUser } from '@/models/User.js';
|
||||
import { UserService } from '@/core/UserService.js';
|
||||
import { ChannelFollowingService } from '@/core/ChannelFollowingService.js';
|
||||
import { RoleService } from '@/core/RoleService.js';
|
||||
import { getIpHash } from '@/misc/get-ip-hash.js';
|
||||
import { LoggerService } from '@/core/LoggerService.js';
|
||||
import { SkRateLimiterService } from '@/server/api/SkRateLimiterService.js';
|
||||
|
|
@ -49,7 +48,6 @@ export class StreamingApiServerService {
|
|||
private usersService: UserService,
|
||||
private channelFollowingService: ChannelFollowingService,
|
||||
private rateLimiterService: SkRateLimiterService,
|
||||
private roleService: RoleService,
|
||||
private loggerService: LoggerService,
|
||||
) {
|
||||
}
|
||||
|
|
@ -57,22 +55,18 @@ export class StreamingApiServerService {
|
|||
@bindThis
|
||||
private async rateLimitThis(
|
||||
user: MiLocalUser | null | undefined,
|
||||
requestIp: string | undefined,
|
||||
requestIp: string,
|
||||
limit: IEndpointMeta['limit'] & { key: NonNullable<string> },
|
||||
) : Promise<boolean> {
|
||||
let limitActor: string;
|
||||
let limitActor: string | MiLocalUser;
|
||||
if (user) {
|
||||
limitActor = user.id;
|
||||
limitActor = user;
|
||||
} else {
|
||||
limitActor = getIpHash(requestIp || 'wtf');
|
||||
limitActor = getIpHash(requestIp);
|
||||
}
|
||||
|
||||
const factor = user ? (await this.roleService.getUserPolicies(user.id)).rateLimitFactor : 1;
|
||||
|
||||
if (factor <= 0) return false;
|
||||
|
||||
// Rate limit
|
||||
const rateLimit = await this.rateLimiterService.limit(limit, limitActor, factor);
|
||||
const rateLimit = await this.rateLimiterService.limit(limit, limitActor);
|
||||
return rateLimit.blocked;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue