Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Add shuffle env processor #46883

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
merged 1 commit into from
Jul 10, 2022

Conversation

ostrolucky
Copy link
Contributor

@ostrolucky ostrolucky commented Jul 7, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR waiting on approval of feature

Motivation of this: We are specifying list of hosts for Redis cluster as env var. The way redis library unfortunately works is that it always takes first host and tries to connect to it (and after initial connection is done successfully, only then jump to other nodes). This makes first host under load much more than others. This env var processor could be used to solve it and distribute the load better.

Copy link
Contributor

@ro0NL ro0NL left a comment

Choose a reason for hiding this comment

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

IIUC you could seed shuffle using mt_srand in tests

@ostrolucky ostrolucky force-pushed the env-shuffle branch 3 times, most recently from 4c15709 to d18d383 Compare July 8, 2022 07:34
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.

works for me, here are some comments

@ostrolucky ostrolucky force-pushed the env-shuffle branch 3 times, most recently from 2caa9e5 to a921ed4 Compare July 9, 2022 08:55
@94noni
Copy link
Contributor

94noni commented Jul 10, 2022

Can/will this be chainable with the « env(csv:FOO) » env processor?
Like doing « env(shuffle:csv:FOO) »

@ostrolucky
Copy link
Contributor Author

of course

@fabpot
Copy link
Member

fabpot commented Jul 10, 2022

Thank you @ostrolucky.

@@ -206,6 +207,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
return null;
}

if ('shuffle' === $prefix) {
\is_array($env) ? shuffle($env) : throw new RuntimeException(sprintf('Env var "%s" cannot be shuffled, expected array, got "%s".', $name, get_debug_type($env)));
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO this is "abusing" ternary, the more "classical"

            if (!\is_array($env)) {
                throw new RuntimeException(sprintf('Env var "%s" cannot be shuffled, expected array, got "%s".', $name, get_debug_type($env)));
            }

            shuffle($env);

would be better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Subjective

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, hence "IMHO" 😉 but I can try "more objective" arguments:

  • it's not using the expression result (BTW: shuffle() always returns true [historically could also return false], and if designed today would probably be : void; and throw wasn't usable as/in an expression before PHP 8.0)
  • it's inconsistent with all the rest of the file (even the general codebase)

Dunno if the CS tool could flag it?

@fabpot fabpot mentioned this pull request Oct 24, 2022
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.

7 participants