From 1bf7358e92116dff64466cb85a700bf9ce54d419 Mon Sep 17 00:00:00 2001 From: mrcfps Date: Fri, 24 Jan 2025 10:06:34 +0800 Subject: [PATCH 1/2] fix(api): correctly update usage meter when creating and cancelling subscriptions --- apps/api/src/subscription/subscription.service.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/apps/api/src/subscription/subscription.service.ts b/apps/api/src/subscription/subscription.service.ts index f5e621f1d..a9901afbb 100644 --- a/apps/api/src/subscription/subscription.service.ts +++ b/apps/api/src/subscription/subscription.service.ts @@ -220,8 +220,12 @@ export class SubscriptionService implements OnModuleInit { subscriptionId: sub.subscriptionId, startAt: startOfDay(now), endAt, + t1CountQuota: plan?.t1CountQuota ?? this.config.get('quota.request.t1'), + t1CountUsed: 0, t1TokenQuota: plan?.t1TokenQuota ?? this.config.get('quota.token.t1'), t1TokenUsed: 0, + t2CountQuota: plan?.t2CountQuota ?? this.config.get('quota.request.t2'), + t2CountUsed: 0, t2TokenQuota: plan?.t2TokenQuota ?? this.config.get('quota.token.t2'), t2TokenUsed: 0, }, @@ -236,6 +240,7 @@ export class SubscriptionService implements OnModuleInit { }, data: { subscriptionId: sub.subscriptionId, + fileCountQuota: plan?.fileCountQuota ?? this.config.get('quota.storage.file'), objectStorageQuota: plan?.objectStorageQuota ?? this.config.get('quota.storage.object'), vectorStorageQuota: plan?.vectorStorageQuota ?? this.config.get('quota.storage.vector'), }, @@ -296,6 +301,7 @@ export class SubscriptionService implements OnModuleInit { }, data: { subscriptionId: null, + fileCountQuota: freePlan?.fileCountQuota ?? this.config.get('quota.storage.file'), objectStorageQuota: freePlan?.objectStorageQuota ?? this.config.get('quota.storage.object'), vectorStorageQuota: @@ -504,8 +510,13 @@ export class SubscriptionService implements OnModuleInit { } // Otherwise, create a new meter - const startAt = lastMeter?.endAt ?? startOfDay(now); + let startAt: Date; const planType = sub?.planType || 'free'; + if (planType === 'free') { + startAt = startOfDay(now); + } else { + startAt = lastMeter?.endAt ?? startOfDay(now); + } // For free plan, the meter ends at the next day // For paid plan, the meter ends at the next month From 1a5c3d4013bdd3960a60b6810ba2c2a9d66f1e33 Mon Sep 17 00:00:00 2001 From: mrcfps Date: Fri, 24 Jan 2025 10:08:13 +0800 Subject: [PATCH 2/2] fix(api): add redis-based throttler back --- apps/api/src/app.module.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/api/src/app.module.ts b/apps/api/src/app.module.ts index 9a3fa3be1..ae836827e 100644 --- a/apps/api/src/app.module.ts +++ b/apps/api/src/app.module.ts @@ -1,8 +1,10 @@ import { ExecutionContext, Module } from '@nestjs/common'; import { BullModule } from '@nestjs/bullmq'; +import { APP_GUARD } from '@nestjs/core'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { LoggerModule } from 'nestjs-pino'; import { SkipThrottle, ThrottlerGuard, ThrottlerModule, seconds } from '@nestjs/throttler'; +import { ThrottlerStorageRedisService } from '@nest-lab/throttler-storage-redis'; import api from '@opentelemetry/api'; import { CommonModule } from '@/common/common.module'; @@ -25,7 +27,6 @@ import { CanvasModule } from './canvas/canvas.module'; import { CollabModule } from './collab/collab.module'; import { ActionModule } from './action/action.module'; import { RedisService } from '@/common/redis.service'; -import { APP_GUARD } from '@nestjs/core'; class CustomThrottlerGuard extends ThrottlerGuard { protected async shouldSkip(context: ExecutionContext): Promise { @@ -60,7 +61,7 @@ class CustomThrottlerGuard extends ThrottlerGuard { }, ], getTracker: (req) => (req.ips?.length ? req.ips[0] : req.ip), - // storage: new ThrottlerStorageRedisService(redis), + storage: new ThrottlerStorageRedisService(redis), }), }), LoggerModule.forRoot({