Skip to content

Commit

Permalink
feat: limit and orchestration pull connections to mongo (#3814)
Browse files Browse the repository at this point in the history
Co-authored-by: Ihar <[email protected]>
  • Loading branch information
ihar-tsykala and Ihar authored Jun 21, 2024
1 parent 5f35043 commit 7c25b36
Show file tree
Hide file tree
Showing 47 changed files with 205 additions and 19 deletions.
5 changes: 5 additions & 0 deletions application-events/.env.docker
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
MQ_ADDRESS="message-broker"
MONGODB_SERVER_URL=mongodb://mongo:27017

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
4 changes: 4 additions & 0 deletions application-events/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
MQ_ADDRESS=localhost
MONGODB_SERVER_URL=mongodb://mongo:27017

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
3 changes: 3 additions & 0 deletions application-events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"@azure/core-rest-pipeline": "1.12.1",
"image-size": "1.0.2"
},
"imports": {
"#constants": "./dist/constants/index.js"
},
"dependencies": {
"@guardian/common": "^2.13.0",
"@guardian/interfaces": "^2.13.0",
Expand Down
1 change: 1 addition & 0 deletions application-events/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DEFAULT as DEFAULT_MONGO } from './mongo.js';
5 changes: 5 additions & 0 deletions application-events/src/constants/mongo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const DEFAULT = {
MIN_POOL_SIZE: '1',
MAX_POOL_SIZE: '5',
MAX_IDLE_TIME_MS: '30000',
};
7 changes: 7 additions & 0 deletions application-events/src/singletons/MongodbConnection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { MikroORM } from '@mikro-orm/core';
import { MongoDriver } from '@mikro-orm/mongodb';
import { Webhook } from '../entities/Webhook.js';
import process from 'process';
import { DEFAULT_MONGO } from '#constants';

export default class MongodbConnection {

Expand All @@ -14,6 +16,11 @@ export default class MongodbConnection {
dbName: 'application_events',
driver: MongoDriver,
entities: [ Webhook ],
driverOptions: {
minPoolSize: parseInt(process.env.MIN_POOL_SIZE ?? DEFAULT_MONGO.MIN_POOL_SIZE),
maxPoolSize: parseInt(process.env.MAX_POOL_SIZE ?? DEFAULT_MONGO.MAX_POOL_SIZE),
maxIdleTimeMS: parseInt(process.env.MAX_IDLE_TIME_MS ?? DEFAULT_MONGO.MAX_IDLE_TIME_MS)
},
});
};

