#Sharing actors via context object

1 messages · Page 1 of 1 (latest)

oak bloom
#

I want to understand how I should be sharing actors within a gleam project. It looks like I have a few options:

  1. pass around the actor itself(I am not even sure what the move semantics of gleam are but I assume this doesnt work if I also want it supervised?)
  2. Pass around a subject for the actor
  3. Named actors? (Still trying to understand what this means, do I need a registry for this?)
  4. Registry - use this to request a subject for the actor I want?

I am playing around with wisp and I have:

  let assert Ok(cache_supervisor) =
    supervisor.new(supervisor.OneForOne)
    |> supervisor.add(cache.supervised())
    |> supervisor.start

  let assert Ok(cache) = cache.new()
  let context = context.Context(cache)
  let handler = handle_request(_, context)

The supervision tree is what I think I need and I need to replace the cache.new() call with what I should be doing to use a supervised cache.

New to this whole ecosystem and trying to wrap my head around the gleam way to do it.

vocal viper
#

This actually looks okay to me! If you were using named processes you'd still be passing around a name, and a registry is just an actor so you'd be passing around a reference to that too

shell elm
#

All processes should be supervised, if cache.new creates a process it should be under the supervision tree

#

If there’s one one instance of a process in the app then a name is good

oak bloom
#

Ok so if I end up with multiple caches in the application, would named still be ok? Just each would need a unique name?

#

Sorry and also if I remove the let assert Ok(cache)=.. line, I need either the subject or a name right? Or is a registry what I want? Still confused on registries vs named actors right now

loud quail
#

That means that in your above example you'd need to assign the name to the cache in the scope where you're creating it (e.g. as a parameter in cache.supervised). Then you can send messages to the cache process using its named subject.

#

(And so you'd probably pass the corresponding Subject into the context for your request handler)

shell elm
#

Main thing is that you can’t dynamically create names

#

They are created once and live for the lifetime of the VM

oak bloom
#

Understood on the static front. I am making a small game and I have several maps and regions(chunk) within the maps. The way I see doing this would be to have each chunk be its own actor and active players be their own actor.

The way I initially think this works is that I have the main game application with a region supervisor and a player supervisor. I’m not sure yet if the game needs its own too, but I would want to know where a given player is, but also be able to find players in a given region.

To me this sounds like registries in elixir but I don’t see registries in the gleam otp library but I do see a couple of registry libraries in the ecosystem

Is there an idiomatic gleam way to do this?

shell elm
#

This is OTP, so you want OTP idioms!

#

Registries are the tool you want here, they're the name for systems that give you dynamic and non-global process naming

#

There are a number of different registries that offer different functionality, and which is best depends on what behaviour you want

#

If you don't have any particular desire you could use process groups, which come with OTP