Skip to content

Commit

Permalink
fix: make throttle config
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Apr 30, 2024
1 parent f1190e2 commit 4543459
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ FORCE_CACHE_HEADER=false

# CUSTOM MONGO CONNECTION
MONGO_CONNECTION=

# Throttle
THROTTLE_TTL=10
THROTTLE_LIMIT=20
5 changes: 5 additions & 0 deletions apps/core/src/app.config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ export const ENCRYPT = {
key: '593f62860255feb0a914534a43814b9809cc7534da7f5485cd2e3d3c8609acab',
enable: true,
}

export const THROTTLE_OPTIONS = {
ttl: 10_000,
limit: 50,
}
8 changes: 8 additions & 0 deletions apps/core/src/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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 = {
Expand Down
8 changes: 2 additions & 6 deletions apps/core/src/processors/helper/helper.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ services:
- ENCRYPT_ENABLE
- FORCE_CACHE_HEADER
- CDN_CACHE_HEADER
- THROTTLE_TTL
- THROTTLE_LIMIT

volumes:
- ./data/mx-space:/root/.mx-space
Expand Down
8 changes: 8 additions & 0 deletions docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4543459

Please sign in to comment.