Expand Down
5 changes: 4 additions & 1 deletion application-events/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"esModuleInterop": true,
"skipLibCheck": true,
"isolatedModules": true,
"moduleResolution": "node"
"moduleResolution": "node",
"paths": {
"#constants": ["constants/index.js"]
}
},
"include": ["./src/**/*", "./tests/**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
Expand Down
5 changes: 4 additions & 1 deletion application-events/tsconfig.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"esModuleInterop": true,
"skipLibCheck": true,
"isolatedModules": true,
"moduleResolution": "node"
"moduleResolution": "node",
"paths": {
"#constants": ["constants/index.js"]
}
},
"include": [
"./src/**/*",
Expand Down
5 changes: 5 additions & 0 deletions auth-service/configs/.env.auth
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ HASHICORP_ENCRIPTION_ALG="sha512"

ACCESS_TOKEN_UPDATE_INTERVAL=60000
REFRESH_TOKEN_UPDATE_INTERVAL=31536000000

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
5 changes: 5 additions & 0 deletions auth-service/configs/.env.auth.develop
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ AZURE_VAULT_NAME=guardianVault

ACCESS_TOKEN_UPDATE_INTERVAL=60000
REFRESH_TOKEN_UPDATE_INTERVAL=31536000000

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
5 changes: 5 additions & 0 deletions auth-service/configs/.env.auth.template
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ AZURE_VAULT_NAME=

ACCESS_TOKEN_UPDATE_INTERVAL=60000
REFRESH_TOKEN_UPDATE_INTERVAL=31536000000

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
8 changes: 6 additions & 2 deletions auth-service/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { MeecoAuthService } from './api/meeco-service.js';
import { ApplicationEnvironment } from './environment.js';
import { RoleService } from './api/role-service.js';
import { DEFAULT_MONGO } from '#constants';

Promise.all([
Migration({
Expand All @@ -25,9 +26,12 @@ Promise.all([
MikroORM.init<MongoDriver>({
...COMMON_CONNECTION_CONFIG,
driverOptions: {
useUnifiedTopology: true
useUnifiedTopology: true,
minPoolSize: parseInt(process.env.MIN_POOL_SIZE ?? DEFAULT_MONGO.MIN_POOL_SIZE, 10),
maxPoolSize: parseInt(process.env.MAX_POOL_SIZE ?? DEFAULT_MONGO.MAX_POOL_SIZE, 10),
maxIdleTimeMS: parseInt(process.env.MAX_IDLE_TIME_MS ?? DEFAULT_MONGO.MAX_IDLE_TIME_MS, 10)
},
ensureIndexes: true
ensureIndexes: true,
}),
MessageBrokerChannel.connect('AUTH_SERVICE'),
NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
Expand Down
2 changes: 2 additions & 0 deletions auth-service/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { REQUIRED_PROPS as USER_REQUIRED_PROPS, REGISTER_REQUIRED_PROPS } from './user.js';

export { DEFAULT as DEFAULT_MONGO } from './mongo.js';
5 changes: 5 additions & 0 deletions auth-service/src/constants/mongo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const DEFAULT = {
MIN_POOL_SIZE: '1',
MAX_POOL_SIZE: '5',
MAX_IDLE_TIME_MS: '30000',
};
11 changes: 11 additions & 0 deletions common/src/helpers/migration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { MikroORM } from '@mikro-orm/core';
import { MongoDriver } from '@mikro-orm/mongodb';
import { Migrator } from '@mikro-orm/migrations-mongodb';
import process from 'process';

const DEFAULT_MIN_POOL_SIZE = '1';
const DEFAULT_MAX_POOL_SIZE = '5';
const DEFAULT_MAX_IDLE_TIME_MS = '30000';
const RADIX = 10;

/**
* Define migration process
Expand All @@ -10,6 +16,11 @@ export async function Migration(initConfig: any, migrations?: string[]) {
const orm = await MikroORM.init<MongoDriver>({
...initConfig,
extensions: [Migrator],
driverOptions: {
minPoolSize: parseInt(process.env.MIN_POOL_SIZE ?? DEFAULT_MIN_POOL_SIZE, RADIX),
maxPoolSize: parseInt(process.env.MAX_POOL_SIZE ?? DEFAULT_MAX_POOL_SIZE, RADIX),
maxIdleTimeMS: parseInt(process.env.MAX_IDLE_TIME_MS ?? DEFAULT_MAX_IDLE_TIME_MS, RADIX),
},
});

const migrator = orm.getMigrator();
Expand Down
16 changes: 12 additions & 4 deletions common/src/secret-manager/migrations/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const workerEnvPath = path.join(process.cwd(), '../worker-service/.env')
const guardianCertsPath = path.join('../guardian-service/tls/vault/client.js')
const workerCertsPath = path.join('../worker-service/tls/vault/client.js')

const DEFAULT_MIN_POOL_SIZE = '1';
const DEFAULT_MAX_POOL_SIZE = '5';
const DEFAULT_MAX_IDLE_TIME_MS = '30000';
const RADIX = 10;

/**
* Set common configs for Vault
*/
Expand Down Expand Up @@ -127,10 +132,13 @@ async function migrate() {
entities: [
'dist/secret-manager/migrations/vault-account.js'
],
driverOptions: {
useUnifiedTopology: true
},
ensureIndexes: true,
driverOptions: {
useUnifiedTopology: true,
minPoolSize: parseInt(process.env.MIN_POOL_SIZE ?? DEFAULT_MIN_POOL_SIZE, RADIX),
maxPoolSize: parseInt(process.env.MAX_POOL_SIZE ?? DEFAULT_MAX_POOL_SIZE, RADIX),
maxIdleTimeMS: parseInt(process.env.MAX_IDLE_TIME_MS ?? DEFAULT_MAX_IDLE_TIME_MS, RADIX),
},
ensureIndexes: true,
})

DataBaseHelper.orm = db;
Expand Down
5 changes: 5 additions & 0 deletions configs/.env..guardian.system
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,8 @@ OPENAI_API_KEY=...
HOST_CACHE='cache'
PORT_CACHE='6379'
ENABLE_CACHE='true'

# MONGO_INIT
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
5 changes: 5 additions & 0 deletions configs/.env.develop.guardian.system
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,8 @@ OPENAI_API_KEY=...
HOST_CACHE='cache'
PORT_CACHE='6379'
ENABLE_CACHE='true'

# MONGO_INIT
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
7 changes: 6 additions & 1 deletion configs/.env.template.guardian.system
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ REFRESH_TOKEN_UPDATE_INTERVAL=31536000000
#CACHE
HOST_CACHE='cache'
PORT_CACHE='6379'
ENABLE_CACHE='true'
ENABLE_CACHE='true'

# MONGO_INIT
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
7 changes: 6 additions & 1 deletion configs/.env_SSV
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ WORKER_2_SERVICE_CHANNEL="worker.2"
#CACHE
HOST_CACHE='cache'
PORT_CACHE='6379'
ENABLE_CACHE='true'
ENABLE_CACHE='true'

# MONGO_INIT
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
5 changes: 5 additions & 0 deletions logger-service/configs/.env.logger
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ MQ_ADDRESS="localhost"
DB_HOST="localhost"
MQ_MAX_PAYLOAD="1048576"
#MQ_MESSAGE_CHUNK=5000000

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
5 changes: 5 additions & 0 deletions logger-service/configs/.env.logger.develop
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ MQ_ADDRESS="localhost"
DB_HOST="localhost"
MQ_MAX_PAYLOAD="1048576"
#MQ_MESSAGE_CHUNK=5000000

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
5 changes: 5 additions & 0 deletions logger-service/configs/.env.logger.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ MQ_ADDRESS=""
DB_HOST=""
MQ_MAX_PAYLOAD=""
#MQ_MESSAGE_CHUNK=5000000

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
3 changes: 3 additions & 0 deletions logger-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"main": "dist/index.js",
"name": "logger-service",
"packageManager": "[email protected]",
"imports": {
"#constants": "./dist/constants/index.js"
},
"scripts": {
"build": "tsc",
"build:prod": "tsc --project tsconfig.production.json",
Expand Down
8 changes: 6 additions & 2 deletions logger-service/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NestFactory } from '@nestjs/core';
import { Deserializer, IncomingRequest, MicroserviceOptions, Serializer, Transport } from '@nestjs/microservices';
import process from 'process';
import { AppModule } from './app.module.js';
import { DEFAULT_MONGO } from '#constants';

export class LoggerSerializer implements Serializer {
serialize(value: any, options?: Record<string, any>): any {
Expand All @@ -31,9 +32,12 @@ Promise.all([
MikroORM.init<MongoDriver>({
...COMMON_CONNECTION_CONFIG,
driverOptions: {
useUnifiedTopology: true
useUnifiedTopology: true,
minPoolSize: parseInt(process.env.MIN_POOL_SIZE ?? DEFAULT_MONGO.MIN_POOL_SIZE, 10),
maxPoolSize: parseInt(process.env.MAX_POOL_SIZE ?? DEFAULT_MONGO.MAX_POOL_SIZE, 10),
maxIdleTimeMS: parseInt(process.env.MAX_IDLE_TIME_MS ?? DEFAULT_MONGO.MAX_IDLE_TIME_MS, 10)
},
ensureIndexes: true
ensureIndexes: true,
}),
MessageBrokerChannel.connect('LOGGER_SERVICE'),
NestFactory.createMicroservice<MicroserviceOptions>(AppModule,{
Expand Down
1 change: 1 addition & 0 deletions logger-service/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DEFAULT as DEFAULT_MONGO } from './mongo.js';
5 changes: 5 additions & 0 deletions logger-service/src/constants/mongo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const DEFAULT = {
MIN_POOL_SIZE: '1',
MAX_POOL_SIZE: '5',
MAX_IDLE_TIME_MS: '30000',
};
3 changes: 2 additions & 1 deletion logger-service/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"baseUrl": "./src",
"paths": {
"@entity/*": ["entity/*"],
"@api/*": ["api/*"]
"@api/*": ["api/*"],
"#constants": ["constants/index.js"]
}
},
"include": [
Expand Down
3 changes: 2 additions & 1 deletion logger-service/tsconfig.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"baseUrl": "./src",
"paths": {
"@entity/*": ["entity/*"],
"@api/*": ["api/*"]
"@api/*": ["api/*"],
"#constants": ["constants/index.js"]
}
},
"include": [
Expand Down
5 changes: 5 additions & 0 deletions notification-service/configs/.env.notification
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ DB_DATABASE="notification_db"
MQ_ADDRESS="localhost"
DB_HOST="localhost"
MQ_MAX_PAYLOAD="1048576"

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
5 changes: 5 additions & 0 deletions notification-service/configs/.env.notification.develop
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ DB_DATABASE="notification_db"
MQ_ADDRESS="localhost"
DB_HOST="localhost"
MQ_MAX_PAYLOAD="1048576"

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
5 changes: 5 additions & 0 deletions notification-service/configs/.env.notification.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ DB_DATABASE="notification_db"
MQ_ADDRESS="localhost"
DB_HOST="localhost"
MQ_MAX_PAYLOAD="1048576"

# Mongo init
MIN_POOL_SIZE="1"
MAX_POOL_SIZE="5"
MAX_IDLE_TIME_MS="30000"
3 changes: 3 additions & 0 deletions notification-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"main": "dist/index.js",
"name": "notification-service",
"packageManager": "[email protected]",
"imports": {
"#constants": "./dist/constants/index.js"
},
"scripts": {
"build": "tsc",
"build:prod": "tsc --project tsconfig.production.json",
Expand Down
4 changes: 4 additions & 0 deletions notification-service/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import process from 'process';
import { AppModule } from './app.module.js';
import { DEFAULT_MONGO } from '#constants';

Promise.all([
Migration({
Expand All @@ -19,6 +20,9 @@ Promise.all([
...COMMON_CONNECTION_CONFIG,
driverOptions: {
useUnifiedTopology: true,
minPoolSize: parseInt(process.env.MIN_POOL_SIZE ?? DEFAULT_MONGO.MIN_POOL_SIZE, 10),
maxPoolSize: parseInt(process.env.MAX_POOL_SIZE ?? DEFAULT_MONGO.MAX_POOL_SIZE, 10),
maxIdleTimeMS: parseInt(process.env.MAX_IDLE_TIME_MS ?? DEFAULT_MONGO.MAX_IDLE_TIME_MS, 10)
},
ensureIndexes: true,
}),
Expand Down
1 change: 1 addition & 0 deletions notification-service/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DEFAULT as DEFAULT_MONGO } from './mongo.js';
Loading

0 comments on commit 7c25b36

Please sign in to comment.