Skip to content

[FrameworkBundle] Add ability to use existing service as lock/semaphore resource #58249

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
Sep 18, 2024

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented Sep 13, 2024

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Contains #58250

Currently, it's not possible to set an existing service as the lock/semaphore resource via the config, even though both factories support this functionality, eg:

public static function createStore(#[\SensitiveParameter] object|string $connection): PersistingStoreInterface
{
switch (true) {
case $connection instanceof \Redis:
case $connection instanceof Relay:
case $connection instanceof \RedisArray:
case $connection instanceof \RedisCluster:
case $connection instanceof \Predis\ClientInterface:
return new RedisStore($connection);

@@ -2004,6 +2004,9 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
$storeDefinitions = [];
foreach ($resourceStores as $resourceStore) {
$storeDsn = $container->resolveEnvPlaceholders($resourceStore, null, $usedEnvs);
if (!($usedEnvs || preg_match('#^[a-z]++://#', $resourceStore))) {
$resourceStore = new Reference($resourceStore);
}
Copy link
Contributor Author

@HypeMC HypeMC Sep 13, 2024

Choose a reason for hiding this comment

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

}
$container->resolveEnvPlaceholders($config['handler_id'], null, $usedEnvs);
if ($usedEnvs || preg_match('#^[a-z]++://#', $config['handler_id'])) {
$id = '.cache_connection.'.ContainerBuilder::hash($config['handler_id']);

</xsd:sequence>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="resource" type="xsd:string" />
Copy link
Member

Choose a reason for hiding this comment

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

why allowing a resource attribute here ? this does not make to me: we still need a name for the lock being registered, so <framework:resource name="my_lock">my_service</framework:resource> is still the expected XML format to me.

Copy link
Member

Choose a reason for hiding this comment

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

same for semaphore of course

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof This is the equivalent of doing:

framework:
    lock: dsn-or-service

If it's a string, the default name is used:

->beforeNormalization()
->ifString()->then(fn ($v) => ['default' => $v])
->end()

Copy link
Member

Choose a reason for hiding this comment

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

The issue is that the XML schema allows providing both the attribute and child elements, which would do weird stuff. I'd rather keep the clean XML schema as we have right now, even though it does not support one of the shortcut notation.

If someone is using XML configuration file, they will not complain to us that their configuration is too verbose due to that missing shortcut, as using XML in itself would be responsible for a lot more verbosity. Projects using XML configuration file generally do it to benefit from the XML schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@HypeMC HypeMC force-pushed the use-services-in-lock-and-semaphore branch from c2a12b4 to 34e5112 Compare September 13, 2024 17:15
nicolas-grekas added a commit that referenced this pull request Sep 16, 2024
…aphore config (HypeMC)

This PR was merged into the 6.4 branch.

Discussion
----------

[FrameworkBundle] Finish incomplete tests for lock & semaphore config

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

I noticed this while working on #58249. Basically, the test files were partially added but never used, so the tests were incomplete. ~Also, there's a bug in the schema, as the configuration in those files didn't work.~

Commits
-------

0574c86 [FrameworkBundle] Fix schema & finish incomplete tests for lock & semaphore config
@@ -2004,6 +2004,9 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
$storeDefinitions = [];
foreach ($resourceStores as $resourceStore) {
$storeDsn = $container->resolveEnvPlaceholders($resourceStore, null, $usedEnvs);
if (!($usedEnvs || preg_match('#^[a-z]++://#', $resourceStore))) {
Copy link
Member

Choose a reason for hiding this comment

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

that should be enough IMHO:

Suggested change
if (!($usedEnvs || preg_match('#^[a-z]++://#', $resourceStore))) {
if (!$usedEnvs && !str_contains($resourceStore, '://')) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

LGTM

@HypeMC HypeMC force-pushed the use-services-in-lock-and-semaphore branch from 34e5112 to f53ef17 Compare September 16, 2024 15:26
@HypeMC HypeMC force-pushed the use-services-in-lock-and-semaphore branch from f53ef17 to e91d7aa Compare September 17, 2024 22:56
@HypeMC HypeMC force-pushed the use-services-in-lock-and-semaphore branch from e91d7aa to 670ee5d Compare September 18, 2024 15:08
@fabpot
Copy link
Member

fabpot commented Sep 18, 2024

Thank you @HypeMC.

@fabpot fabpot merged commit c8137fe into symfony:7.2 Sep 18, 2024
9 of 10 checks passed
@HypeMC HypeMC deleted the use-services-in-lock-and-semaphore branch September 18, 2024 16:09
@@ -2004,6 +2004,9 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
$storeDefinitions = [];
foreach ($resourceStores as $resourceStore) {
$storeDsn = $container->resolveEnvPlaceholders($resourceStore, null, $usedEnvs);
if (!$usedEnvs && !str_contains($resourceStore, '://')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This breaks BC in some use cases, like doctrine-bundle tests revealed https://github.com/doctrine/DoctrineBundle/actions/runs/10976729897/job/30477816481

fabpot added a commit that referenced this pull request Oct 6, 2024
…vice IDs (HypeMC)

This PR was merged into the 7.2 branch.

Discussion
----------

[FrameworkBundle][Lock] Fix certain DSNs identified as service IDs

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #58249 (comment)
| License       | MIT

Follow up to #58249

The first PR introduced a BC break, as lock DSNs can also be in the following formats:

```yaml
framework:
    lock: 'flock'
    lock: 'semaphore'
    lock: 'in-memory'

    lock: 'mysql:host=127.0.0.1;dbname=app'
    lock: 'pgsql:host=127.0.0.1;dbname=app'
    lock: 'pgsql+advisory:host=127.0.0.1;dbname=app'
    lock: 'sqlsrv:server=127.0.0.1;Database=app'
    lock: 'oci:host=127.0.0.1;dbname=app'
```

This PR introduces two changes:
1. The framework bundle now treats any string containing a `:` as a DSN, instead of only those with `://`
2. `flock`, `semaphore`, and `in-memory` are now treated as special cases

Commits
-------

9abfbe0 [FrameworkBundle][Lock] Fix certain DSNs identified as service IDs
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Oct 6, 2024
…vice IDs (HypeMC)

This PR was merged into the 7.2 branch.

Discussion
----------

[FrameworkBundle][Lock] Fix certain DSNs identified as service IDs

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix symfony/symfony#58249 (comment)
| License       | MIT

Follow up to #58249

The first PR introduced a BC break, as lock DSNs can also be in the following formats:

```yaml
framework:
    lock: 'flock'
    lock: 'semaphore'
    lock: 'in-memory'

    lock: 'mysql:host=127.0.0.1;dbname=app'
    lock: 'pgsql:host=127.0.0.1;dbname=app'
    lock: 'pgsql+advisory:host=127.0.0.1;dbname=app'
    lock: 'sqlsrv:server=127.0.0.1;Database=app'
    lock: 'oci:host=127.0.0.1;dbname=app'
```

This PR introduces two changes:
1. The framework bundle now treats any string containing a `:` as a DSN, instead of only those with `://`
2. `flock`, `semaphore`, and `in-memory` are now treated as special cases

Commits
-------

9abfbe056d [FrameworkBundle][Lock] Fix certain DSNs identified as service IDs
@fabpot fabpot mentioned this pull request Oct 27, 2024
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.

6 participants