Skip to content
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

feat(microservices): enable wildcards in redis microservice patterns #10359

Merged

Conversation

tijsmoree
Copy link
Contributor

@tijsmoree tijsmoree commented Oct 4, 2022

The redis microservice now makes use of psubscribe and pmessage if the wildcards option is enabled in the options of the microservice, which makes it possible to use wildcards as specified by the Redis documentation.

Closes #10344

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?

The current setup uses subscribe and message when listening for patterns in a Redis microservice, while using psubscribe and pmessage would make it possible to use wildcards in those patterns as prescribed by the Redis documentation.

Issue Number: #10344

What is the new behavior?

The change makes it possible to use wildcards in the Redis microservice patterns by enabling the wildcards option of the microservice.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@coveralls
Copy link

coveralls commented Oct 4, 2022

Pull Request Test Coverage Report for Build 8941bc69-2bf3-49ef-bed3-cadb50eb8c86

  • 7 of 9 (77.78%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 93.775%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/microservices/server/server-redis.ts 7 9 77.78%
Totals Coverage Status
Change from base Build 6b637f64-9c7a-4d7b-8537-b6a8d098f58e: -0.01%
Covered Lines: 6116
Relevant Lines: 6522

💛 - Coveralls

@@ -64,11 +62,11 @@ export class ServerRedis extends Server implements CustomTransportStrategy {
}

public bindEvents(subClient: Redis, pubClient: Redis) {
subClient.on(MESSAGE_EVENT, this.getMessageHandler(pubClient).bind(this));
subClient.on('pmessage', this.getMessageHandler(pubClient).bind(this));
Copy link
Member

Choose a reason for hiding this comment

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

Correct me if I'm wrong but I think this would introduce a breaking change.

Can we use "pmessage" and psubscribe if explicitly enabled by the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If psubscribe is being used, we must use 'pmessage' as well.

psubscribe is an extended version of subscribe, as far as I understand. The differences between subscribe and psubscribe are the wildcards and the third argument given back to the 'message'/'pmessage' event.

I do not believe there to be any breaking changes, where do you see one?

Copy link
Member

Choose a reason for hiding this comment

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

If someone used project:* as a pattern, then it was interpreted as project:* not project:{wildcard - put anything here}. This is a breaking change.

Likewise, if someone inherited from the built-in ServerRedis class and had tests spying on "on('message')", then these tests would fail now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is true. Would you rather like to ask the user explicitly whether they want to enable this feature than to make it a breaking change?

I don't know the usual way to go about this. Nor do I know how big the impact would be of a breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

I'd say exposing a flag (adding a new property to the redis transporter options) would be ideal. Someone could just explicitly turn wildcards on (e.g., { wildcards: true })

Copy link
Contributor Author

@tijsmoree tijsmoree Oct 12, 2022

Choose a reason for hiding this comment

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

I changed the PR and tested the system now so it uses an explicit option.

BTW, I found a bug in the test file of the redis server, where it changes the options by calling (server as any).options.options instead of (server as any).options.

The redis microservice now makes use of `psubscribe` and `pmessage`
when the `wildcards` option is enabled in the options of the microservice,
which makes it possible to use wildcards as specified by the Redis
documentation.

Closes nestjs#10344
@Guergeiro
Copy link

Any updates on this PR? Having wildcard support would be really useful.

@kamilmysliwiec kamilmysliwiec added this to the 10.0.0 milestone Apr 17, 2023
@kamilmysliwiec kamilmysliwiec changed the base branch from master to 10.0.0 April 17, 2023 11:12
@kamilmysliwiec kamilmysliwiec merged commit 69fba03 into nestjs:10.0.0 Apr 17, 2023
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.

Wildcard support for Redis subscriptions in microservices
5 participants