Comments (10)
Thinking again, I see that my issue maybe solve having a reg_or_locate
for shared counters. Is that possible?
from gproc.
How about this:
safe_incr(C, Incr) ->
K = {c,l,C},
try gproc:update_shared_counter(K, Incr)
catch error:_ ->
try gproc:reg_shared(K, Incr), Incr
catch error:_ ->
gproc:update_shared_counter(K, Incr)
end
end.
from gproc.
A reg_or_locate_shared() feels a little bit weird when I try to get a feeling for the semantics. Possibly an ensure_shared_reg(Key, Value)?
from gproc.
The safe_incr
can still fail, right? It would need an infinite try catch as the reg/unreg can happen between the try catch treatment. Or am I understanding something wrong?
The reg_or_locate_shared
name would be based on existing functions reg_shared
and reg_or_locate
, but I agree that it's not really meaningful alone.
What about reg_or_locate
support the shared
atom as other functions do? And the ensure_shared_reg/2
would be an alias?
Do you think that's a good enhancement to gproc?
I advocate for this inclusion as it would be possible to keep shared counters without the trouble of building a safe critical session when creating/destroying shared counters.
from gproc.
And now thinking about the counter, it would be a better approach to reg_or_locate
and update the counter if exists.
update_or_reg_shared_counter
?
from gproc.
I think the three catch levels should be enough:
- Either the increment succeeds, or the counter isn't there
- Either we succeed in creating the counter, or someone beat us to it
- If 2 fails the counter should still be there; then we increment.
The last increment should not have a catch, since it should fail for most other cases.
I'm sure one can create a bizarre case where another process first creates, then deletes the counter...
I'll think about what a new API function should look like.
Ulf Wiger
http://ulf.wiger.net/
25 jul 2013 kl. 23:06 skrev Eduardo Gurgel [email protected]:
The safe_incr can still fail, right? It would need an infinite try catch as the reg/unreg can happen between the try catch treatment. Or am I understanding something wrong?
from gproc.
Ok, thank you Ulf. I will just explain my use case:
I have processes based on an associated id that when they spawn, they:
- Check if a shared counter with their id already exists
- If it exists increment
- Otherwise create one
If another process with the same associated id spawn, they do the same.
When they terminate, they decrement. If it reaches zero, destroy the shared counter.
The creation/destruction can happen during the steps described above.
Resuming it would be awesome to have:
- reg_or_update_shared_local_counter(...., InitialValue, UnregValue)
Where if the counter reaches the unregvalue, for example 0, -1, it becomes unregistered.
Maybe this is too crazy. Thank you again!
from gproc.
Considering the fact that many already think that the gproc API is bloated, I think I'll pass on adding this function. ;-)
from gproc.
I agree @uwiger and I think this change (erlang/otp#362) will solve my problems (considering that gproc will use it also if it gets merged).
from gproc.
Indeed.
from gproc.
Related Issues (20)
- pid weirdness with gproc_dist HOT 7
- Some gproc_dists_tests are very unstable (namely, t_sync_cand_dies, t_fail_node, t_master_dies) HOT 6
- Compiling with OTP 20 yields warnings about crypto:rand_uniform/2 HOT 4
- goodbye() doesn't work in global mode HOT 1
- Can't reset a shared counter
- Unable to start processes in global mode (Elixir)
- Generic server gproc terminating on reg HOT 2
- Warning On OTP 21.2 HOT 1
- aggregated counter can leak HOT 1
- Removing a worker from a pool does not make room for a new worker HOT 1
- gproc_pool round_robin is broken a bit HOT 2
- Monitor stops working if one of the processes monitoring the same name terminates
- Has gen_leader been deprecated ? HOT 1
- Replace key owner HOT 4
- local_only exception error when trying to register Pid golobally HOT 8
- OTP Release 23 depreciated functions HOT 1
- Use a stable app version number HOT 2
- The value of `auto_size` is not judged when the worker is removed HOT 1
- Relase for OTP 24 HOT 2
- If gproc:await finishes with timeout then generic leader gproc_dist terminating with error.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gproc.