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

Skip to content

[Lock] add a with helper #24146

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

[Lock] add a with helper #24146

wants to merge 1 commit into from

Conversation

jderusse
Copy link
Member

Q A
Branch? 3.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR TODO

From a suggestion of symfony/symfony-docs#7866 (comment)

$called = $lock->with(function() {
  //my businesss logic
});

@linaori
Copy link
Contributor

linaori commented Sep 11, 2017

Haven't used the locks myself yet, but as it's not in the interface, how will I know this method exists?

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Sep 11, 2017
@chalasr
Copy link
Member

chalasr commented Sep 13, 2017

with() doesn't tell me what this method does nor when it should be called (withXX is common for named constructors, not the case here).
Agree with Iltar about adding it to the interface while we can.

@jderusse
Copy link
Member Author

Thinking about closing this PR, Don't see a real avantage to use it

$lock->acquire()
try {
  doSomthing();
  doSomthing();
  doSomthing();
} finally {
  $lock->release()
}

vs

$lock->with(function() {
  doSomthing();
  doSomthing();
  doSomthing();
});

WDYT?

@nicolas-grekas
Copy link
Member

👍 for closing

@jderusse jderusse closed this Sep 24, 2017
@malkusch
Copy link

malkusch commented Mar 18, 2018

Don't see a real avantage to use it

The advantage is to remove the freedom of a developer to produce a class of bugs where code never reaches the release() call. There are some developers, like me, who are lazy as shit and don't want to think at all. When I see your API I would probably never think about the consequences not putting the release into finally block. By not even giving me that opportunity, you will make my code a bit more robust.

Actually I would even suggest removing acquire and release completely from the public API.

Btw. anyone interested in taking over a surprisingly similar named project: php-lock/lock#13

@theofidry
Copy link
Contributor

I also agree that requiring two calls is more error prone. The same goes for transactions with DBAL: you have both choice for greater flexibility but using the transaction(callback) method is always safer.

@b1rdex
Copy link
Contributor

b1rdex commented Mar 21, 2018

+1 that this method is more solid than acquire/release pair. This just needs better name.

@stof
Copy link
Member

stof commented Mar 21, 2018

Well, as we cannot add the method in the LockInterface (it would be a BC break), this API is not possible. The helper function would need to be implemented separate (and receiving the LockInterface object and the callable as argument)

@malkusch
Copy link

Well, as we cannot add the method in the LockInterface (it would be a BC break)

Adding is not a BC break. I suggest, add it and mark the others as deprecated. That should discourage users enough to use them. Then, the others can be removed in some distant new major release.

@jderusse
Copy link
Member Author

Adding a method to an interface is a BC break, given everybody who implements the interface will get a Fatal error: Class XXX contains 1 abstract method and must therefore be declared abstract or implement the remaining methods

Not sure to understand the point of deprecating all others (methods?)

@malkusch
Copy link

Adding a method to an interface is a BC break, given everybody who implements the interface

Oh, true. Ok, you can't do that without BC breaking.

Not sure to understand the point of deprecating all others (methods?)

Removing freedom to enforce more robust client code. The idea of having one method is to help the user not to make the mistake of forgetting a release. If you still leave acquire and release in the API, users will use it and potentially shoot them self. Maybe I'm too drastic as there might well be a use case for having them serperate.

@NoiseByNorthwest
Copy link

@chalasr @b1rdex I've suggested "with" as name because it is very common for this kind of RAII support, see try/with statement in Java 8+ or with statement in Python.
The meaning is "WITH this acquired resource do something then release it".

@jderusse jderusse deleted the lock-with branch August 2, 2019 12:17
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.

10 participants