@@ -5,13 +5,16 @@ import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
5
5
import { FileUpload } from 'graphql-upload' ;
6
6
import { Repository } from 'typeorm' ;
7
7
8
- import { ServerlessExecuteResult } from 'src/engine/integrations/serverless/drivers/interfaces/serverless-driver.interface' ;
9
8
import { FileStorageExceptionCode } from 'src/engine/integrations/file-storage/interfaces/file-storage-exception' ;
9
+ import { ServerlessExecuteResult } from 'src/engine/integrations/serverless/drivers/interfaces/serverless-driver.interface' ;
10
10
11
+ import { ThrottlerService } from 'src/engine/core-modules/throttler/throttler.service' ;
12
+ import { EnvironmentService } from 'src/engine/integrations/environment/environment.service' ;
11
13
import { FileStorageService } from 'src/engine/integrations/file-storage/file-storage.service' ;
12
14
import { readFileContent } from 'src/engine/integrations/file-storage/utils/read-file-content' ;
13
15
import { SOURCE_FILE_NAME } from 'src/engine/integrations/serverless/drivers/constants/source-file-name' ;
14
16
import { ServerlessService } from 'src/engine/integrations/serverless/serverless.service' ;
17
+ import { getServerlessFolder } from 'src/engine/integrations/serverless/utils/serverless-get-folder.utils' ;
15
18
import { CreateServerlessFunctionFromFileInput } from 'src/engine/metadata-modules/serverless-function/dtos/create-serverless-function-from-file.input' ;
16
19
import { UpdateServerlessFunctionInput } from 'src/engine/metadata-modules/serverless-function/dtos/update-serverless-function.input' ;
17
20
import {
@@ -24,7 +27,6 @@ import {
24
27
} from 'src/engine/metadata-modules/serverless-function/serverless-function.exception' ;
25
28
import { serverlessFunctionCreateHash } from 'src/engine/metadata-modules/serverless-function/utils/serverless-function-create-hash.utils' ;
26
29
import { isDefined } from 'src/utils/is-defined' ;
27
- import { getServerlessFolder } from 'src/engine/integrations/serverless/utils/serverless-get-folder.utils' ;
28
30
29
31
@Injectable ( )
30
32
export class ServerlessFunctionService extends TypeOrmQueryService < ServerlessFunctionEntity > {
@@ -33,6 +35,8 @@ export class ServerlessFunctionService extends TypeOrmQueryService<ServerlessFun
33
35
private readonly serverlessService : ServerlessService ,
34
36
@InjectRepository ( ServerlessFunctionEntity , 'metadata' )
35
37
private readonly serverlessFunctionRepository : Repository < ServerlessFunctionEntity > ,
38
+ private readonly throttlerService : ThrottlerService ,
39
+ private readonly environmentService : EnvironmentService ,
36
40
) {
37
41
super ( serverlessFunctionRepository ) ;
38
42
}
@@ -86,6 +90,8 @@ export class ServerlessFunctionService extends TypeOrmQueryService<ServerlessFun
86
90
payload : object | undefined = undefined ,
87
91
version = 'latest' ,
88
92
) : Promise < ServerlessExecuteResult > {
93
+ await this . throttleExecution ( workspaceId ) ;
94
+
89
95
const functionToExecute = await this . serverlessFunctionRepository . findOne ( {
90
96
where : {
91
97
id,
@@ -268,4 +274,19 @@ export class ServerlessFunctionService extends TypeOrmQueryService<ServerlessFun
268
274
269
275
return await this . findById ( createdServerlessFunction . id ) ;
270
276
}
277
+
278
+ private async throttleExecution ( workspaceId : string ) {
279
+ try {
280
+ await this . throttlerService . throttle (
281
+ `${ workspaceId } -serverless-function-execution` ,
282
+ this . environmentService . get ( 'SERVERLESS_FUNCTION_EXEC_THROTTLE_LIMIT' ) ,
283
+ this . environmentService . get ( 'SERVERLESS_FUNCTION_EXEC_THROTTLE_TTL' ) ,
284
+ ) ;
285
+ } catch ( error ) {
286
+ throw new ServerlessFunctionException (
287
+ 'Serverless function execution rate limit exceeded' ,
288
+ ServerlessFunctionExceptionCode . SERVERLESS_FUNCTION_EXECUTION_LIMIT_REACHED ,
289
+ ) ;
290
+ }
291
+ }
271
292
}
0 commit comments