nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.91k stars 7.55k forks source link

RabbitMQ connection is not reused #11702

Closed MinaFayez9 closed 1 year ago

MinaFayez9 commented 1 year ago

Is there an existing issue for this?

Current behavior

NestJS is creating a new connection and a new channel for each worker or client even if they are on the same server By my understanding this is a bad practice and you should reuse the connection and the channel Here is one of the references: 13 Common RabbitMQ Mistakes and How to Avoid Them

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-udy3jt

Steps to reproduce

  1. Download the code on a machine with RabbitMQ server with Management UI or any other way to check for connections and channels
  2. Adjust the connections if necessary, inside main.ts and app.module.ts
  3. npm ci
  4. npm run start:dev
  5. Call the server from browser like http://localhost:3000/ to trigger the clients to send messages
  6. Check RabbitMQ management UI, you will find 3 connections and 3 channels created

Expected behavior

Only 1 or 2 connections and channels should be created

Package

Other package

No response

NestJS version

8.7.7

Packages versions

"dependencies": {
    "@automapper/classes": "^8.7.7",
    "@automapper/core": "^8.7.7",
    "@automapper/nestjs": "^8.7.7",
    "@casl/ability": "^6.5.0",
    "@nestjs/common": "^9.4.2",
    "@nestjs/config": "^2.3.2",
    "@nestjs/core": "^9.4.2",
    "@nestjs/event-emitter": "^1.4.1",
    "@nestjs/microservices": "^9.4.2",
    "@nestjs/mongoose": "^9.2.2",
    "@nestjs/passport": "^9.0.3",
    "@nestjs/platform-express": "^9.4.2",
    "@nestjs/schedule": "^2.2.2",
    "@nestjs/swagger": "^6.3.0",
    "@nestjs/throttler": "^4.0.0",
    "amqp-connection-manager": "^4.1.13",
    "amqplib": "^0.10.3",
    "chalk": "^4.1.2",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "compression": "^1.7.4",
    "cookie-parser": "^1.4.6",
    "csrf-csrf": "^2.2.4",
    "file-type": "^16.5.4",
    "helmet": "^7.0.0",
    "joi": "^17.9.2",
    "lodash": "^4.17.21",
    "mongoose": "^7.2.2",
    "mongoose-autopopulate": "^1.0.1",
    "passport": "^0.6.0",
    "passport-http-bearer": "^1.0.1",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1",
    "uuid": "^9.0.0"
},
"devDependencies": {
    "@nestjs/cli": "^9.5.0",
    "@nestjs/schematics": "^9.2.0",
    "@nestjs/testing": "^9.4.2",
    "@types/amqplib": "^0.10.1",
    "@types/compression": "^1.7.2",
    "@types/cookie-parser": "^1.4.3",
    "@types/cron": "^2.0.1",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/lodash": "^4.14.195",
    "@types/multer": "^1.4.7",
    "@types/node": "^18.16.16",
    "@types/passport": "^1.0.12",
    "@types/passport-http-bearer": "^1.0.37",
    "@types/supertest": "^2.0.12",
    "@types/uuid": "^9.0.1",
    "@typescript-eslint/eslint-plugin": "^5.59.9",
    "@typescript-eslint/parser": "^5.59.9",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^8.8.0",
    "husky": "^8.0.3",
    "jest": "^29.5.0",
    "lint-staged": "^13.2.2",
    "prettier": "^2.8.8",
    "sort-package-json": "^2.4.1",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "~5.0.4"
}

Node.js version

18.16.0

In which operating systems have you tested?

Other

image image

kamilmysliwiec commented 1 year ago

This is intentional.

Please search through some of our old issues on this (this has been discussed several times in the past).