fix: make throttle config

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei 2024-04-30 20:40:17 +08:00
parent f1190e2ae4
commit 4543459336
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
6 changed files with 29 additions and 6 deletions

View File

@ -13,3 +13,7 @@ FORCE_CACHE_HEADER=false
# CUSTOM MONGO CONNECTION
MONGO_CONNECTION=
# Throttle
THROTTLE_TTL=10
THROTTLE_LIMIT=20

View File

@ -68,3 +68,8 @@ export const ENCRYPT = {
key: '593f62860255feb0a914534a43814b9809cc7534da7f5485cd2e3d3c8609acab',
enable: true,
}
export const THROTTLE_OPTIONS = {
ttl: 10_000,
limit: 50,
}

View File

@ -4,6 +4,7 @@ import { program } from 'commander'
import { load as yamlLoad } from 'js-yaml'
import { machineIdSync } from 'node-machine-id'
import { seconds } from '@nestjs/throttler'
import { isDebugMode, isDev } from './global/env.global'
import { parseBooleanishValue } from './utils'
import type { AxiosRequestConfig } from 'axios'
@ -62,6 +63,9 @@ const commander = program
'--encrypt_algorithm <string>',
'custom encrypt algorithm, default is aes-256-ecb',
)
// throttle
.option('--throttle_ttl <number>', 'throttle ttl')
.option('--throttle_limit <number>', 'throttle limit')
// other
.option('--color', 'force enable shell color')
@ -157,6 +161,10 @@ export const DEBUG_MODE = {
httpRequestVerbose:
argv.httpRequestVerbose ?? argv.http_request_verbose ?? true,
}
export const THROTTLE_OPTIONS = {
ttl: seconds(argv.throttle_ttl ?? 10),
limit: argv.throttle_limit ?? 10,
}
const ENCRYPT_KEY = argv.encrypt_key || MX_ENCRYPT_KEY
export const ENCRYPT = {

View File

@ -8,6 +8,7 @@ import { NoteModule } from '~/modules/note/note.module'
import { PageModule } from '~/modules/page/page.module'
import { PostModule } from '~/modules/post/post.module'
import { THROTTLE_OPTIONS } from '~/app.config'
import { AssetService } from './helper.asset.service'
import { BarkPushService } from './helper.bark.service'
import { CountingService } from './helper.counting.service'
@ -42,12 +43,7 @@ const providers: Provider<any>[] = [
@Module({
imports: [
ScheduleModule.forRoot(),
ThrottlerModule.forRoot([
{
ttl: 10_000,
limit: 50,
},
]),
ThrottlerModule.forRoot([THROTTLE_OPTIONS]),
EventEmitterModule.forRoot({
wildcard: false,
// the delimiter used to segment namespaces

View File

@ -14,6 +14,8 @@ services:
- ENCRYPT_ENABLE
- FORCE_CACHE_HEADER
- CDN_CACHE_HEADER
- THROTTLE_TTL
- THROTTLE_LIMIT
volumes:
- ./data/mx-space:/root/.mx-space

View File

@ -27,4 +27,12 @@ if [ "$ENCRYPT_ENABLE" = "true" ]; then
command+=" --encrypt_enable "
fi
if [ -n "$THROTTLE_TTL" ]; then
command+=" --throttle_ttl=${THROTTLE_TTL}"
fi
if [ -n "$THROTTLE_LIMIT" ]; then
command+=" --throttle_limit=${THROTTLE_LIMIT}"
fi
exec $command