zampino

zampino

This proposal probably arises from my immature experience with GenStage,
but what if, at subscribe time a consumer could specify a selector i.e. a function
(event :: any -> boolean) which would filter events being sent to itself. Somewhat like:

GenStage.sync_subscribe(consumer, 
  to: producer, 
  selector: fn %{key: key} -> key =~ "foo" end
)

Now when producer emits an event say %{key: key}, this will be dispatched to consumer
only if "foo" matches key.
This is probably a similar approach to the partition dispatcher with extra control over the hashing function. Could I state this so?

I have a very naïve proof of concept in the form of a private pull request here.
https://github.com/zampino/gen_stage/pull/1

where I also modified the gen_event example:
https://github.com/zampino/gen_stage/blob/selective-broadcast-dispatcher/examples/gen_event.exs#L89-L108

What would the community think of this feature?
Is there already a way to achieve this with no need of extra work?

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This seems to be essentially what Partition Dispatcher does: https://hexdocs.pm/gen_stage/Experimental.GenStage.PartitionDispatcher.html#content

josevalim

josevalim

Creator of Elixir

The partition dispatcher has formats specified upfront and it does not allow sharing. For example, I can’t have three consumers where one consumers A, B, D, the other A, B, C and the other B, C and D.

@zampino can you please send a pull request? Your broadcaster code looks good, the only changes I would do are:

  1. Document the selector option
  2. Instead of Keyword.get(opts, :selector, fn _event -> true end), the selector should be nil if there is no selector. This way we don’t need to call Enum.filter(..., selector) if there is no selector, just return the list as is
  3. Do not change the examples folder for now
  4. Tests. Unit tests for broadcast_dispatcher_test.exs are fine :slight_smile:
zampino

zampino OP

Hi Ben,

thanks for the reply,
indeed that’s almost it.

But as far as I understand, using PartitionDispatcher
one needs to specify upfront the hashing range and the hash function,
hence the number and the shape of consumers must be known prior to starting the producer.

My approach above would allow subscribers to decide which events they are
interested in, somewhat dynamically.

In addition with the selector function I describe above there could be multiple consumers recipient of the same message, if I am not wrong…

zampino

zampino OP

Hi @josevalim,

thanks for the feedback,
I’ll be glad to submit a PR with your suggested changes,

cheers,
Andrea

zampino

zampino OP

…to give some further motivations,
we’re working with a dynamic population of stages which all subscribe to some producer
with broadcast dispatch.
Consumer stages gets created and updated continuously based on a stream of user events.

Each node might observe some other for changes derived from receiving
events from the source.
This is the reason why I’d need to specify which events I might be interested in, assuming that such events, carry the identifier of the node to which they’re primarily addressed.


At this point a question arises, what if I’d need to update the subscription options
(not necessarily the :selector, but in general)
at present dispatchers save they’re consumers (demands) in a list:

https://github.com/elixir-lang/gen_stage/blob/accec040079e93cbc1b9a57f0d22a365bcb4771e/lib/gen_stage/broadcast_dispatcher.ex#L82-L84

and not in a map (by ref), what is the intention behind this behaviour, maybe @josevalim?
Can I update subscription options without canceling and subscribing again?
If I’m not wrong, if I subscribe twice to a producer, I will receive events twice, is it correct?

josevalim

josevalim

Creator of Elixir

You cannot update subscriptions. I can see it becoming really complex given
certain behaviors: what if you update to a different max/min demand? What
if you try to change a partition?

zampino

zampino OP

I agree, especially in the demand auto-adjustment,
that could cause problems.

If I wish to change the selector then,
one could first re-subscribe with a new selector
and subsequently cancel the old ref, in this case a consumer
might get at most double messages
until the dispatcher cancels the old demand.

Would you suggest to do so?

Better guarding against duplicates in the stage,
than possibly losing events by first canceling and then re-subscribing right?

josevalim

josevalim

Creator of Elixir

What we could do is to support async_resubscribe and sync_resubscribe. The idea is that it cancels a previous subscription and starts a new one atomically. This way you won’t receive duplicated messages nor lose messages and the semantics make it clear it is a new subscription altogether. What do you think? If that fits your use case, please open up an issue and I can tackle it this week.

zampino

zampino OP

great, I’ll open an issue for resubscriptions
(the name itself sounds terrific :slight_smile:

ah, and that won’t block the :selector PR #112 right?

thanks again,
A

josevalim

josevalim

Creator of Elixir

No, it won’t. :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Discussions Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews