Skip to content

[Messenger] Avoid reconnecting an already-connected Redis instance. #52811

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

Closed
wants to merge 1 commit into from
Closed

Conversation

BusterNeece
Copy link
Contributor

Q A
Branch? 7.1
Bug fix? yes?
New feature? yes?
Deprecations? no
Issues Fix #45057
License MIT

With the Messenger component's internal Redis Connection class, if you pass it a \Redis or \Relay instance that's already connected, it will still try to reconnect that instance using the host/port/etc. that's passed to the connection object's "options" array in the constructor.

Not only does this cause problems when the Redis instance is already connected, but it also prevents you from, say, using socket connections instead (as the only connection method supported by the class itself is IP-based connections) and just passing in a ready-to-go Redis instance.

While I personally use this class heavily in my own applications, I'm not aware of every possible implication of making a change like this, i.e. if there are other parts of the application that are counting on this working as-is (even though I can't imagine that unnecessarily reconnecting is a desired feature in this case). While it's intended to fix a bug in my specific use-case, I'd consider it also arguably "new" functionality that the class can now handle pre-connected instances being passed to it.

Basically, I'm new to contributing to Symfony so I'm a little shaky on my legs here, but the reported issue above mentioned needing a PR, and we're running into the issue ourselves, so here's the PR!

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

I'd be fine considering this as a bugfix, can you please target 5.4?

$connect = isset($params['persistent_id']) ? 'pconnect' : 'connect';
$redis->{$connect}($host, $port, $params['timeout'], $params['persistent_id'], $params['retry_interval'], $params['read_timeout'], ...(\defined('Redis::SCAN_PREFIX') || \extension_loaded('relay')) ? [['stream' => $params['ssl'] ?? null]] : []);

if (!$redis->isConnected()) {
Copy link
Member

Choose a reason for hiding this comment

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

What about auth, serializer option and dbindex? should we consider them already initialized?

Suggested change
if (!$redis->isConnected()) {
if ($redis->isConnected()) {
return $redis;
}

@BusterNeece
Copy link
Contributor Author

@nicolas-grekas Thank you for the review! I've submitted an updated version against the 5.4 branch in PR #52896.

@BusterNeece BusterNeece closed this Dec 5, 2023
fabpot added a commit that referenced this pull request Dec 8, 2023
…BusterNeece)

This PR was merged into the 5.4 branch.

Discussion
----------

[Messenger] Avoid reconnecting active Redis connections.

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #45057
| License       | MIT

With the Messenger component's internal Redis Connection class, if you pass it a \Redis or \Relay instance that's already connected, it will still try to reconnect that instance using the host/port/etc. that's passed to the connection object's "options" array in the constructor.

Not only does this cause problems when the Redis instance is already connected, but it also prevents you from, say, using socket connections instead (as the only connection method supported by the class itself is IP-based connections) and just passing in a ready-to-go Redis instance.

This PR is a resubmission of #52811 targeting 5.4 as it's a bug fix.

Commits
-------

650f153 bug #45057 [Messenger] Avoid reconnecting active Redis connections.
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.

[Messenger (Redis transport)] Connection should use custom Redis instance
3 participants