devsisters / shardcake

Sharding and location transparency for Scala
https://devsisters.github.io/shardcake/
Apache License 2.0
389 stars 30 forks source link

enhancement: Change behaviour to `(String, Queue)` #61

Closed frekw closed 1 year ago

frekw commented 1 year ago

Hi!

Sometimes it might make sense to send a message to the entity itself from within the handler, e.g to schedule a message to be sent that encapsulates future work that needs to be done from within a behaviour itself. The current way to do this is to call into Sharding, which makes the behaviours hard to mock in tests, as well as introduces unnecessary overhead.

This changes the behaviour to instead receive the full Queue (rather than the Dequeue)which makes it trivial to just publish things to oneself (which is essentially how Erlang's process model works as well :) )

ghostdogpr commented 1 year ago

Actually I haven't got this problem so far, why is it hard to test? Using a single pod the message will be redirected to itself without going through the pods layer so there's no need to even expose the protocol API and there won't be much overhead (for example the message won't even be serialized). Is it because you test the behavior independently without even sharding?

frekw commented 1 year ago

Is it because you test the behavior independently without even sharding?

Exactly, or at least that was what I wanted to do initially. Since the behaviour is mostly reliant on just the queue, it felt as if it would be nice to test only the behaviour in isolation from the sharding.

And in the same sense it felt natural to me that the behaviour also got full access to its publish messages to itself (even if there's some caveats to that, but that could be solved by providing a forked publish function rather than the queue in its entirety).

Maybe it's my Erlang/Elixir background speaking, but it feels like an unnecessary detour/coupling to have to go through a central registry of all shards to pass a message to oneself :)

ghostdogpr commented 1 year ago

I see! Guess it doesn't hurt to give more power to the user anyway. Can you also make a PR for 1.x? 🙏

frekw commented 1 year ago

Done! https://github.com/devsisters/shardcake/pull/62