[erlang-questions] conditional pub-sub in gproc

Tim Watson <>
Sat Jun 16 00:17:56 CEST 2012


This is pretty cool - I also noticed await/1 for the first time, which 
looks pretty useful for synchronizing distributed startup. Looking at 
the code, gproc_dist seems to be using gen_leader. Is this something 
you'd be comfortable using in production (I assume yes) - I only ask 
because I've seen numerous threads talking about various incarnations of 
it, and I wondered whether it's considered stable now? I'm also very 
interested to know what kind of consistency guarantees I have when using 
this in a distributed scenario.

Cheers,
Tim

On 15/06/2012 19:33, Ulf Wiger wrote:
> I threw in a function for doing pub/sub with simple filter conditions in gproc.
>
> https://github.com/esl/gproc/blob/master/doc/gproc_ps.md#subscribe_cond-3
>
> A simple example:
>
> Eshell V5.9  (abort with ^G)
> 1>  application:start(gproc).
> ok
>
> %% Subscribe to level changes for a gauge; only report if Level>  3
>
> 2>  gproc_ps:subscribe_cond(l,gauge,ets:fun2ms(fun({level,N}) when N>  3 ->  true end)).
> true
>
> 3>  gproc_ps:publish_cond(l,gauge,{level,1}).
> ok
> 4>  flush().
> ok		% no message delivered
>
> 5>  gproc_ps:publish_cond(l,gauge,{level,4}).
> ok
> 6>  flush().
> Shell got {gproc_ps_event,gauge,{level,4}}
> ok
>
> Essentially, gproc attaches a match_spec to the subscription entry, and this
> match_spec is evaluated when each event is published.
>
> If ets:match_spec_run([Msg], ets:match_spec_compile(Condition)) == [true],
> the message is delivered; otherwise not.
>
> The match specs are stored un-compiled, partly because you can't pass
> compiled match specs between nodes (gproc's global scope), and partly
> because you can't read them when compiled, and it's useful to be able to
> inspect properties:
>
> 7>  gproc:info(self()).
> [{gproc,[{{p,l,{gproc_ps_event,gauge}},
>            [{{level,'$1'},[{'>','$1',3}],[true]}]}]},
>   {current_function,{erl_eval,do_apply,6}},
>   {initial_call,{erlang,apply,2}},
>   {status,running},
>>
> Feedback is welcome.
>
> BR,
> Ulf W
>
> Ulf Wiger, Co-founder&  Developer Advocate, Feuerlabs Inc.
> http://feuerlabs.com
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list