refactor(server): config system (#11081)

This commit is contained in:
forehalo
2025-03-27 12:32:28 +00:00
parent 7091111f85
commit 0ea38680fa
274 changed files with 7583 additions and 5841 deletions
@@ -1,27 +1,38 @@
import { defineStartupConfig, ModuleConfig } from '../config';
import { defineModuleConfig } from '../config';
export type ThrottlerType = 'default' | 'strict';
type ThrottlerStartupConfigurations = {
[key in ThrottlerType]: {
ttl: number;
limit: number;
};
};
declare module '../config' {
interface AppConfig {
throttler: ModuleConfig<ThrottlerStartupConfigurations>;
declare global {
interface AppConfigSchema {
throttle: {
enabled: boolean;
throttlers: {
[key in ThrottlerType]: ConfigItem<{
ttl: number;
limit: number;
}>;
};
};
}
}
defineStartupConfig('throttler', {
default: {
ttl: 60,
limit: 120,
defineModuleConfig('throttle', {
enabled: {
desc: 'Whether the throttler is enabled.',
default: true,
},
strict: {
ttl: 60,
limit: 20,
'throttlers.default': {
desc: 'The config for the default throttler.',
default: {
ttl: 60,
limit: 120,
},
},
'throttlers.strict': {
desc: 'The config for the strict throttler.',
default: {
ttl: 60,
limit: 20,
},
},
});
@@ -24,14 +24,19 @@ export class ThrottlerStorage extends ThrottlerStorageService {}
@Injectable()
class CustomOptionsFactory implements ThrottlerOptionsFactory {
constructor(private readonly storage: ThrottlerStorage) {}
constructor(
private readonly config: Config,
private readonly storage: ThrottlerStorage
) {}
createThrottlerOptions() {
const options: ThrottlerModuleOptions = {
throttlers: Object.entries(AFFiNE.throttler).map(([name, config]) => ({
name,
...config,
})),
throttlers: Object.entries(this.config.throttle.throttlers).map(
([name, config]) => ({
name,
...config,
})
),
storage: this.storage,
};
@@ -84,6 +89,7 @@ export class CloudThrottlerGuard extends ThrottlerGuard {
ttl,
blockDuration,
} = request;
let limit = request.limit;
// give it 'default' if no throttler is specified,
@@ -110,13 +116,9 @@ export class CloudThrottlerGuard extends ThrottlerGuard {
let tracker = await this.getTracker(req);
if (this.config.node.dev) {
limit = Number.MAX_SAFE_INTEGER;
} else {
// custom limit or ttl APIs will be treated standalone
if (limit !== throttlerOptions.limit || ttl !== throttlerOptions.ttl) {
tracker += ';custom';
}
// custom limit or ttl APIs will be treated standalone
if (limit !== throttlerOptions.limit || ttl !== throttlerOptions.ttl) {
tracker += ';custom';
}
const key = this.generateKey(
@@ -151,6 +153,10 @@ export class CloudThrottlerGuard extends ThrottlerGuard {
}
override async canActivate(context: ExecutionContext): Promise<boolean> {
if (!this.config.throttle.enabled) {
return true;
}
const { req } = this.getRequestResponse(context);
const throttler = this.getSpecifiedThrottler(context);