Skip to content

feat(microservices): add specific transport id to microservices #14606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

maxbronnikov10
Copy link

@maxbronnikov10 maxbronnikov10 commented Feb 10, 2025

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

transportId field is hardcoded in specific server classes.
If users need more concurrency on same server or many servers need to extends from base server classes and override server id
Sample:

import { CustomTransportStrategy, KafkaOptions, ServerKafka } from '@nestjs/microservices';

export const NEW_KAFKA_TRANSPORT = Symbol('NEW_KAFKA_TRANSPORT');

export class NewKafkaServer extends ServerKafka implements CustomTransportStrategy {
  //@ts-expect-error In Nest.js ServerKafka can not override transportId
  transportId = NEW_KAFKA_TRANSPORT;

  constructor(config: KafkaOptions['options']) {
    super(config);
  }
}

Issue Number:
#14590
#11298

What is the new behavior?

Add new property to microservice options interface that overrides default transportId field in specific server classes

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@maxbronnikov10 maxbronnikov10 changed the title feat: add specific transport id to microservices feat(microservices): add specific transport id to microservices Feb 10, 2025
@coveralls
Copy link

coveralls commented Feb 13, 2025

Pull Request Test Coverage Report for Build 509e49e5-e054-465a-bd3f-5172427e2355

Details

  • 8 of 8 (100.0%) changed or added relevant lines in 8 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 88.94%

Totals Coverage Status
Change from base Build 0ff12a80-22b7-43a2-8e8a-1d5a5d5d96fc: 0.002%
Covered Lines: 7197
Relevant Lines: 8092

💛 - Coveralls

@maxbronnikov10 maxbronnikov10 force-pushed the feat/specific-transport-id branch 2 times, most recently from 926ed5f to 6c1e30d Compare May 2, 2025 00:46
@maxbronnikov10
Copy link
Author

@kamilmysliwiec can u check this PR please?

return new ServerRedis(options as Required<RedisOptions>['options']);
return new ServerRedis(
options as Required<RedisOptions>['options'],
buildServerSettings,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of introducing a 2nd constructor parameter, wouldn't it be easier to expose a setter for the transportId attribute? And then set it only if the user passed a custom transportId (which won't be the case for 99% apps out there anyway)

Copy link
Author

@maxbronnikov10 maxbronnikov10 May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be easier, but another problem arises - what if the developer changes the transportId via setter at runtime and add handlers dynamically, which will cause unexpected errors because the current Server code doesn't have a status model like started or not_started.
So if you don't want to have a second parameter in the constructor because it will cause breaking changes for those extending the Server class - we need to make a status model or accept that risk and leave it to the developer's responsibility

I realize it's an unusual problem, but it should have been highlighted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can just leave a note on this in the docs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@maxbronnikov10 maxbronnikov10 force-pushed the feat/specific-transport-id branch from 6c1e30d to 5a979f2 Compare May 8, 2025 16:44
@maxbronnikov10 maxbronnikov10 force-pushed the feat/specific-transport-id branch from 5a979f2 to 076fb0e Compare May 8, 2025 16:46
@kamilmysliwiec kamilmysliwiec merged commit a17f52d into nestjs:master May 12, 2025
5 checks passed
@kamilmysliwiec
Copy link
Member

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants